Skip to content

Commit ff681f0

Browse files
Removing PestPHP, using phpunit 11 (#55)
* Removing PestPHP, using phpunit 11 * re-enabling php 8.2
1 parent 2448532 commit ff681f0

37 files changed

Lines changed: 1760 additions & 1537 deletions

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
fail-fast: true
1010
matrix:
1111
os: [ubuntu-latest, windows-latest]
12-
php: [8.3, 8.4, 8.5]
12+
php: [8.2, 8.3, 8.4, 8.5]
1313

1414
name: P${{ matrix.php }} - ${{ matrix.os }}
1515

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ psalm.xml
1212
vendor
1313
.php-cs-fixer.cache
1414
.zed
15+
.DS_Store

composer.json

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
}
1919
],
2020
"require": {
21-
"php": "^8.3|^8.4|^8.5",
21+
"php": "^8.2|^8.3|^8.4|^8.5",
2222
"swaggest/json-schema": "^0.12.42",
2323
"symfony/http-client-contracts": "^3.4.4",
2424
"symfony/yaml": "^6.4|^7.1|^8.0"
2525
},
2626
"require-dev": {
2727
"laravel/pint": "^1.2",
28-
"pestphp/pest": "^4.0",
2928
"phpstan/phpstan": "^2",
29+
"phpunit/phpunit": "^11",
3030
"rector/rector": "^2",
3131
"symfony/http-client": "^7.4|^8.0"
3232
},
@@ -55,18 +55,15 @@
5555
"@format",
5656
"@phpstan"
5757
],
58-
"test": "vendor/bin/pest --exclude-group=url",
59-
"test-with-url": "vendor/bin/pest",
60-
"test-coverage": "vendor/bin/pest --coverage",
58+
"test": "vendor/bin/phpunit --exclude-group url",
59+
"test-with-url": "vendor/bin/phpunit",
60+
"test-coverage": "vendor/bin/phpunit --coverage",
6161
"phpstan": "vendor/bin/phpstan",
6262
"format": "vendor/bin/pint",
6363
"rector": "vendor/bin/rector"
6464
},
6565
"config": {
66-
"sort-packages": true,
67-
"allow-plugins": {
68-
"pestphp/pest-plugin": true
69-
}
66+
"sort-packages": true
7067
},
7168
"minimum-stability": "dev",
7269
"prefer-stable": true

phpunit.xml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
4-
bootstrap="vendor/autoload.php"
5-
colors="true"
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
5+
bootstrap="vendor/autoload.php"
6+
colors="true"
67
>
78
<testsuites>
89
<testsuite name="Test Suite">

rector.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@
55
use Rector\Config\RectorConfig;
66

77
return RectorConfig::configure()
8-
->withPaths([
9-
__DIR__ . '/src',
10-
__DIR__ . '/tests',
11-
])
8+
->withPaths([__DIR__ . "/src", __DIR__ . "/tests"])
129
// uncomment to reach your current PHP version
13-
->withPhpSets(
14-
php82: true,
15-
)
10+
->withPhpSets(php82: true)
1611
->withPreparedSets(
1712
deadCode: true,
1813
codeQuality: true,

src/Traits/ExportableBlock.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public function toArray(): array
1717
return $this->data;
1818
}
1919

20-
2120
/**
2221
* Returns the JSON String (pretty format by default)
2322
* @return string|false
@@ -45,7 +44,6 @@ public function toJsonObject(): mixed
4544
return json_decode($jsonString, associative: false);
4645
}
4746

48-
4947
/**
5048
* Returns the YAML String
5149
*/

src/Traits/ValidableBlock.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,25 @@ trait ValidableBlock
1111
public function validateJsonViaUrl(string $url): bool
1212
{
1313
try {
14-
$schema
15-
= Schema::import($url);
14+
$schema = Schema::import($url);
1615
$schema->in($this->toJsonObject());
1716
} catch (\Exception) {
1817
return false;
1918
}
2019
return true;
21-
2220
}
2321

2422
public function validateJsonSchemaGithubWorkflow(): bool
2523
{
26-
return $this->validateJsonViaUrl('https://json.schemastore.org/github-workflow');
24+
return $this->validateJsonViaUrl(
25+
"https://json.schemastore.org/github-workflow",
26+
);
2727
}
2828

