|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Neos.ContentGraph.DoctrineDbalAdapter package. |
| 5 | + * |
| 6 | + * (c) Contributors of the Neos Project - www.neos.io |
| 7 | + * |
| 8 | + * This package is Open Source Software. For the full copyright and license |
| 9 | + * information, please view the LICENSE file which was distributed with this |
| 10 | + * source code. |
| 11 | + */ |
| 12 | + |
| 13 | +declare(strict_types=1); |
| 14 | + |
| 15 | +namespace Neos\ContentGraph\DoctrineDbalAdapter\Tests\Behavior\Features\Bootstrap; |
| 16 | + |
| 17 | +use Behat\Gherkin\Node\PyStringNode; |
| 18 | +use Behat\Gherkin\Node\TableNode; |
| 19 | +use League\Flysystem\Filesystem; |
| 20 | +use League\Flysystem\InMemory\InMemoryFilesystemAdapter; |
| 21 | +use Neos\ContentGraph\DoctrineDbalAdapter\DoctrineDbalContentGraphProjectionFactory; |
| 22 | +use Neos\ContentRepository\Core\Factory\ContentRepositoryServiceFactoryDependencies; |
| 23 | +use Neos\ContentRepository\Core\Factory\ContentRepositoryServiceFactoryInterface; |
| 24 | +use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId; |
| 25 | +use Neos\ContentRepository\Export\Event\ValueObject\ExportedEvents; |
| 26 | +use Neos\ContentRepository\Export\ProcessorResult; |
| 27 | +use Neos\ContentRepository\Export\Processors\EventExportProcessor; |
| 28 | +use Neos\ContentRepository\Export\Processors\EventStoreImportProcessor; |
| 29 | +use Neos\ContentRepository\Export\Severity; |
| 30 | +use Neos\ContentRepository\TestSuite\Behavior\Features\Bootstrap\CRTestSuiteRuntimeVariables; |
| 31 | +use PHPUnit\Framework\Assert; |
| 32 | + |
| 33 | +/** |
| 34 | + * @todo move this class somewhere where its autoloaded |
| 35 | + */ |
| 36 | +trait CrImportExportTrait |
| 37 | +{ |
| 38 | + use CRTestSuiteRuntimeVariables; |
| 39 | + |
| 40 | + private Filesystem $crImportExportTrait_filesystem; |
| 41 | + |
| 42 | + private ?ProcessorResult $crImportExportTrait_lastMigrationResult = null; |
| 43 | + |
| 44 | + /** @var array<string> */ |
| 45 | + private array $crImportExportTrait_loggedErrors = []; |
| 46 | + |
| 47 | + /** @var array<string> */ |
| 48 | + private array $crImportExportTrait_loggedWarnings = []; |
| 49 | + |
| 50 | + public function setupCrImportExportTrait() |
| 51 | + { |
| 52 | + $this->crImportExportTrait_filesystem = new Filesystem(new InMemoryFilesystemAdapter()); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @When /^the events are exported$/ |
| 57 | + */ |
| 58 | + public function theEventsAreExportedIExpectTheFollowingJsonl() |
| 59 | + { |
| 60 | + $eventExporter = $this->getContentRepositoryService( |
| 61 | + new class ($this->crImportExportTrait_filesystem) implements ContentRepositoryServiceFactoryInterface { |
| 62 | + public function __construct(private readonly Filesystem $filesystem) |
| 63 | + { |
| 64 | + } |
| 65 | + public function build(ContentRepositoryServiceFactoryDependencies $serviceFactoryDependencies): EventExportProcessor { |
| 66 | + return new EventExportProcessor( |
| 67 | + $this->filesystem, |
| 68 | + $serviceFactoryDependencies->contentRepository->getWorkspaceFinder(), |
| 69 | + $serviceFactoryDependencies->eventStore |
| 70 | + ); |
| 71 | + } |
| 72 | + } |
| 73 | + ); |
| 74 | + assert($eventExporter instanceof EventExportProcessor); |
| 75 | + |
| 76 | + $eventExporter->onMessage(function (Severity $severity, string $message) { |
| 77 | + if ($severity === Severity::ERROR) { |
| 78 | + $this->crImportExportTrait_loggedErrors[] = $message; |
| 79 | + } elseif ($severity === Severity::WARNING) { |
| 80 | + $this->crImportExportTrait_loggedWarnings[] = $message; |
| 81 | + } |
| 82 | + }); |
| 83 | + $this->crImportExportTrait_lastMigrationResult = $eventExporter->run(); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @When /^I import the events\.jsonl(?: into "([^"]*)")?$/ |
| 88 | + */ |
| 89 | + public function iImportTheFollowingJson(?string $contentStreamId = null) |
| 90 | + { |
| 91 | + $eventImporter = $this->getContentRepositoryService( |
| 92 | + new class ($this->crImportExportTrait_filesystem, $contentStreamId ? ContentStreamId::fromString($contentStreamId) : null) implements ContentRepositoryServiceFactoryInterface { |
| 93 | + public function __construct( |
| 94 | + private readonly Filesystem $filesystem, |
| 95 | + private readonly ?ContentStreamId $contentStreamId |
| 96 | + ) { |
| 97 | + } |
| 98 | + public function build(ContentRepositoryServiceFactoryDependencies $serviceFactoryDependencies): EventStoreImportProcessor { |
| 99 | + return new EventStoreImportProcessor( |
| 100 | + false, |
| 101 | + $this->filesystem, |
| 102 | + $serviceFactoryDependencies->eventStore, |
| 103 | + $serviceFactoryDependencies->eventNormalizer, |
| 104 | + $this->contentStreamId |
| 105 | + ); |
| 106 | + } |
| 107 | + } |
| 108 | + ); |
| 109 | + assert($eventImporter instanceof EventStoreImportProcessor); |
| 110 | + |
| 111 | + $eventImporter->onMessage(function (Severity $severity, string $message) { |
| 112 | + if ($severity === Severity::ERROR) { |
| 113 | + $this->crImportExportTrait_loggedErrors[] = $message; |
| 114 | + } elseif ($severity === Severity::WARNING) { |
| 115 | + $this->crImportExportTrait_loggedWarnings[] = $message; |
| 116 | + } |
| 117 | + }); |
| 118 | + $this->crImportExportTrait_lastMigrationResult = $eventImporter->run(); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @Given /^using the following events\.jsonl:$/ |
| 123 | + */ |
| 124 | + public function usingTheFollowingEventsJsonl(PyStringNode $string) |
| 125 | + { |
| 126 | + $this->crImportExportTrait_filesystem->write('events.jsonl', $string->getRaw()); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @AfterScenario |
| 131 | + */ |
| 132 | + public function failIfLastMigrationHasErrors(): void |
| 133 | + { |
| 134 | + if ($this->crImportExportTrait_lastMigrationResult !== null && $this->crImportExportTrait_lastMigrationResult->severity === Severity::ERROR) { |
| 135 | + throw new \RuntimeException(sprintf('The last migration run led to an error: %s', $this->crImportExportTrait_lastMigrationResult->message)); |
| 136 | + } |
| 137 | + if ($this->crImportExportTrait_loggedErrors !== []) { |
| 138 | + throw new \RuntimeException(sprintf('The last migration run logged %d error%s', count($this->crImportExportTrait_loggedErrors), count($this->crImportExportTrait_loggedErrors) === 1 ? '' : 's')); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @Then I expect the following jsonl: |
| 144 | + */ |
| 145 | + public function iExpectTheFollowingJsonL(PyStringNode $string): void |
| 146 | + { |
| 147 | + if (!$this->crImportExportTrait_filesystem->has('events.jsonl')) { |
| 148 | + Assert::fail('No events were exported'); |
| 149 | + } |
| 150 | + |
| 151 | + $jsonL = $this->crImportExportTrait_filesystem->read('events.jsonl'); |
| 152 | + |
| 153 | + $exportedEvents = ExportedEvents::fromJsonl($jsonL); |
| 154 | + $eventsWithoutRandomIds = []; |
| 155 | + |
| 156 | + foreach ($exportedEvents as $exportedEvent) { |
| 157 | + // we have to remove the event id in \Neos\ContentRepository\Core\Feature\Common\NodeAggregateEventPublisher::enrichWithCommand |
| 158 | + // and the initiatingTimestamp to make the events diff able |
| 159 | + $eventsWithoutRandomIds[] = $exportedEvent |
| 160 | + ->withIdentifier('random-event-uuid') |
| 161 | + ->processMetadata(function (array $metadata) { |
| 162 | + $metadata['initiatingTimestamp'] = 'random-time'; |
| 163 | + return $metadata; |
| 164 | + }); |
| 165 | + } |
| 166 | + |
| 167 | + Assert::assertSame($string->getRaw(), ExportedEvents::fromIterable($eventsWithoutRandomIds)->toJsonl()); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @Then I expect the following errors to be logged |
| 172 | + */ |
| 173 | + public function iExpectTheFollowingErrorsToBeLogged(TableNode $table): void |
| 174 | + { |
| 175 | + Assert::assertSame($table->getColumn(0), $this->crImportExportTrait_loggedErrors, 'Expected logged errors do not match'); |
| 176 | + $this->crImportExportTrait_loggedErrors = []; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * @Then I expect the following warnings to be logged |
| 181 | + */ |
| 182 | + public function iExpectTheFollowingWarningsToBeLogged(TableNode $table): void |
| 183 | + { |
| 184 | + Assert::assertSame($table->getColumn(0), $this->crImportExportTrait_loggedWarnings, 'Expected logged warnings do not match'); |
| 185 | + $this->crImportExportTrait_loggedWarnings = []; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * @Then I expect a MigrationError |
| 190 | + * @Then I expect a MigrationError with the message |
| 191 | + */ |
| 192 | + public function iExpectAMigrationErrorWithTheMessage(PyStringNode $expectedMessage = null): void |
| 193 | + { |
| 194 | + Assert::assertNotNull($this->crImportExportTrait_lastMigrationResult, 'Expected the previous migration to contain errors, but no migration has been executed'); |
| 195 | + Assert::assertSame(Severity::ERROR, $this->crImportExportTrait_lastMigrationResult->severity, sprintf('Expected the previous migration to contain errors, but it ended with severity "%s"', $this->crImportExportTrait_lastMigrationResult->severity->name)); |
| 196 | + if ($expectedMessage !== null) { |
| 197 | + Assert::assertSame($expectedMessage->getRaw(), $this->crImportExportTrait_lastMigrationResult->message); |
| 198 | + } |
| 199 | + $this->crImportExportTrait_lastMigrationResult = null; |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * @template T of object |
| 204 | + * @param class-string<T> $className |
| 205 | + * |
| 206 | + * @return T |
| 207 | + */ |
| 208 | + abstract private function getObject(string $className): object; |
| 209 | + |
| 210 | + protected function getTableNamePrefix(): string |
| 211 | + { |
| 212 | + return DoctrineDbalContentGraphProjectionFactory::graphProjectionTableNamePrefix( |
| 213 | + $this->currentContentRepository->id |
| 214 | + ); |
| 215 | + } |
| 216 | +} |
0 commit comments