Skip to content

Commit 2578e7f

Browse files
committed
update tests
1 parent c8e073d commit 2578e7f

File tree

14 files changed

+1017
-235
lines changed

14 files changed

+1017
-235
lines changed

.env.php.local

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export PATH="/usr/local/opt/php@8.1/bin:$PATH"
2+
export PATH="/usr/local/opt/php@8.1/sbin:$PATH"

.travis.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
language: php
2+
3+
php:
4+
- '8.1'
5+
- '8.2'
6+
- '8.3'
7+
- '8.4'
8+
- nightly
9+
10+
env:
11+
- TESTER_PHP_BIN="php"
12+
- TESTER_PHP_BIN="phpdbg"
13+
14+
before_install:
15+
- composer self-update
16+
17+
install:
18+
- composer install --no-interaction --prefer-source
19+
20+
matrix:
21+
exclude:
22+
- php: 8.2
23+
env: TESTER_PHP_BIN="phpdbg"
24+
25+
- php: 8.3
26+
env: TESTER_PHP_BIN="phpdbg"
27+
28+
- php: 8.4
29+
env: TESTER_PHP_BIN="phpdbg"
30+
31+
- php: nightly
32+
env: TESTER_PHP_BIN="phpdbg"
33+
allow_failures:
34+
- php: nightly
35+
36+
script:
37+
- ./vendor/bin/tester ./tests -p $TESTER_PHP_BIN -s --coverage coverage.html --coverage-src src/Parsem
38+
39+
after_failure:
40+
# Print *.actual file contents
41+
- for i in $(find ./tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
{
1616
"name": "Matronator",
1717
"email": "5470780+matronator@users.noreply.github.com",
18-
"homepage": "https://matronator.com",
18+
"homepage": "https://matronator.cz",
1919
"role": "Developer"
2020
}
2121
],
@@ -32,7 +32,7 @@
3232
}
3333
},
3434
"scripts": {
35-
"test": "tester ."
35+
"test": "tester tests/ -p phpdbg --coverage coverage.html --coverage-src src/Parsem"
3636
},
3737
"minimum-stability": "dev",
3838
"funding": [

coverage.html

Lines changed: 525 additions & 0 deletions
Large diffs are not rendered by default.

src/Parsem/Config/Options.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Matronator\Parsem\Config;
6+
7+
use ArrayObject;
8+
9+
class Options extends ArrayObject
10+
{
11+
public function __construct(
12+
public readonly bool $strict = false,
13+
public readonly PatternsOption $patterns = new PatternsOption(),
14+
public readonly ?bool $trimBeforeBlocks = false,
15+
public readonly ?bool $trimAfterBlocks = false,
16+
) { }
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Matronator\Parsem\Config;
6+
7+
use ArrayObject;
8+
9+
class PatternsOption extends ArrayObject
10+
{
11+
public function __construct(
12+
public ?string $variables = null,
13+
public ?string $conditions = null,
14+
public ?string $comments = null,
15+
) { }
16+
}

src/Parsem/Engine.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Matronator\Parsem;
6+
7+
use Matronator\Parsem\Config\Options;
8+
use Matronator\Parsem\Config\PatternsOption;
9+
10+
class Engine
11+
{
12+
private bool $strict = true;
13+
private bool $trimBeforeBlocks = false;
14+
private bool $trimAfterBlocks = false;
15+
private PatternsOption $patterns = new PatternsOption();
16+
17+
public function __construct(Options|array|null $options = new Options())
18+
{
19+
$this->setOptions($options);
20+
}
21+
22+
public function setOptions(Options|array|null $options = new Options()): void
23+
{
24+
if (!($options instanceof Options)) {
25+
if (is_array($options)) {
26+
$options = new Options(...$options);
27+
} else {
28+
throw new \InvalidArgumentException('Options must be an instance of Options class or an array.');
29+
}
30+
}
31+
32+
$this->strict = $options->strict ?? $this->strict;
33+
$this->patterns = $options->patterns ?? $this->patterns;
34+
$this->trimBeforeBlocks = $options->trimBeforeBlocks ?? $this->trimBeforeBlocks;
35+
$this->trimAfterBlocks = $options->trimAfterBlocks ?? $this->trimAfterBlocks;
36+
}
37+
38+
public function getOptions(): Options
39+
{
40+
return new Options(
41+
$this->strict,
42+
$this->patterns,
43+
$this->trimBeforeBlocks,
44+
$this->trimAfterBlocks,
45+
);
46+
}
47+
48+
public function parse(string $input, array $arguments = []): string
49+
{
50+
return Parser::parseString($input, $arguments, $this->strict, $this->patterns);
51+
}
52+
}

src/Parsem/Filters.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ class Filters
88
{
99
public const ENCODING = 'UTF-8';
1010

11-
public const GLOBAL_FILTERS = ['upper', 'lower', 'upperFirst', 'lowerFirst', 'first', 'last', 'camelCase', 'snakeCase', 'kebabCase', 'pascalCase', 'titleCase', 'length', 'reverse', 'random', 'truncate'];
11+
public const GLOBAL_FILTERS = [
12+
'upper', 'lower',
13+
'upperFirst', 'lowerFirst',
14+
'first', 'last',
15+
'camelCase', 'snakeCase', 'kebabCase', 'pascalCase', 'titleCase',
16+
'length',
17+
'reverse', 'random', 'shuffle',
18+
'truncate',
19+
'escape', 'unescape', 'hash'];
1220

1321
public static function upper(string $string): string
1422
{
@@ -78,30 +86,39 @@ public static function titleCase(string $string): string
7886
return $string;
7987
}
8088

81-
public static function length(string $string): int
89+
public static function length(array|string $value): int
8290
{
83-
return mb_strlen($string, static::ENCODING);
91+
return is_string($value) ? mb_strlen($value, static::ENCODING) : count($value);
8492
}
8593

86-
public static function reverse(string $string): string
94+
public static function reverse(array|string $value): array|string
8795
{
88-
return strrev($string);
96+
return is_string($value) ? strrev($value) : array_reverse($value);
8997
}
9098

91-
public static function random(array|string $array): mixed
99+
public static function random(array|string $value): array|string
92100
{
93-
if (is_string($array))
94-
return $array[rand(0, strlen($array) - 1)];
95-
96-
return $array[array_rand($array)];
101+
if (is_string($value)) {
102+
return $value[rand(0, strlen($value) - 1)];
103+
}
104+
105+
return $value[array_rand($value)];
106+
}
107+
108+
public static function shuffle(array|string $value): array|string
109+
{
110+
$array = is_string($value) ? str_split($value) : $value;
111+
shuffle($array);
112+
113+
return $value;
97114
}
98115

99116
public static function truncate(string $string, int $length, string $ending = '...'): string
100117
{
101118
if (mb_strlen($string, static::ENCODING) <= $length) {
102119
return $string;
103120
}
104-
121+
105122
return mb_substr($string, 0, $length, static::ENCODING) . $ending;
106123
}
107124
}

0 commit comments

Comments
 (0)