2929
public function validateJsonWithSchema(string $schemaJson): bool
3030
{
3131
try {
32-
$schema
33-
= Schema::import(json_decode($schemaJson));
32+
$schema = Schema::import(json_decode($schemaJson));
3433
$schema->in($this->toJsonObject());
3534
} catch (\Exception) {
3635
//echo $e->getMessage();

tests/Architecture/BasicTest.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/ArchitectureTest.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpParser\Node;
6+
use PhpParser\ParserFactory;
7+
use PhpParser\NodeTraverser;
8+
use PhpParser\NodeVisitorAbstract;
9+
use PHPUnit\Framework\TestCase;
10+
11+
final class ArchitectureTest extends TestCase
12+
{
13+
private array $forbiddenFunctions = ["var_dump", "dd", "dump"];
14+
15+
private function getPhpFiles(string $dir): array
16+
{
17+
$files = [];
18+
$iterator = new RecursiveIteratorIterator(
19+
new RecursiveDirectoryIterator($dir),
20+
);
21+
foreach ($iterator as $file) {
22+
if ($file->isFile() && $file->getExtension() === "php") {
23+
$files[] = $file->getPathname();
24+
}
25+
}
26+
return $files;
27+
}
28+
29+
public function testNoForbiddenGlobals(): void
30+
{
31+
// Use the newest supported parser API
32+
$parser = (new ParserFactory())->createForNewestSupportedVersion();
33+
34+
$errors = [];
35+
36+
foreach ($this->getPhpFiles(__DIR__ . "/../src") as $filePath) {
37+
$code = file_get_contents($filePath);
38+
39+
try {
40+
$ast = $parser->parse($code);
41+
} catch (\PhpParser\Error $e) {
42+
$errors[] = "Parse error in $filePath: {$e->getMessage()}";
43+
continue;
44+
}
45+
46+
$traverser = new NodeTraverser();
47+
$traverser->addVisitor(
48+
new class ($filePath, $this->forbiddenFunctions, $errors) extends NodeVisitorAbstract {
49+
private readonly array $forbiddenFunctions;
50+
private array $errors;
51+
52+
public function __construct(
53+
private readonly string $filePath,
54+
array $forbiddenFunctions,
55+
array &$errors,
56+
) {
57+
$this->forbiddenFunctions = array_map(
58+
strtolower(...),
59+
$forbiddenFunctions,
60+
);
61+
$this->errors = &$errors;
62+
}
63+
64+
public function enterNode(Node $node): void
65+
{
66+
if ($node instanceof Node\Expr\FuncCall) {
67+
$name = $node->name;
68+
if ($name instanceof Node\Name) {
69+
$func = strtolower($name->toString());
70+
if (
71+
in_array(
72+
$func,
73+
$this->forbiddenFunctions,
74+
true,
75+
)
76+
) {
77+
if (
78+
str_ends_with(
79+
$this->filePath,
80+
"ExportableBlock.php",
81+
)
82+
&& $node->getStartLine() === 35
83+
) {
84+
// skip
85+
} else {
86+
$this->errors[] = "Forbidden function '$func()' used in {$this->filePath} on line {$node->getStartLine()}";
87+
}
88+
}
89+
}
90+
}
91+
}
92+
},
93+
);
94+
95+
$traverser->traverse($ast);
96+
}
97+
98+
$this->assertEmpty(
99+
$errors,
100+
"Forbidden global functions found:\n" . implode("\n", $errors),
101+
);
102+
}
103+
}

tests/BlockAppendTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use HiFolks\DataType\Block;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class BlockAppendTest extends TestCase
9+
{
10+
public function testAppendsJsons(): void
11+
{
12+
$data1 = Block::fromJsonFile(
13+
__DIR__ . "/data/commits-json/commits-10-p1.json",
14+
);
15+
$data2 = Block::fromJsonFile(
16+
__DIR__ . "/data/commits-json/commits-10-p2.json",
17+
);
18+
$data3 = Block::fromJsonFile(
19+
__DIR__ . "/data/commits-json/commits-10-p3.json",
20+
);
21+
22+
$this->assertCount(10, $data1);
23+
$this->assertCount(10, $data2);
24+
25+
$data1->append($data2);
26+
$this->assertCount(20, $data1);
27+
28+
$arrayData3 = $data3->toArray();
29+
$this->assertIsArray($arrayData3);
30+
$this->assertCount(10, $arrayData3);
31+
32+
$this->assertCount(20, $data1);
33+
34+
$data1->append($arrayData3);
35+
$this->assertCount(30, $data1);
36+
}
37+
38+
public function testAppendsArray(): void
39+
{
40+
$data1 = Block::make(["a", "b"]);
41+
$arrayData2 = ["c", "d"];
42+
43+
$this->assertCount(2, $data1);
44+
45+
$data1->append($arrayData2);
46+
47+
$this->assertCount(4, $data1);
48+
49+
$this->assertSame(
50+
["a", "b", "c", "d"],
51+
array_values($data1->toArray()),
52+
);
53+
}
54+
55+
public function testAppendsItem(): void
56+
{
57+
$data1 = Block::make(["a", "b"]);
58+
$arrayData2 = ["c", "d"];
59+
60+
$this->assertCount(2, $data1);
61+
62+
// appendItem adds the whole array as a single element
63+
$data1->appendItem($arrayData2);
64+
65+
$this->assertCount(3, $data1);
66+
67+
$this->assertSame(
68+
["a", "b", ["c", "d"]],
69+
array_values($data1->toArray()),
70+
);
71+
}
72+
}

0 commit comments

Comments
 (0)