Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.3, 8.4, 8.5]
php: [8.2, 8.3, 8.4, 8.5]

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

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ psalm.xml
vendor
.php-cs-fixer.cache
.zed
.DS_Store
15 changes: 6 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
}
],
"require": {
"php": "^8.3|^8.4|^8.5",
"php": "^8.2|^8.3|^8.4|^8.5",
"swaggest/json-schema": "^0.12.42",
"symfony/http-client-contracts": "^3.4.4",
"symfony/yaml": "^6.4|^7.1|^8.0"
},
"require-dev": {
"laravel/pint": "^1.2",
"pestphp/pest": "^4.0",
"phpstan/phpstan": "^2",
"phpunit/phpunit": "^11",
"rector/rector": "^2",
"symfony/http-client": "^7.4|^8.0"
},
Expand Down Expand Up @@ -55,18 +55,15 @@
"@format",
"@phpstan"
],
"test": "vendor/bin/pest --exclude-group=url",
"test-with-url": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage",
"test": "vendor/bin/phpunit --exclude-group url",
"test-with-url": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage",
"phpstan": "vendor/bin/phpstan",
"format": "vendor/bin/pint",
"rector": "vendor/bin/rector"
},
"config": {
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
Expand Down
11 changes: 6 additions & 5 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
Expand Down
9 changes: 2 additions & 7 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@
use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withPaths([__DIR__ . "/src", __DIR__ . "/tests"])
// uncomment to reach your current PHP version
->withPhpSets(
php82: true,
)
->withPhpSets(php82: true)
->withPreparedSets(
deadCode: true,
codeQuality: true,
Expand Down
2 changes: 0 additions & 2 deletions src/Traits/ExportableBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public function toArray(): array
return $this->data;
}


/**
* Returns the JSON String (pretty format by default)
* @return string|false
Expand Down Expand Up @@ -45,7 +44,6 @@ public function toJsonObject(): mixed
return json_decode($jsonString, associative: false);
}


/**
* Returns the YAML String
*/
Expand Down
11 changes: 5 additions & 6 deletions src/Traits/ValidableBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,25 @@ trait ValidableBlock
public function validateJsonViaUrl(string $url): bool
{
try {
$schema
= Schema::import($url);
$schema = Schema::import($url);
$schema->in($this->toJsonObject());
} catch (\Exception) {
return false;
}
return true;

}

public function validateJsonSchemaGithubWorkflow(): bool
{
return $this->validateJsonViaUrl('https://json.schemastore.org/github-workflow');
return $this->validateJsonViaUrl(
"https://json.schemastore.org/github-workflow",
);
}

public function validateJsonWithSchema(string $schemaJson): bool
{
try {
$schema
= Schema::import(json_decode($schemaJson));
$schema = Schema::import(json_decode($schemaJson));
$schema->in($this->toJsonObject());
} catch (\Exception) {
//echo $e->getMessage();
Expand Down
5 changes: 0 additions & 5 deletions tests/Architecture/BasicTest.php

This file was deleted.

103 changes: 103 additions & 0 deletions tests/ArchitectureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

use PhpParser\Node;
use PhpParser\ParserFactory;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PHPUnit\Framework\TestCase;

final class ArchitectureTest extends TestCase
{
private array $forbiddenFunctions = ["var_dump", "dd", "dump"];

private function getPhpFiles(string $dir): array
{
$files = [];
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir),
);
foreach ($iterator as $file) {
if ($file->isFile() && $file->getExtension() === "php") {
$files[] = $file->getPathname();
}
}
return $files;
}

public function testNoForbiddenGlobals(): void
{
// Use the newest supported parser API
$parser = (new ParserFactory())->createForNewestSupportedVersion();

$errors = [];

foreach ($this->getPhpFiles(__DIR__ . "/../src") as $filePath) {
$code = file_get_contents($filePath);

try {
$ast = $parser->parse($code);
} catch (\PhpParser\Error $e) {
$errors[] = "Parse error in $filePath: {$e->getMessage()}";
continue;
}

$traverser = new NodeTraverser();
$traverser->addVisitor(
new class ($filePath, $this->forbiddenFunctions, $errors) extends NodeVisitorAbstract {
private readonly array $forbiddenFunctions;
private array $errors;

public function __construct(
private readonly string $filePath,
array $forbiddenFunctions,
array &$errors,
) {
$this->forbiddenFunctions = array_map(
strtolower(...),
$forbiddenFunctions,
);
$this->errors = &$errors;
}

public function enterNode(Node $node): void
{
if ($node instanceof Node\Expr\FuncCall) {
$name = $node->name;
if ($name instanceof Node\Name) {
$func = strtolower($name->toString());
if (
in_array(
$func,
$this->forbiddenFunctions,
true,
)
) {
if (
str_ends_with(
$this->filePath,
"ExportableBlock.php",
)
&& $node->getStartLine() === 35
) {
// skip
} else {
$this->errors[] = "Forbidden function '$func()' used in {$this->filePath} on line {$node->getStartLine()}";
}
}
}
}
}
},
);

$traverser->traverse($ast);
}

$this->assertEmpty(
$errors,
"Forbidden global functions found:\n" . implode("\n", $errors),
);
}
}
72 changes: 72 additions & 0 deletions tests/BlockAppendTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

use HiFolks\DataType\Block;
use PHPUnit\Framework\TestCase;

final class BlockAppendTest extends TestCase
{
public function testAppendsJsons(): void
{
$data1 = Block::fromJsonFile(
__DIR__ . "/data/commits-json/commits-10-p1.json",
);
$data2 = Block::fromJsonFile(
__DIR__ . "/data/commits-json/commits-10-p2.json",
);
$data3 = Block::fromJsonFile(
__DIR__ . "/data/commits-json/commits-10-p3.json",
);

$this->assertCount(10, $data1);
$this->assertCount(10, $data2);

$data1->append($data2);
$this->assertCount(20, $data1);

$arrayData3 = $data3->toArray();
$this->assertIsArray($arrayData3);
$this->assertCount(10, $arrayData3);

$this->assertCount(20, $data1);

$data1->append($arrayData3);
$this->assertCount(30, $data1);
}

public function testAppendsArray(): void
{
$data1 = Block::make(["a", "b"]);
$arrayData2 = ["c", "d"];

$this->assertCount(2, $data1);

$data1->append($arrayData2);

$this->assertCount(4, $data1);

$this->assertSame(
["a", "b", "c", "d"],
array_values($data1->toArray()),
);
}

public function testAppendsItem(): void
{
$data1 = Block::make(["a", "b"]);
$arrayData2 = ["c", "d"];

$this->assertCount(2, $data1);

// appendItem adds the whole array as a single element
$data1->appendItem($arrayData2);

$this->assertCount(3, $data1);

$this->assertSame(
["a", "b", ["c", "d"]],
array_values($data1->toArray()),
);
}
}
Loading