Skip to content

Commit

Permalink
Added failling test
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jan 18, 2024
1 parent 8342785 commit 00a20d4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
63 changes: 63 additions & 0 deletions tests/PHPStan/Collectors/JsonSerializableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types = 1);

namespace PHPStan\Collectors;

use PhpParser\Node;
use PhpParser\Node\Stmt\If_;
use PHPStan\Node\CollectedDataNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\UniversalRule;
use PHPStan\Testing\RuleTestCase;

/**
* @extends RuleTestCase<UniversalRule<CollectedDataNode>>
*/
class JsonSerializableTest extends RuleTestCase
{

/** @var Collector<If_, array<mixed>> */
private Collector $collector;

public function getRule(): Rule
{
return new UniversalRule(CollectedDataNode::class, static fn (Node $node): array => []);
}

public function testNotDecodable(): void
{
$this->collector = new UniversalCollector(
If_::class,
static fn (If_ $node): array => [$node],
);

$this->analyse([__DIR__ . '/data/json-serializable.php'], [
[
'Data collected by PHPStan\Collectors\UniversalCollector is not JSON decodable and will not work in parallel analysis.',
-1,
],
]);
}

public function testNotJsonSerializable(): void
{
$this->collector = new UniversalCollector(
If_::class,
static fn (If_ $node): array => ["\x61\xb0\x62"], // a invalid utf8 string
);

$this->analyse([__DIR__ . '/data/json-serializable.php'], [
[
'Data collected by PHPStan\Collectors\UniversalCollector is not JSON serializable.',
-1,
],
]);
}

public function getCollectors(): array
{
return [
$this->collector,
];
}

}
5 changes: 5 additions & 0 deletions tests/PHPStan/Collectors/data/json-serializable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace CollectorJsonSerializable;

if (rand(0,1)) {}

0 comments on commit 00a20d4

Please sign in to comment.