|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Tests\Functional\Standards\RepositoryPattern\Sniffs\Controllers; |
| 6 | + |
| 7 | +use PHP_CodeSniffer\Config; |
| 8 | +use PHP_CodeSniffer\Files\LocalFile; |
| 9 | +use PHP_CodeSniffer\Ruleset; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +class DisallowRepositoryUsageSniffTest extends TestCase |
| 13 | +{ |
| 14 | + private string $tempDir; |
| 15 | + private string $tempFilePath; |
| 16 | + |
| 17 | + protected function setUp(): void |
| 18 | + { |
| 19 | + parent::setUp(); |
| 20 | + |
| 21 | + $this->tempDir = sys_get_temp_dir().'/src/Controller'; |
| 22 | + if (!is_dir($this->tempDir)) { |
| 23 | + mkdir($this->tempDir, 0o777, true); |
| 24 | + } |
| 25 | + |
| 26 | + $this->tempFilePath = $this->tempDir.'/UserController.php'; |
| 27 | + $this->createTemporaryFile($this->tempFilePath); |
| 28 | + } |
| 29 | + |
| 30 | + protected function tearDown(): void |
| 31 | + { |
| 32 | + if (file_exists($this->tempFilePath)) { |
| 33 | + unlink($this->tempFilePath); |
| 34 | + } |
| 35 | + if (is_dir($this->tempDir)) { |
| 36 | + rmdir($this->tempDir); |
| 37 | + } |
| 38 | + if (is_dir(dirname($this->tempDir))) { |
| 39 | + rmdir(dirname($this->tempDir)); |
| 40 | + } |
| 41 | + |
| 42 | + parent::tearDown(); |
| 43 | + } |
| 44 | + |
| 45 | + public function testRepositoryUsageInControllerIsDetected(): void |
| 46 | + { |
| 47 | + $config = new Config(); |
| 48 | + |
| 49 | + $rulesetPath = dirname(__DIR__, 6).'/src/Standards/RepositoryPattern/ruleset.xml'; |
| 50 | + $config->standards = [$rulesetPath]; |
| 51 | + |
| 52 | + $ruleset = new Ruleset($config); |
| 53 | + |
| 54 | + $file = new LocalFile($this->tempFilePath, $ruleset, $config); |
| 55 | + $file->process(); |
| 56 | + |
| 57 | + $errors = $file->getErrors(); |
| 58 | + $this->assertNotEmpty($errors, 'Expected an error to be detected.'); |
| 59 | + |
| 60 | + $foundError = $this->containsErrorMessage( |
| 61 | + $errors |
| 62 | + ); |
| 63 | + |
| 64 | + $this->assertTrue($foundError, 'Expected error message not found.'); |
| 65 | + } |
| 66 | + |
| 67 | + // phpcs:disable RepositoryPattern.Controllers.DisallowRepositoryUsage |
| 68 | + private function createTemporaryFile(string $path): void |
| 69 | + { |
| 70 | + $content = <<<'PHP' |
| 71 | + <?php |
| 72 | +
|
| 73 | + declare(strict_types=1); |
| 74 | +
|
| 75 | + namespace App\Controller; |
| 76 | +
|
| 77 | + use App\Repository\UserRepository; |
| 78 | +
|
| 79 | + class UserController |
| 80 | + { |
| 81 | + private $userRepository; |
| 82 | +
|
| 83 | + public function __construct(UserRepository $userRepository) |
| 84 | + { |
| 85 | + $this->userRepository = $userRepository; |
| 86 | + } |
| 87 | +
|
| 88 | + public function index() |
| 89 | + { |
| 90 | + $users = $this->userRepository->findAll(); |
| 91 | + } |
| 92 | + } |
| 93 | + PHP; |
| 94 | + |
| 95 | + $tempFile = fopen($path, 'w'); |
| 96 | + if (false === $tempFile) { |
| 97 | + $this->fail('Failed to create temporary file.'); |
| 98 | + } |
| 99 | + |
| 100 | + fwrite($tempFile, $content); |
| 101 | + fclose($tempFile); |
| 102 | + } |
| 103 | + // phpcs:enable RepositoryPattern.Controllers.DisallowRepositoryUsage |
| 104 | + |
| 105 | + private function containsErrorMessage(array $errors): bool |
| 106 | + { |
| 107 | + return array_any( |
| 108 | + $errors, |
| 109 | + fn ($lineErrors) => array_any( |
| 110 | + $lineErrors, |
| 111 | + fn ($errorMessages) => array_any( |
| 112 | + $errorMessages, |
| 113 | + fn ($errorMessage) => str_contains($errorMessage['message'], 'Controllers must not use repositories directly') |
| 114 | + ) |
| 115 | + ) |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
0 commit comments