|
7 | 7 | use OpenSearch;
|
8 | 8 | use PDO;
|
9 | 9 | use stdClass;
|
| 10 | +use ReflectionMethod; |
10 | 11 | use Symfony\Component\Yaml\Yaml;
|
11 | 12 | use PHPUnit\Framework\Attributes\Before;
|
12 | 13 | use PHPUnit\Metadata\Annotation\Parser\Registry;
|
13 | 14 | use PHPUnit\Util\Test;
|
14 |
| - |
15 |
| -const AVAILABLE_MODES = ['read-only', 'write']; |
| 15 | +use PHPUnit\Event\Facade as EventFacade; |
16 | 16 |
|
17 | 17 | trait DbFixturesTrait
|
18 | 18 | {
|
@@ -40,44 +40,45 @@ abstract protected function getConnection(string $connectionName) : mixed;
|
40 | 40 | */
|
41 | 41 | #[Before]
|
42 | 42 | public function loadFixturesByAnnotations(): void {
|
43 |
| - if (method_exists(Test::class, 'parseTestMethodAnnotations')) { |
44 |
| - $annotations = $annotations = Test::parseTestMethodAnnotations( |
45 |
| - static::class, |
46 |
| - $this->getName(false) |
47 |
| - )['method'] ?? []; |
48 |
| - } else { |
49 |
| - $annotations = Registry::getInstance()->forMethod( |
50 |
| - static::class, |
51 |
| - $this->name() |
52 |
| - )->symbolAnnotations(); |
53 |
| - } |
| 43 | + $annotations = Registry::getInstance()->forMethod( |
| 44 | + static::class, |
| 45 | + $this->name() |
| 46 | + )->symbolAnnotations(); |
54 | 47 |
|
55 | 48 | $fixtures = [];
|
56 | 49 | foreach ($annotations['fixtures'] ?? [] as $fixture) {
|
57 |
| - [$connectionName, $mode, $args] = \explode(' ', $fixture, 3) + [null, null, null]; |
| 50 | + [$connectionName, $mode, $files] = \explode(' ', $fixture, 3) + [null, null, null]; |
58 | 51 |
|
59 |
| - if (!in_array($mode, AVAILABLE_MODES)) { |
60 |
| - throw new \UnexpectedValueException( |
61 |
| - sprintf('Wrong or missing mode of the fixture. Available modes [%s].', implode(', ', AVAILABLE_MODES)) |
62 |
| - ); |
| 52 | + $fixture = new Fixtures($connectionName, $mode, ...explode(' ', $files)); |
| 53 | + |
| 54 | + if (isset($fixtures[$connectionName])) { |
| 55 | + $fixture = $fixture->mergeWith($fixtures[$connectionName]); |
63 | 56 | }
|
64 | 57 |
|
65 |
| - if (array_key_exists($connectionName, $fixtures)) { |
66 |
| - [$newMode, $newArgs] = $fixtures[$connectionName]; |
67 |
| - $params = [$newMode, $newArgs.' '.$args]; |
68 |
| - } else { |
69 |
| - $params = [$mode, $args]; |
| 58 | + $fixtures[$connectionName] = $fixture; |
| 59 | + |
| 60 | + EventFacade::emitter()->testTriggeredPhpunitDeprecation( |
| 61 | + $this->valueObjectForEvents(), |
| 62 | + 'Annotation @fixtures is deprecated, use attribute Fixtures instead', |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + foreach ((new ReflectionMethod($this, $this->name()))->getAttributes(Fixtures::class) as $attribute) { |
| 67 | + $fixture = $attribute->newInstance(); |
| 68 | + |
| 69 | + if (isset($fixtures[$fixture->label])) { |
| 70 | + $fixture = $fixture->mergeWith($fixtures[$fixture->label]); |
70 | 71 | }
|
71 | 72 |
|
72 |
| - $fixtures[$connectionName] = $params; |
| 73 | + $fixtures[$fixture->label] = $fixture; |
73 | 74 | }
|
74 | 75 |
|
75 |
| - foreach ($fixtures as $connectionName => [$mode, $args]) { |
| 76 | + foreach ($fixtures as $connectionName => $fixture) { |
| 77 | + $mode = $fixture->mode; |
| 78 | + |
76 | 79 | $filenames = [];
|
77 |
| - if ($args) { |
78 |
| - foreach (\explode(' ', $args) as $filename) { |
79 |
| - $filenames[] = $this->resolveFilePath($connectionName, $filename); |
80 |
| - } |
| 80 | + foreach ($fixture->files as $filename) { |
| 81 | + $filenames[] = $this->resolveFilePath($connectionName, $filename); |
81 | 82 | }
|
82 | 83 |
|
83 | 84 | $cache = $this->getCache();
|
|
0 commit comments