|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\CodeBlockChecker\Tests\Service\Validator; |
| 4 | + |
| 5 | +use Doctrine\RST\Configuration; |
| 6 | +use Doctrine\RST\Environment; |
| 7 | +use Doctrine\RST\Nodes\CodeNode; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Symfony\CodeBlockChecker\Issue\IssueCollection; |
| 10 | +use Symfony\CodeBlockChecker\Service\CodeValidator\PhpValidator; |
| 11 | +use Symfony\CodeBlockChecker\Service\CodeValidator\Validator; |
| 12 | + |
| 13 | +class PhpValidatorTest extends TestCase |
| 14 | +{ |
| 15 | + private Validator $validator; |
| 16 | + private Environment $environment; |
| 17 | + |
| 18 | + protected function setUp(): void |
| 19 | + { |
| 20 | + $this->environment = new Environment(new Configuration()); |
| 21 | + $this->validator = new PhpValidator(); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @dataProvider getCodeExamples |
| 26 | + */ |
| 27 | + public function testCodeExamples(int $errors, string $code) |
| 28 | + { |
| 29 | + $node = new CodeNode(explode(PHP_EOL, $code)); |
| 30 | + $node->setEnvironment($this->environment); |
| 31 | + $node->setLanguage('php'); |
| 32 | + $issues = new IssueCollection(); |
| 33 | + $this->validator->validate($node, $issues); |
| 34 | + $this->assertCount($errors, $issues); |
| 35 | + } |
| 36 | + |
| 37 | + public function getCodeExamples(): iterable |
| 38 | + { |
| 39 | + yield [0, ' |
| 40 | +namespace Symfony\Component\HttpKernel; |
| 41 | +
|
| 42 | +use Symfony\Component\HttpFoundation\Request; |
| 43 | +
|
| 44 | +interface HttpKernelInterface |
| 45 | +{ |
| 46 | + // ... |
| 47 | +
|
| 48 | + /** |
| 49 | + * @return Response A Response instance |
| 50 | + */ |
| 51 | + public function handle( |
| 52 | + Request $request, |
| 53 | + $type = self::MASTER_REQUEST, |
| 54 | + $catch = true |
| 55 | + ); |
| 56 | +} |
| 57 | +']; |
| 58 | + yield [0, ' |
| 59 | +public static function validate($object, ExecutionContextInterface $context, $payload) |
| 60 | +{ |
| 61 | + // somehow you have an array of "fake names" |
| 62 | + $fakeNames = [/* ... */]; |
| 63 | +
|
| 64 | + // check if the name is actually a fake name |
| 65 | + if (in_array($object->getFirstName(), $fakeNames)) { |
| 66 | + $context->buildViolation("This name sounds totally fake!") |
| 67 | + ->atPath("firstName") |
| 68 | + ->addViolation() |
| 69 | + ; |
| 70 | + } |
| 71 | +} |
| 72 | +']; |
| 73 | + } |
| 74 | +} |
0 commit comments