From c7f9d8cc1a0bf86ee434449b6b001a19df116636 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Fri, 17 Mar 2023 22:25:22 +0100 Subject: [PATCH] Fix tests usage of generic interfaces --- .../Model/DimensionContentCollection.php | 3 +- .../Sulu/Preview/ContentObjectProvider.php | 2 +- .../PreviewDimensionContentCollection.php | 2 +- .../Route/ContentRouteDefaultsProvider.php | 7 +- .../Sulu/Structure/ContentStructureBridge.php | 1 + .../ExampleTestBundle/Entity/Example.php | 3 + .../DataMapper/RoutableDataMapperTest.php | 19 +- .../DataMapper/TemplateDataMapperTest.php | 19 +- .../ContentMerger/ContentMergerTest.php | 2 +- .../ContentNormalizerTest.php | 16 +- .../ContentWorkflow/ContentWorkflowTest.php | 15 +- .../DimensionContentCollectionFactoryTest.php | 4 +- .../Model/ContentRichEntityTraitTest.php | 31 ++- .../Model/DimensionContentCollectionTest.php | 23 +-- .../Model/DimensionContentTraitTest.php | 8 +- .../Domain/Model/RoutableTraitTest.php | 31 ++- .../Admin/ContentViewBuilderFactoryTest.php | 19 +- .../ContentPublishTaskHandlerTest.php | 23 ++- .../ContentUnpublishTaskHandlerTest.php | 23 ++- .../Preview/ContentObjectProviderTest.php | 53 ++--- .../PreviewDimensionContentCollectionTest.php | 39 ++-- .../ContentRouteDefaultsProviderTest.php | 101 +++++----- .../DataItem/ContentDataItemTest.php | 190 ++++++++---------- Tests/phpstan/stubs/ObjectProphecy.phpstub | 30 +++ Tests/phpstan/stubs/ProphecyInterface.phpstub | 27 +++ phpstan.neon | 1 + 26 files changed, 388 insertions(+), 304 deletions(-) create mode 100644 Tests/phpstan/stubs/ObjectProphecy.phpstub create mode 100644 Tests/phpstan/stubs/ProphecyInterface.phpstub diff --git a/Content/Domain/Model/DimensionContentCollection.php b/Content/Domain/Model/DimensionContentCollection.php index b609a99e..933b4255 100644 --- a/Content/Domain/Model/DimensionContentCollection.php +++ b/Content/Domain/Model/DimensionContentCollection.php @@ -17,7 +17,7 @@ use Doctrine\Common\Collections\Criteria; /** - * @template T of DimensionContentInterface + * @template-covariant T of DimensionContentInterface * * @implements \IteratorAggregate * @implements DimensionContentCollectionInterface @@ -91,6 +91,7 @@ public function getDimensionAttributes(): array return $this->dimensionAttributes; } + #[\ReturnTypeWillChange] public function getIterator() { return $this->dimensionContents; diff --git a/Content/Infrastructure/Sulu/Preview/ContentObjectProvider.php b/Content/Infrastructure/Sulu/Preview/ContentObjectProvider.php index 826cc888..a9c97600 100644 --- a/Content/Infrastructure/Sulu/Preview/ContentObjectProvider.php +++ b/Content/Infrastructure/Sulu/Preview/ContentObjectProvider.php @@ -149,7 +149,7 @@ public function serialize($object) /** * @param string $serializedObject - * @param string $objectClass + * @param class-string $objectClass * * @return B|null */ diff --git a/Content/Infrastructure/Sulu/Preview/PreviewDimensionContentCollection.php b/Content/Infrastructure/Sulu/Preview/PreviewDimensionContentCollection.php index 2594f56b..54cc12be 100644 --- a/Content/Infrastructure/Sulu/Preview/PreviewDimensionContentCollection.php +++ b/Content/Infrastructure/Sulu/Preview/PreviewDimensionContentCollection.php @@ -66,7 +66,7 @@ public function getDimensionAttributes(): array ); } - public function getIterator() + public function getIterator(): \Traversable { return new ArrayCollection([$this->previewDimensionContent]); } diff --git a/Content/Infrastructure/Sulu/Route/ContentRouteDefaultsProvider.php b/Content/Infrastructure/Sulu/Route/ContentRouteDefaultsProvider.php index 7fc8206e..1330796f 100644 --- a/Content/Infrastructure/Sulu/Route/ContentRouteDefaultsProvider.php +++ b/Content/Infrastructure/Sulu/Route/ContentRouteDefaultsProvider.php @@ -61,12 +61,13 @@ public function __construct( } /** - * @template T of DimensionContentInterface + * @template B of DimensionContentInterface + * @template T of ContentRichEntityInterface * - * @param class-string> $entityClass + * @param class-string $entityClass * @param string $id * @param string $locale - * @param T|null $object + * @param B|null $object * * @return mixed[] */ diff --git a/Content/Infrastructure/Sulu/Structure/ContentStructureBridge.php b/Content/Infrastructure/Sulu/Structure/ContentStructureBridge.php index 59d6b08a..acfad6ab 100644 --- a/Content/Infrastructure/Sulu/Structure/ContentStructureBridge.php +++ b/Content/Infrastructure/Sulu/Structure/ContentStructureBridge.php @@ -327,6 +327,7 @@ public function toArray($complete = true): array /** * @return mixed[] */ + #[\ReturnTypeWillChange] public function jsonSerialize() { return $this->toArray(true); diff --git a/Tests/Application/ExampleTestBundle/Entity/Example.php b/Tests/Application/ExampleTestBundle/Entity/Example.php index 5b3a7f75..736ec984 100644 --- a/Tests/Application/ExampleTestBundle/Entity/Example.php +++ b/Tests/Application/ExampleTestBundle/Entity/Example.php @@ -43,6 +43,9 @@ public function getId() return $this->id; } + /** + * @return ExampleDimensionContent + */ public function createDimensionContent(): DimensionContentInterface { $exampleDimensionContent = new ExampleDimensionContent($this); diff --git a/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapperTest.php b/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapperTest.php index 5897a77c..f9f48e31 100644 --- a/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapperTest.php +++ b/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapperTest.php @@ -66,7 +66,9 @@ protected function createRouteDataMapperInstance( } /** - * @param ObjectProphecy $routableMock + * @template T of DimensionContentInterface&TemplateInterface&RoutableInterface + * + * @param ObjectProphecy $routableMock */ protected function wrapRoutableMock(ObjectProphecy $routableMock): RoutableInterface { @@ -75,7 +77,7 @@ protected function wrapRoutableMock(ObjectProphecy $routableMock): RoutableInter TemplateInterface, RoutableInterface { /** - * @var DimensionContentInterface&TemplateInterface&RoutableInterface + * @var T */ protected $instance; @@ -209,7 +211,6 @@ public function testMapNoTemplate(): void $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent->willImplement(RoutableInterface::class); - /** @var ObjectProphecy $localizedDimensionContent */ $localizedDimensionContent->willImplement(TemplateInterface::class); $dimensionContentCollection = $this->prophesize(DimensionContentCollectionInterface::class); @@ -252,7 +253,6 @@ public function testMapNoMetadata(): void $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent->willImplement(RoutableInterface::class); - /** @var ObjectProphecy $localizedDimensionContent */ $localizedDimensionContent->willImplement(TemplateInterface::class); $dimensionContentCollection = $this->prophesize(DimensionContentCollectionInterface::class); @@ -295,7 +295,6 @@ public function testMapNoRouteProperty(): void $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent->willImplement(RoutableInterface::class); - /** @var ObjectProphecy $localizedDimensionContent */ $localizedDimensionContent->willImplement(TemplateInterface::class); $dimensionContentCollection = $this->prophesize(DimensionContentCollectionInterface::class); @@ -347,7 +346,6 @@ public function testMapNoRoutePropertyData(): void $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent->willImplement(RoutableInterface::class); - /** @var ObjectProphecy $localizedDimensionContent */ $localizedDimensionContent->willImplement(TemplateInterface::class); $dimensionContentCollection = $this->prophesize(DimensionContentCollectionInterface::class); @@ -400,7 +398,6 @@ public function testMapNoRoutePropertyDataAndNoOldRoute(): void $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent->willImplement(RoutableInterface::class); - /** @var ObjectProphecy $localizedDimensionContent */ $localizedDimensionContent->willImplement(TemplateInterface::class); $localizedDimensionContentMock = $this->wrapRoutableMock($localizedDimensionContent); @@ -469,7 +466,6 @@ public function testMapNoRoutePropertyDataAndNoOldRouteIgnoreSlash(): void $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent->willImplement(RoutableInterface::class); - /** @var ObjectProphecy $localizedDimensionContent */ $localizedDimensionContent->willImplement(TemplateInterface::class); $localizedDimensionContentMock = $this->wrapRoutableMock($localizedDimensionContent); @@ -530,7 +526,6 @@ public function testMapNoContentId(): void $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent->willImplement(RoutableInterface::class); - /** @var ObjectProphecy $localizedDimensionContent */ $localizedDimensionContent->willImplement(TemplateInterface::class); $dimensionContentCollection = $this->prophesize(DimensionContentCollectionInterface::class); @@ -578,7 +573,6 @@ public function testMapNoLocale(): void $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent->willImplement(RoutableInterface::class); - /** @var ObjectProphecy $localizedDimensionContent */ $localizedDimensionContent->willImplement(TemplateInterface::class); $dimensionContentCollection = $this->prophesize(DimensionContentCollectionInterface::class); @@ -628,7 +622,6 @@ public function testMapNoRoutePath(): void $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent->willImplement(RoutableInterface::class); - /** @var ObjectProphecy $localizedDimensionContent */ $localizedDimensionContent->willImplement(TemplateInterface::class); $localizedDimensionContentMock = $this->wrapRoutableMock($localizedDimensionContent); @@ -697,7 +690,6 @@ public function testMap(): void $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent->willImplement(RoutableInterface::class); - /** @var ObjectProphecy $localizedDimensionContent */ $localizedDimensionContent->willImplement(TemplateInterface::class); $dimensionContentCollection = $this->prophesize(DimensionContentCollectionInterface::class); @@ -759,7 +751,6 @@ public function testMapConflictingRoute(): void $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent->willImplement(RoutableInterface::class); - /** @var ObjectProphecy $localizedDimensionContent */ $localizedDimensionContent->willImplement(TemplateInterface::class); $dimensionContentCollection = $this->prophesize(DimensionContentCollectionInterface::class); @@ -821,7 +812,6 @@ public function testMapNoTemplateWithDefaultTemplate(): void $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent->willImplement(RoutableInterface::class); - /** @var ObjectProphecy $localizedDimensionContent */ $localizedDimensionContent->willImplement(TemplateInterface::class); $dimensionContentCollection = $this->prophesize(DimensionContentCollectionInterface::class); @@ -884,7 +874,6 @@ public function testMapCustomRoute(): void $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent->willImplement(RoutableInterface::class); - /** @var ObjectProphecy $localizedDimensionContent */ $localizedDimensionContent->willImplement(TemplateInterface::class); $localizedDimensionContentMock = $this->wrapRoutableMock($localizedDimensionContent); diff --git a/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/TemplateDataMapperTest.php b/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/TemplateDataMapperTest.php index e7cadeb2..3424011f 100644 --- a/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/TemplateDataMapperTest.php +++ b/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/TemplateDataMapperTest.php @@ -41,7 +41,9 @@ protected function createTemplateDataMapperInstance( } /** - * @param ObjectProphecy $templateMock + * @template T of DimensionContentInterface&TemplateInterface + * + * @param ObjectProphecy $templateMock */ protected function wrapTemplateMock(ObjectProphecy $templateMock): TemplateInterface { @@ -50,6 +52,11 @@ protected function wrapTemplateMock(ObjectProphecy $templateMock): TemplateInter TemplateInterface { use DimensionContentMockWrapperTrait; use TemplateMockWrapperTrait; + + /** + * @var T + */ + protected $instance; }; } @@ -88,7 +95,6 @@ public function testMapLocalizedNoTemplateKey(): void $structureMetadataFactory = $this->prophesize(StructureMetadataFactoryInterface::class); $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); - /** @var ObjectProphecy $unlocalizedDimensionContent */ $unlocalizedDimensionContent->willImplement(TemplateInterface::class); $localizedDimensionContent = $this->prophesize(DimensionContentInterface::class); @@ -130,7 +136,6 @@ public function testMapLocalizedNoTemplateInstance(): void )->willReturn($structureMetadata->reveal())->shouldBeCalled(); $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); - /** @var ObjectProphecy $unlocalizedDimensionContent */ $unlocalizedDimensionContent->willImplement(TemplateInterface::class); $dimensionContentCollection = $this->prophesize(DimensionContentCollectionInterface::class); @@ -161,7 +166,6 @@ public function testMapLocalizedNoStructureFound(): void )->willReturn(null)->shouldBeCalled(); $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); - /** @var ObjectProphecy $unlocalizedDimensionContent */ $unlocalizedDimensionContent->willImplement(TemplateInterface::class); $localizedDimensionContent = $this->prophesize(DimensionContentInterface::class); @@ -186,7 +190,6 @@ public function testMapUnlocalizedTemplate(): void ]; $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); - /** @var ObjectProphecy $unlocalizedDimensionContent */ $unlocalizedDimensionContent->willImplement(TemplateInterface::class); $unlocalizedDimensionContent->getTemplateData()->willReturn([])->shouldBeCalled(); $unlocalizedDimensionContent->setTemplateKey('template-key')->shouldBeCalled(); @@ -228,14 +231,12 @@ public function testMapLocalizedTemplate(): void ]; $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); - /** @var ObjectProphecy $unlocalizedDimensionContent */ $unlocalizedDimensionContent->willImplement(TemplateInterface::class); $unlocalizedDimensionContent->getTemplateData()->willReturn([])->shouldBeCalled(); $unlocalizedDimensionContent->setTemplateData(['unlocalizedField' => 'Test Unlocalized'])->shouldBeCalled(); $localizedDimensionContent = $this->prophesize(DimensionContentInterface::class); $localizedDimensionContent->willImplement(TemplateInterface::class); - /** @var ObjectProphecy $localizedDimensionContent */ $localizedDimensionContent->setTemplateKey('template-key')->shouldBeCalled(); $localizedDimensionContent->setTemplateData(['localizedField' => 'Test Localized'])->shouldBeCalled(); @@ -278,13 +279,11 @@ public function testMapLocalizedNoTemplateKeyWithDefaultTemplate(): void ]; $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); - /** @var ObjectProphecy $unlocalizedDimensionContent */ $unlocalizedDimensionContent->willImplement(TemplateInterface::class); $unlocalizedDimensionContent->getTemplateData()->willReturn([])->shouldBeCalled(); $unlocalizedDimensionContent->setTemplateData(['unlocalizedField' => 'Test Unlocalized'])->shouldBeCalled(); $localizedDimensionContent = $this->prophesize(DimensionContentInterface::class); - /** @var ObjectProphecy $localizedDimensionContent */ $localizedDimensionContent->willImplement(TemplateInterface::class); $localizedDimensionContent->setTemplateKey('template-key')->shouldBeCalled(); $localizedDimensionContent->setTemplateData(['localizedField' => 'Test Localized'])->shouldBeCalled(); @@ -332,13 +331,11 @@ public function testMapFloatValueTemplate(): void ]; $unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class); - /** @var ObjectProphecy $unlocalizedDimensionContent */ $unlocalizedDimensionContent->willImplement(TemplateInterface::class); $unlocalizedDimensionContent->getTemplateData()->willReturn([])->shouldBeCalled(); $unlocalizedDimensionContent->setTemplateData(['1.1' => 'Test Unlocalized'])->shouldBeCalled(); $localizedDimensionContent = $this->prophesize(DimensionContentInterface::class); - /** @var ObjectProphecy $localizedDimensionContent */ $localizedDimensionContent->willImplement(TemplateInterface::class); $localizedDimensionContent->setTemplateKey('template-key')->shouldBeCalled(); $localizedDimensionContent->setTemplateData(['localizedField' => 'Test Localized'])->shouldBeCalled(); diff --git a/Tests/Unit/Content/Application/ContentMerger/ContentMergerTest.php b/Tests/Unit/Content/Application/ContentMerger/ContentMergerTest.php index 58828b7b..d82f6edb 100644 --- a/Tests/Unit/Content/Application/ContentMerger/ContentMergerTest.php +++ b/Tests/Unit/Content/Application/ContentMerger/ContentMergerTest.php @@ -46,7 +46,7 @@ public function testMerge(): void $merger2->reveal(), ]); - $mergedDimensionContent = $this->prophesize(DimensionContentInterface::class); + $mergedDimensionContent = $this->prophesize(ExampleDimensionContent::class); $resource = $this->prophesize(Example::class); $resource->createDimensionContent() diff --git a/Tests/Unit/Content/Application/ContentNormalizer/ContentNormalizerTest.php b/Tests/Unit/Content/Application/ContentNormalizer/ContentNormalizerTest.php index 3b7ea312..ce2527fb 100644 --- a/Tests/Unit/Content/Application/ContentNormalizer/ContentNormalizerTest.php +++ b/Tests/Unit/Content/Application/ContentNormalizer/ContentNormalizerTest.php @@ -61,10 +61,13 @@ public function testResolveSimple(): void use DimensionContentTrait; /** - * @var ContentRichEntityInterface + * @var ContentRichEntityInterface */ protected $resource; + /** + * @param ContentRichEntityInterface $resource + */ public function __construct(ContentRichEntityInterface $resource) { $this->resource = $resource; @@ -77,6 +80,9 @@ public static function getResourceKey(): string throw new \RuntimeException('Should not be called while executing tests.'); } + /** + * @return ContentRichEntityInterface + */ public function getResource(): ContentRichEntityInterface { return $this->resource; @@ -105,10 +111,13 @@ public function testResolveFull(): void use WorkflowTrait; /** - * @var ContentRichEntityInterface + * @var ContentRichEntityInterface */ protected $resource; + /** + * @param ContentRichEntityInterface $resource + */ public function __construct(ContentRichEntityInterface $resource) { $this->resource = $resource; @@ -126,6 +135,9 @@ public static function getTemplateType(): string throw new \RuntimeException('Should not be called while executing tests.'); } + /** + * @return ContentRichEntityInterface + */ public function getResource(): ContentRichEntityInterface { return $this->resource; diff --git a/Tests/Unit/Content/Application/ContentWorkflow/ContentWorkflowTest.php b/Tests/Unit/Content/Application/ContentWorkflow/ContentWorkflowTest.php index f22b9b0a..99c9aa30 100644 --- a/Tests/Unit/Content/Application/ContentWorkflow/ContentWorkflowTest.php +++ b/Tests/Unit/Content/Application/ContentWorkflow/ContentWorkflowTest.php @@ -47,18 +47,25 @@ protected function createContentWorkflowInstance( } /** - * @param ObjectProphecy $workflowMock + * @template T of DimensionContentInterface&WorkflowInterface * - * @return DimensionContentInterface&WorkflowInterface + * @param ObjectProphecy $templateMock + * + * @return T */ - protected function wrapWorkflowMock(ObjectProphecy $workflowMock) + protected function wrapWorkflowMock(ObjectProphecy $templateMock): WorkflowInterface { - return new class($workflowMock) extends MockWrapper implements + return new class($templateMock) extends MockWrapper implements // @phpstan-ignore-line DimensionContentInterface, WorkflowInterface { use DimensionContentMockWrapperTrait; use WorkflowMockWrapperTrait; + /** + * @var T + */ + protected $instance; + public static function getWorkflowName(): string { return 'content_workflow'; diff --git a/Tests/Unit/Content/Application/DimensionContentCollectionFactory/DimensionContentCollectionFactoryTest.php b/Tests/Unit/Content/Application/DimensionContentCollectionFactory/DimensionContentCollectionFactoryTest.php index d6557691..a189005d 100644 --- a/Tests/Unit/Content/Application/DimensionContentCollectionFactory/DimensionContentCollectionFactoryTest.php +++ b/Tests/Unit/Content/Application/DimensionContentCollectionFactory/DimensionContentCollectionFactoryTest.php @@ -30,8 +30,10 @@ class DimensionContentCollectionFactoryTest extends TestCase use \Prophecy\PhpUnit\ProphecyTrait; /** + * @template T of DimensionContentInterface + * * @param mixed[] $dimensionAttributes - * @param DimensionContentInterface[] $existDimensionContents + * @param T[] $existDimensionContents */ protected function createDimensionContentCollectionFactoryInstance( array $dimensionAttributes, diff --git a/Tests/Unit/Content/Domain/Model/ContentRichEntityTraitTest.php b/Tests/Unit/Content/Domain/Model/ContentRichEntityTraitTest.php index 2babebb3..2f6f60cc 100644 --- a/Tests/Unit/Content/Domain/Model/ContentRichEntityTraitTest.php +++ b/Tests/Unit/Content/Domain/Model/ContentRichEntityTraitTest.php @@ -14,20 +14,15 @@ namespace Sulu\Bundle\ContentBundle\Tests\Unit\Content\Domain\Model; use PHPUnit\Framework\TestCase; -use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface; -use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityTrait; -use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface; +use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example; +use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\ExampleDimensionContent; class ContentRichEntityTraitTest extends TestCase { - use \Prophecy\PhpUnit\ProphecyTrait; - - protected function getContentRichEntityInstance(): ContentRichEntityInterface + protected function getContentRichEntityInstance(): Example { - return new class() implements ContentRichEntityInterface { - use ContentRichEntityTrait; - - public function createDimensionContent(): DimensionContentInterface + return new class() extends Example { + public function createDimensionContent(): ExampleDimensionContent { throw new \RuntimeException('Should not be called while executing tests.'); } @@ -45,21 +40,21 @@ public function testGetAddRemoveDimension(): void $this->assertEmpty($model->getDimensionContents()); - $modelDimension1 = $this->prophesize(DimensionContentInterface::class); - $modelDimension2 = $this->prophesize(DimensionContentInterface::class); + $modelDimension1 = new ExampleDimensionContent($model); + $modelDimension2 = new ExampleDimensionContent($model); - $model->addDimensionContent($modelDimension1->reveal()); - $model->addDimensionContent($modelDimension2->reveal()); + $model->addDimensionContent($modelDimension1); + $model->addDimensionContent($modelDimension2); $this->assertSame([ - $modelDimension1->reveal(), - $modelDimension2->reveal(), + $modelDimension1, + $modelDimension2, ], \iterator_to_array($model->getDimensionContents())); - $model->removeDimensionContent($modelDimension2->reveal()); + $model->removeDimensionContent($modelDimension2); $this->assertSame([ - $modelDimension1->reveal(), + $modelDimension1, ], \iterator_to_array($model->getDimensionContents())); } } diff --git a/Tests/Unit/Content/Domain/Model/DimensionContentCollectionTest.php b/Tests/Unit/Content/Domain/Model/DimensionContentCollectionTest.php index 465dc373..3e458258 100644 --- a/Tests/Unit/Content/Domain/Model/DimensionContentCollectionTest.php +++ b/Tests/Unit/Content/Domain/Model/DimensionContentCollectionTest.php @@ -16,19 +16,16 @@ use PHPUnit\Framework\TestCase; use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollection; use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface; -use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface; use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example; use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\ExampleDimensionContent; class DimensionContentCollectionTest extends TestCase { - use \Prophecy\PhpUnit\ProphecyTrait; - /** - * @param DimensionContentInterface[] $dimensionContents + * @param ExampleDimensionContent[] $dimensionContents * @param mixed[] $dimensionAttributes * - * @return DimensionContentCollectionInterface + * @return DimensionContentCollectionInterface */ protected function createDimensionContentCollectionInstance( array $dimensionContents, @@ -148,18 +145,18 @@ public function testGetDimensionContent(): void public function testGetDimensionContentNotExist(): void { - $dimensionContent1 = $this->prophesize(DimensionContentInterface::class); - $dimensionContent1->getLocale()->willReturn(null); - $dimensionContent1->getStage()->willReturn('draft'); - $dimensionContent2 = $this->prophesize(DimensionContentInterface::class); - $dimensionContent2->getLocale()->willReturn('de'); - $dimensionContent2->getStage()->willReturn('draft'); + $example = new Example(); + $dimensionContent1 = new ExampleDimensionContent($example); + $dimensionContent1->setStage('draft'); + $dimensionContent2 = new ExampleDimensionContent($example); + $dimensionContent2->setLocale('de'); + $dimensionContent2->setStage('draft'); $attributes = ['locale' => 'de']; $dimensionContentCollection = $this->createDimensionContentCollectionInstance([ - $dimensionContent1->reveal(), - $dimensionContent2->reveal(), + $dimensionContent1, + $dimensionContent2, ], $attributes); $this->assertNull($dimensionContentCollection->getDimensionContent(['locale' => 'en'])); diff --git a/Tests/Unit/Content/Domain/Model/DimensionContentTraitTest.php b/Tests/Unit/Content/Domain/Model/DimensionContentTraitTest.php index 68fcc934..33447344 100644 --- a/Tests/Unit/Content/Domain/Model/DimensionContentTraitTest.php +++ b/Tests/Unit/Content/Domain/Model/DimensionContentTraitTest.php @@ -20,16 +20,22 @@ class DimensionContentTraitTest extends TestCase { - protected function getDimensionContentInstance(): DimensionContentInterface + protected function getDimensionContentInstance(): DimensionContentInterface // @phpstan-ignore-line { return new class() implements DimensionContentInterface { use DimensionContentTrait; + /** + * @return never + */ public static function getResourceKey(): string { throw new \RuntimeException('Should not be called while executing tests.'); } + /** + * @return never + */ public function getResource(): ContentRichEntityInterface { throw new \RuntimeException('Should not be called while executing tests.'); diff --git a/Tests/Unit/Content/Domain/Model/RoutableTraitTest.php b/Tests/Unit/Content/Domain/Model/RoutableTraitTest.php index 5add2c4d..f40082c0 100644 --- a/Tests/Unit/Content/Domain/Model/RoutableTraitTest.php +++ b/Tests/Unit/Content/Domain/Model/RoutableTraitTest.php @@ -22,19 +22,26 @@ class RoutableTraitTest extends TestCase { use \Prophecy\PhpUnit\ProphecyTrait; - protected function getRoutableInstance(): RoutableInterface + /** + * @template T of ContentRichEntityInterface + * + * @param T $contentRichEntity + * + * @return RoutableInterface + */ + protected function getRoutableInstance(ContentRichEntityInterface $contentRichEntity): RoutableInterface { - $contentRichEntity = $this->prophesize(ContentRichEntityInterface::class); - $contentRichEntity->getId()->willReturn('content-id-123'); - - return new class($contentRichEntity->reveal()) implements RoutableInterface { + return new class($contentRichEntity) implements RoutableInterface { use RoutableTrait; /** - * @var ContentRichEntityInterface + * @var T */ private $resource; + /** + * @param T $resource + */ public function __construct(ContentRichEntityInterface $resource) { $this->resource = $resource; @@ -50,6 +57,9 @@ public function getLocale(): string return 'en'; } + /** + * @return T + */ public function getResource(): ContentRichEntityInterface { return $this->resource; @@ -59,13 +69,18 @@ public function getResource(): ContentRichEntityInterface public function testGetLocale(): void { - $model = $this->getRoutableInstance(); + $contentRichEntity = $this->prophesize(ContentRichEntityInterface::class); + + $model = $this->getRoutableInstance($contentRichEntity->reveal()); $this->assertSame('en', $model->getLocale()); } public function testGetResourceId(): void { - $model = $this->getRoutableInstance(); + $contentRichEntity = $this->prophesize(ContentRichEntityInterface::class); + $contentRichEntity->getId()->willReturn('content-id-123'); + + $model = $this->getRoutableInstance($contentRichEntity->reveal()); $this->assertSame('content-id-123', $model->getResourceId()); } } diff --git a/Tests/Unit/Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactoryTest.php b/Tests/Unit/Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactoryTest.php index 80495f90..db977cca 100644 --- a/Tests/Unit/Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactoryTest.php +++ b/Tests/Unit/Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactoryTest.php @@ -73,7 +73,12 @@ protected function createPreviewObjectProviderRegistry(array $providers): Previe } /** - * @param class-string $entityClass + * @template B of DimensionContentInterface + * @template T of ContentRichEntityInterface + * + * @param class-string $entityClass + * + * @return ContentObjectProvider */ protected function createContentObjectProvider( EntityManagerInterface $entityManager, @@ -316,6 +321,9 @@ public static function getResourceKey(): string return 'mock-resource-key'; } + /** + * @return never + */ public function getResource(): ContentRichEntityInterface { throw new \RuntimeException('Should not be called while executing tests.'); @@ -333,6 +341,9 @@ public function getResource(): ContentRichEntityInterface use SeoTrait; use TemplateTrait; + /** + * @return never + */ public function getResource(): ContentRichEntityInterface { throw new \RuntimeException('Should not be called while executing tests.'); @@ -363,6 +374,9 @@ public static function getResourceKey(): string use TemplateTrait; use WorkflowTrait; + /** + * @return never + */ public function getResource(): ContentRichEntityInterface { throw new \RuntimeException('Should not be called while executing tests.'); @@ -389,6 +403,9 @@ public static function getResourceKey(): string } /** + * @template T of DimensionContentInterface + * + * @param T $dimensionContentObject * @param mixed[] $expectedToolbarActions * * @dataProvider getContentRichEntityClassData diff --git a/Tests/Unit/Content/Infrastructure/Sulu/Automation/ContentPublishTaskHandlerTest.php b/Tests/Unit/Content/Infrastructure/Sulu/Automation/ContentPublishTaskHandlerTest.php index 42bd9195..d29d1984 100644 --- a/Tests/Unit/Content/Infrastructure/Sulu/Automation/ContentPublishTaskHandlerTest.php +++ b/Tests/Unit/Content/Infrastructure/Sulu/Automation/ContentPublishTaskHandlerTest.php @@ -22,6 +22,7 @@ use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface; use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Automation\ContentPublishTaskHandler; +use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example; use Sulu\Bundle\PageBundle\Document\PageDocument; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Contracts\Translation\TranslatorInterface; @@ -29,22 +30,24 @@ class ContentPublishTaskHandlerTest extends TestCase { use \Prophecy\PhpUnit\ProphecyTrait; + /** - * @var EntityManagerInterface|ObjectProphecy + * @var ObjectProphecy */ private $entityManager; /** - * @var ObjectRepository|ObjectProphecy + * @var ObjectProphecy> */ private $repository; /** - * @var ContentManagerInterface|ObjectProphecy + * @var ObjectProphecy */ private $contentManager; + /** - * @var TranslatorInterface|ObjectProphecy + * @var ObjectProphecy */ private $translator; @@ -67,8 +70,7 @@ protected function setUp(): void public function testPublish(): void { - /** @var ContentRichEntityInterface|ObjectProphecy $entity */ - $entity = $this->prophesize(ContentRichEntityInterface::class); + $entity = new Example(); $class = ContentRichEntityInterface::class; $id = 1; @@ -79,11 +81,11 @@ public function testPublish(): void ->shouldBeCalled(); $this->repository->findOneBy(Argument::is(['id' => $id])) - ->willReturn($entity->reveal()) + ->willReturn($entity) ->shouldBeCalled(); $this->contentManager->applyTransition( - Argument::is($entity->reveal()), + Argument::is($entity), Argument::is(['locale' => $locale]), Argument::is(WorkflowInterface::WORKFLOW_TRANSITION_PUBLISH) )->shouldBeCalled(); @@ -108,10 +110,9 @@ public function testConfiguration(): void public function testSupports(): void { - /** @var ContentRichEntityInterface|ObjectProphecy $entity */ - $entity = $this->prophesize(ContentRichEntityInterface::class); + $entity = new Example(); - $this->assertTrue($this->handler->supports(\get_class($entity->reveal()))); + $this->assertTrue($this->handler->supports(\get_class($entity))); $this->assertFalse($this->handler->supports(PageDocument::class)); } diff --git a/Tests/Unit/Content/Infrastructure/Sulu/Automation/ContentUnpublishTaskHandlerTest.php b/Tests/Unit/Content/Infrastructure/Sulu/Automation/ContentUnpublishTaskHandlerTest.php index 89cb3545..dc0c08ad 100644 --- a/Tests/Unit/Content/Infrastructure/Sulu/Automation/ContentUnpublishTaskHandlerTest.php +++ b/Tests/Unit/Content/Infrastructure/Sulu/Automation/ContentUnpublishTaskHandlerTest.php @@ -22,6 +22,7 @@ use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface; use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Automation\ContentUnpublishTaskHandler; +use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example; use Sulu\Bundle\PageBundle\Document\PageDocument; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Contracts\Translation\TranslatorInterface; @@ -29,22 +30,24 @@ class ContentUnpublishTaskHandlerTest extends TestCase { use \Prophecy\PhpUnit\ProphecyTrait; + /** - * @var EntityManagerInterface|ObjectProphecy + * @var ObjectProphecy */ private $entityManager; /** - * @var ObjectRepository|ObjectProphecy + * @var ObjectProphecy> */ private $repository; /** - * @var ContentManagerInterface|ObjectProphecy + * @var ObjectProphecy */ private $contentManager; + /** - * @var TranslatorInterface|ObjectProphecy + * @var ObjectProphecy */ private $translator; @@ -67,8 +70,7 @@ protected function setUp(): void public function testUnpublish(): void { - /** @var ContentRichEntityInterface|ObjectProphecy $entity */ - $entity = $this->prophesize(ContentRichEntityInterface::class); + $entity = new Example(); $class = ContentRichEntityInterface::class; $id = 1; @@ -79,11 +81,11 @@ public function testUnpublish(): void ->shouldBeCalled(); $this->repository->findOneBy(Argument::is(['id' => $id])) - ->willReturn($entity->reveal()) + ->willReturn($entity) ->shouldBeCalled(); $this->contentManager->applyTransition( - Argument::is($entity->reveal()), + Argument::is($entity), Argument::is(['locale' => $locale]), Argument::is(WorkflowInterface::WORKFLOW_TRANSITION_UNPUBLISH) )->shouldBeCalled(); @@ -108,10 +110,9 @@ public function testConfiguration(): void public function testSupports(): void { - /** @var ContentRichEntityInterface|ObjectProphecy $entity */ - $entity = $this->prophesize(ContentRichEntityInterface::class); + $entity = new Example(); - $this->assertTrue($this->handler->supports(\get_class($entity->reveal()))); + $this->assertTrue($this->handler->supports(\get_class($entity))); $this->assertFalse($this->handler->supports(PageDocument::class)); } diff --git a/Tests/Unit/Content/Infrastructure/Sulu/Preview/ContentObjectProviderTest.php b/Tests/Unit/Content/Infrastructure/Sulu/Preview/ContentObjectProviderTest.php index 91fe6d8e..5ddd1b24 100644 --- a/Tests/Unit/Content/Infrastructure/Sulu/Preview/ContentObjectProviderTest.php +++ b/Tests/Unit/Content/Infrastructure/Sulu/Preview/ContentObjectProviderTest.php @@ -30,27 +30,31 @@ use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Preview\PreviewDimensionContentCollection; use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Admin\ExampleAdmin; use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example; +use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\ExampleDimensionContent; +use Sulu\Bundle\TestBundle\Testing\SetGetPrivatePropertyTrait; class ContentObjectProviderTest extends TestCase { use \Prophecy\PhpUnit\ProphecyTrait; + use SetGetPrivatePropertyTrait; + /** - * @var ObjectProphecy|EntityManagerInterface + * @var ObjectProphecy */ private $entityManager; /** - * @var ObjectProphecy|ContentResolverInterface + * @var ObjectProphecy */ private $contentResolver; /** - * @var ObjectProphecy|ContentDataMapperInterface + * @var ObjectProphecy */ private $contentDataMapper; /** - * @var ContentObjectProvider + * @var ContentObjectProvider */ private $contentObjectProvider; @@ -162,13 +166,12 @@ public function testGetObjectContentNotFound(int $id = 1, string $locale = 'de') public function testGetId(int $id = 1): void { - $contentRichEntity = $this->prophesize(ContentRichEntityInterface::class); - $contentRichEntity->getId()->willReturn($id); + $contentRichEntity = new Example(); + static::setPrivateProperty($contentRichEntity, 'id', $id); - $dimensionContent = $this->prophesize(DimensionContentInterface::class); - $dimensionContent->getResource()->willReturn($contentRichEntity->reveal()); + $dimensionContent = new ExampleDimensionContent($contentRichEntity); - $actualId = (string) $this->contentObjectProvider->getId($dimensionContent->reveal()); + $actualId = (string) $this->contentObjectProvider->getId($dimensionContent); $this->assertSame((string) $id, $actualId); } @@ -196,15 +199,15 @@ public function testSetValues( 'excerptIcon' => ['id' => 4], ] ): void { - $dimensionContent = $this->prophesize(DimensionContentInterface::class); + $dimensionContent = new ExampleDimensionContent(new Example()); - $this->contentObjectProvider->setValues($dimensionContent->reveal(), $locale, $data); + $this->contentObjectProvider->setValues($dimensionContent, $locale, $data); $this->contentDataMapper->map( $data, Argument::that( function(PreviewDimensionContentCollection $dimensionContentCollection) use ($dimensionContent) { - return $dimensionContent->reveal() === $dimensionContentCollection->getDimensionContent([]); + return $dimensionContent === $dimensionContentCollection->getDimensionContent([]); } ) )->shouldBeCalledTimes(1); @@ -215,30 +218,28 @@ function(PreviewDimensionContentCollection $dimensionContentCollection) use ($di */ public function testSetContext(string $locale = 'de', array $context = ['template' => 'overview']): void { - $dimensionContent = $this->prophesize(DimensionContentInterface::class); - $dimensionContent->willImplement(TemplateInterface::class); + $dimensionContent = new ExampleDimensionContent(new Example()); - $this->contentObjectProvider->setContext($dimensionContent->reveal(), $locale, $context); + $this->contentObjectProvider->setContext($dimensionContent, $locale, $context); - $dimensionContent->setTemplateKey($context['template'])->shouldBeCalled(); + $this->assertSame($context['template'], $dimensionContent->getTemplateKey()); } public function testSerialize(): void { - $contentRichEntity = $this->prophesize(ContentRichEntityInterface::class); - $contentRichEntity->getId()->willReturn('123-456'); + $contentRichEntity = new Example(); + static::setPrivateProperty($contentRichEntity, 'id', '123-456'); - $dimensionContent = $this->prophesize(DimensionContentInterface::class); - $dimensionContent->getResource()->willReturn($contentRichEntity->reveal()); - $dimensionContent->getLocale()->willReturn('en'); - $dimensionContent->getStage()->willReturn('draft'); + $dimensionContent = new ExampleDimensionContent($contentRichEntity); + $dimensionContent->setLocale('en'); + $dimensionContent->setStage('draft'); $serializedObject = \json_encode([ 'id' => '123-456', 'locale' => 'en', ]); - $result = $this->contentObjectProvider->serialize($dimensionContent->reveal()); + $result = $this->contentObjectProvider->serialize($dimensionContent); $this->assertSame($serializedObject, $result); } @@ -273,12 +274,12 @@ public function testDeserialize(): void $query->getSingleResult()->willReturn($entity->reveal())->shouldBeCalledTimes(1); - $dimensionContent = $this->prophesize(DimensionContentInterface::class); + $dimensionContent = new ExampleDimensionContent(new Example()); $this->contentResolver->resolve( $entity->reveal(), Argument::type('array') - )->willReturn($dimensionContent->reveal())->shouldBeCalledTimes(1); + )->willReturn($dimensionContent)->shouldBeCalledTimes(1); $serializedObject = \json_encode([ 'id' => '123-456', @@ -287,7 +288,7 @@ public function testDeserialize(): void $result = $this->contentObjectProvider->deserialize($serializedObject, DimensionContentInterface::class); - $this->assertSame($dimensionContent->reveal(), $result); + $this->assertSame($dimensionContent, $result); } public function testDeserializeIdNull(): void diff --git a/Tests/Unit/Content/Infrastructure/Sulu/Preview/PreviewDimensionContentCollectionTest.php b/Tests/Unit/Content/Infrastructure/Sulu/Preview/PreviewDimensionContentCollectionTest.php index 872fa4e5..9ed324fd 100644 --- a/Tests/Unit/Content/Infrastructure/Sulu/Preview/PreviewDimensionContentCollectionTest.php +++ b/Tests/Unit/Content/Infrastructure/Sulu/Preview/PreviewDimensionContentCollectionTest.php @@ -21,22 +21,26 @@ class PreviewDimensionContentCollectionTest extends TestCase { - use \Prophecy\PhpUnit\ProphecyTrait; - + /** + * @template T of ExampleDimensionContent + * + * @param T|null $previewDimensionContent + * + * @return PreviewDimensionContentCollection + */ protected function createPreviewDimensionContentCollection( - ?DimensionContentInterface $previewDimensionContent = null, + ?ExampleDimensionContent $previewDimensionContent = null, string $locale = 'en' ): PreviewDimensionContentCollection { return new PreviewDimensionContentCollection( - $previewDimensionContent ?: $this->prophesize(DimensionContentInterface::class)->reveal(), + $previewDimensionContent ?: new ExampleDimensionContent(new Example()), $locale ); } public function testGetDimensionContentClass(): void { - $example = $this->prophesize(Example::class); - $dimensionContent = new ExampleDimensionContent($example->reveal()); + $dimensionContent = new ExampleDimensionContent(new Example()); $previewDimensionContentCollection = $this->createPreviewDimensionContentCollection($dimensionContent); @@ -48,28 +52,27 @@ public function testGetDimensionContentClass(): void public function testGetDimensionContent(): void { - $dimensionContent = $this->prophesize(DimensionContentInterface::class); + $dimensionContent = new ExampleDimensionContent(new Example()); - $previewDimensionContentCollection = $this->createPreviewDimensionContentCollection($dimensionContent->reveal()); + $previewDimensionContentCollection = $this->createPreviewDimensionContentCollection($dimensionContent); $this->assertSame( - $dimensionContent->reveal(), + $dimensionContent, $previewDimensionContentCollection->getDimensionContent([]) ); $this->assertSame( - $dimensionContent->reveal(), + $dimensionContent, $previewDimensionContentCollection->getDimensionContent(['stage' => 'draft', 'locale' => null]) ); $this->assertSame( - $dimensionContent->reveal(), + $dimensionContent, $previewDimensionContentCollection->getDimensionContent(['stage' => 'draft', 'locale' => 'en']) ); } public function testGetDimensionAttributes(): void { - $example = $this->prophesize(Example::class); - $dimensionContent = new ExampleDimensionContent($example->reveal()); + $dimensionContent = new ExampleDimensionContent(new Example()); $previewDimensionContentCollection = $this->createPreviewDimensionContentCollection( $dimensionContent, @@ -84,21 +87,21 @@ public function testGetDimensionAttributes(): void public function testGetIterator(): void { - $dimensionContent = $this->prophesize(DimensionContentInterface::class); + $dimensionContent = new ExampleDimensionContent(new Example()); - $previewDimensionContentCollection = $this->createPreviewDimensionContentCollection($dimensionContent->reveal()); + $previewDimensionContentCollection = $this->createPreviewDimensionContentCollection($dimensionContent); $this->assertSame( - [$dimensionContent->reveal()], + [$dimensionContent], \iterator_to_array($previewDimensionContentCollection) ); } public function testGetCount(): void { - $dimensionContent = $this->prophesize(DimensionContentInterface::class); + $dimensionContent = new ExampleDimensionContent(new Example()); - $previewDimensionContentCollection = $this->createPreviewDimensionContentCollection($dimensionContent->reveal()); + $previewDimensionContentCollection = $this->createPreviewDimensionContentCollection($dimensionContent); $this->assertCount(1, $previewDimensionContentCollection); } diff --git a/Tests/Unit/Content/Infrastructure/Sulu/Route/ContentRouteDefaultsProviderTest.php b/Tests/Unit/Content/Infrastructure/Sulu/Route/ContentRouteDefaultsProviderTest.php index b510106b..4877df1b 100644 --- a/Tests/Unit/Content/Infrastructure/Sulu/Route/ContentRouteDefaultsProviderTest.php +++ b/Tests/Unit/Content/Infrastructure/Sulu/Route/ContentRouteDefaultsProviderTest.php @@ -29,6 +29,7 @@ use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Structure\ContentStructureBridgeFactory; use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Structure\StructureMetadataNotFoundException; use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example; +use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\ExampleDimensionContent; use Sulu\Bundle\HttpCacheBundle\CacheLifetime\CacheLifetimeResolverInterface; use Sulu\Component\Content\Metadata\StructureMetadata; @@ -64,9 +65,9 @@ public function testSupports(): void $cacheLifetimeResolver->reveal() ); - $contentRichEntity = $this->prophesize(ContentRichEntityInterface::class); + $contentRichEntity = new Example(); - $this->assertTrue($contentRouteDefaultsProvider->supports(\get_class($contentRichEntity->reveal()))); + $this->assertTrue($contentRouteDefaultsProvider->supports(\get_class($contentRichEntity))); $this->assertFalse($contentRouteDefaultsProvider->supports(\stdClass::class)); } @@ -84,11 +85,10 @@ public function testIsPublished(): void $cacheLifetimeResolver->reveal() ); - $contentRichEntity = $this->prophesize(ContentRichEntityInterface::class); - $resolvedDimensionContent = $this->prophesize(TemplateInterface::class); - $resolvedDimensionContent->willImplement(DimensionContentInterface::class); - $resolvedDimensionContent->getLocale()->willReturn('en'); - $resolvedDimensionContent->getStage()->willReturn('live'); + $contentRichEntity = new Example(); + $resolvedDimensionContent = new ExampleDimensionContent($contentRichEntity); + $resolvedDimensionContent->setLocale('en'); + $resolvedDimensionContent->setStage('live'); $queryBuilder = $this->prophesize(QueryBuilder::class); $query = $this->prophesize(AbstractQuery::class); @@ -99,12 +99,12 @@ public function testIsPublished(): void $queryBuilder->where('entity.id = :id')->willReturn($queryBuilder->reveal()); $queryBuilder->setParameter('id', '123-123-123')->willReturn($queryBuilder->reveal()); $queryBuilder->getQuery()->willReturn($query); - $query->getSingleResult()->willReturn($contentRichEntity->reveal()); + $query->getSingleResult()->willReturn($contentRichEntity); $contentResolver->resolve( - $contentRichEntity->reveal(), + $contentRichEntity, ['locale' => 'en', 'stage' => 'live'] - )->willReturn($resolvedDimensionContent->reveal()); + )->willReturn($resolvedDimensionContent); $this->assertTrue($contentRouteDefaultsProvider->isPublished(Example::class, '123-123-123', 'en')); } @@ -153,7 +153,7 @@ public function testIsPublishedContentNotFound(): void $cacheLifetimeResolver->reveal() ); - $contentRichEntity = $this->prophesize(ContentRichEntityInterface::class); + $contentRichEntity = new Example(); $queryBuilder = $this->prophesize(QueryBuilder::class); $query = $this->prophesize(AbstractQuery::class); @@ -164,12 +164,12 @@ public function testIsPublishedContentNotFound(): void $queryBuilder->where('entity.id = :id')->willReturn($queryBuilder->reveal()); $queryBuilder->setParameter('id', '123-123-123')->willReturn($queryBuilder->reveal()); $queryBuilder->getQuery()->willReturn($query); - $query->getSingleResult()->willReturn($contentRichEntity->reveal()); + $query->getSingleResult()->willReturn($contentRichEntity); $contentResolver->resolve( - $contentRichEntity->reveal(), + $contentRichEntity, ['locale' => 'en', 'stage' => 'live'] - )->willThrow(new ContentNotFoundException($contentRichEntity->reveal(), ['locale' => 'en', 'stage' => 'live'])); + )->willThrow(new ContentNotFoundException($contentRichEntity, ['locale' => 'en', 'stage' => 'live'])); $this->assertFalse($contentRouteDefaultsProvider->isPublished(Example::class, '123-123-123', 'en')); } @@ -188,11 +188,10 @@ public function testIsPublishedWithLocalizedDimension(): void $cacheLifetimeResolver->reveal() ); - $contentRichEntity = $this->prophesize(ContentRichEntityInterface::class); - $resolvedDimensionContent = $this->prophesize(TemplateInterface::class); - $resolvedDimensionContent->willImplement(DimensionContentInterface::class); - $resolvedDimensionContent->getLocale()->willReturn('en'); - $resolvedDimensionContent->getStage()->willReturn('live'); + $contentRichEntity = new Example(); + $resolvedDimensionContent = new ExampleDimensionContent($contentRichEntity); + $resolvedDimensionContent->setLocale('en'); + $resolvedDimensionContent->setStage('live'); $queryBuilder = $this->prophesize(QueryBuilder::class); $query = $this->prophesize(AbstractQuery::class); @@ -202,10 +201,10 @@ public function testIsPublishedWithLocalizedDimension(): void $queryBuilder->where('entity.id = :id')->willReturn($queryBuilder->reveal()); $queryBuilder->setParameter('id', '123-123-123')->willReturn($queryBuilder->reveal()); $queryBuilder->getQuery()->willReturn($query); - $query->getSingleResult()->willReturn($contentRichEntity->reveal()); + $query->getSingleResult()->willReturn($contentRichEntity); - $contentResolver->resolve($contentRichEntity->reveal(), ['locale' => 'en', 'stage' => 'live']) - ->willReturn($resolvedDimensionContent->reveal()) + $contentResolver->resolve($contentRichEntity, ['locale' => 'en', 'stage' => 'live']) + ->willReturn($resolvedDimensionContent) ->shouldBeCalled(); $this->assertTrue($contentRouteDefaultsProvider->isPublished(Example::class, '123-123-123', 'en')); @@ -225,11 +224,10 @@ public function testIsPublishedWithUnlocalizedDimension(): void $cacheLifetimeResolver->reveal() ); - $contentRichEntity = $this->prophesize(ContentRichEntityInterface::class); - $resolvedDimensionContent = $this->prophesize(TemplateInterface::class); - $resolvedDimensionContent->willImplement(DimensionContentInterface::class); - $resolvedDimensionContent->getLocale()->willReturn(null); - $resolvedDimensionContent->getStage()->willReturn('live'); + $contentRichEntity = new Example(); + $resolvedDimensionContent = new ExampleDimensionContent($contentRichEntity); + $resolvedDimensionContent->setLocale(null); + $resolvedDimensionContent->setStage('live'); $queryBuilder = $this->prophesize(QueryBuilder::class); $query = $this->prophesize(AbstractQuery::class); @@ -239,10 +237,10 @@ public function testIsPublishedWithUnlocalizedDimension(): void $queryBuilder->where('entity.id = :id')->willReturn($queryBuilder->reveal()); $queryBuilder->setParameter('id', '123-123-123')->willReturn($queryBuilder->reveal()); $queryBuilder->getQuery()->willReturn($query); - $query->getSingleResult()->willReturn($contentRichEntity->reveal()); + $query->getSingleResult()->willReturn($contentRichEntity); - $contentResolver->resolve($contentRichEntity->reveal(), ['locale' => 'en', 'stage' => 'live']) - ->willReturn($resolvedDimensionContent->reveal()) + $contentResolver->resolve($contentRichEntity, ['locale' => 'en', 'stage' => 'live']) + ->willReturn($resolvedDimensionContent) ->shouldBeCalled(); $this->assertFalse($contentRouteDefaultsProvider->isPublished(Example::class, '123-123-123', 'en')); @@ -294,13 +292,13 @@ public function testGetByEntityReturnNoneTemplate(): void public function testGetByEntityReturnNoneTemplateFromPreview(): void { - $contentRichEntity = $this->prophesize(ContentRichEntityInterface::class); + $dimensionContent = $this->prophesize(DimensionContentInterface::class); $this->expectException(\RuntimeException::class); $this->expectExceptionMessage(\sprintf( 'Expected to get "%s" from ContentResolver but "%s" given.', TemplateInterface::class, - \get_class($contentRichEntity->reveal()) + \get_class($dimensionContent->reveal()) )); $entityManager = $this->prophesize(EntityManagerInterface::class); @@ -315,10 +313,7 @@ public function testGetByEntityReturnNoneTemplateFromPreview(): void $cacheLifetimeResolver->reveal() ); - /** @var DimensionContentInterface */ - $entity = $contentRichEntity->reveal(); - - $contentRouteDefaultsProvider->getByEntity(Example::class, '123-123-123', 'en', $entity); + $contentRouteDefaultsProvider->getByEntity(Example::class, '123-123-123', 'en', $dimensionContent->reveal()); } public function testGetByEntity(): void @@ -335,9 +330,8 @@ public function testGetByEntity(): void $cacheLifetimeResolver->reveal() ); - $contentRichEntity = $this->prophesize(ContentRichEntityInterface::class); - $resolvedDimensionContent = $this->prophesize(TemplateInterface::class); - $resolvedDimensionContent->willImplement(DimensionContentInterface::class); + $contentRichEntity = new Example(); + $resolvedDimensionContent = new ExampleDimensionContent($contentRichEntity); $queryBuilder = $this->prophesize(QueryBuilder::class); $query = $this->prophesize(AbstractQuery::class); @@ -348,10 +342,10 @@ public function testGetByEntity(): void $queryBuilder->where('entity.id = :id')->willReturn($queryBuilder->reveal()); $queryBuilder->setParameter('id', '123-123-123')->willReturn($queryBuilder->reveal()); $queryBuilder->getQuery()->willReturn($query); - $query->getSingleResult()->willReturn($contentRichEntity->reveal()); + $query->getSingleResult()->willReturn($contentRichEntity); - $contentResolver->resolve($contentRichEntity->reveal(), ['locale' => 'en', 'stage' => 'live']) - ->willReturn($resolvedDimensionContent->reveal()); + $contentResolver->resolve($contentRichEntity, ['locale' => 'en', 'stage' => 'live']) + ->willReturn($resolvedDimensionContent); $cacheLifetimeResolver->supports('seconds', 3600)->willReturn(true); $cacheLifetimeResolver->resolve('seconds', 3600)->willReturn(3600); @@ -363,11 +357,11 @@ public function testGetByEntity(): void $contentStructureBridge->getView()->willReturn('default'); $contentStructureBridge->getController()->willReturn('App\Controller\TestController:testAction'); $contentStructureBridge->getStructure()->willReturn($structureMetadata->reveal()); - $contentStructureBridgeFactory->getBridge($resolvedDimensionContent->reveal(), '123-123-123', 'en') + $contentStructureBridgeFactory->getBridge($resolvedDimensionContent, '123-123-123', 'en') ->willReturn($contentStructureBridge->reveal()); $result = $contentRouteDefaultsProvider->getByEntity(Example::class, '123-123-123', 'en'); - $this->assertSame($resolvedDimensionContent->reveal(), $result['object']); + $this->assertSame($resolvedDimensionContent, $result['object']); $this->assertSame('default', $result['view']); $this->assertSame($contentStructureBridge->reveal(), $result['structure']); $this->assertSame('App\Controller\TestController:testAction', $result['_controller']); @@ -388,7 +382,7 @@ public function testGetByEntityNotPublished(): void $cacheLifetimeResolver->reveal() ); - $contentRichEntity = $this->prophesize(ContentRichEntityInterface::class); + $contentRichEntity = new Example(); $queryBuilder = $this->prophesize(QueryBuilder::class); $query = $this->prophesize(AbstractQuery::class); @@ -399,9 +393,9 @@ public function testGetByEntityNotPublished(): void $queryBuilder->where('entity.id = :id')->willReturn($queryBuilder->reveal()); $queryBuilder->setParameter('id', '123-123-123')->willReturn($queryBuilder->reveal()); $queryBuilder->getQuery()->willReturn($query); - $query->getSingleResult()->willReturn($contentRichEntity->reveal()); + $query->getSingleResult()->willReturn($contentRichEntity); - $contentResolver->resolve($contentRichEntity->reveal(), ['locale' => 'en', 'stage' => 'live']) + $contentResolver->resolve($contentRichEntity, ['locale' => 'en', 'stage' => 'live']) ->will(function($arguments) { throw new ContentNotFoundException($arguments[0], $arguments[1]); }); @@ -423,9 +417,8 @@ public function testGetByEntityStructureMetadataNotFound(): void $cacheLifetimeResolver->reveal() ); - $contentRichEntity = $this->prophesize(ContentRichEntityInterface::class); - $resolvedDimensionContent = $this->prophesize(TemplateInterface::class); - $resolvedDimensionContent->willImplement(DimensionContentInterface::class); + $contentRichEntity = new Example(); + $resolvedDimensionContent = new ExampleDimensionContent($contentRichEntity); $queryBuilder = $this->prophesize(QueryBuilder::class); $query = $this->prophesize(AbstractQuery::class); @@ -436,12 +429,12 @@ public function testGetByEntityStructureMetadataNotFound(): void $queryBuilder->where('entity.id = :id')->willReturn($queryBuilder->reveal()); $queryBuilder->setParameter('id', '123-123-123')->willReturn($queryBuilder->reveal()); $queryBuilder->getQuery()->willReturn($query); - $query->getSingleResult()->willReturn($contentRichEntity->reveal()); + $query->getSingleResult()->willReturn($contentRichEntity); - $contentResolver->resolve($contentRichEntity->reveal(), ['locale' => 'en', 'stage' => 'live']) - ->willReturn($resolvedDimensionContent->reveal()); + $contentResolver->resolve($contentRichEntity, ['locale' => 'en', 'stage' => 'live']) + ->willReturn($resolvedDimensionContent); - $contentStructureBridgeFactory->getBridge($resolvedDimensionContent->reveal(), '123-123-123', 'en') + $contentStructureBridgeFactory->getBridge($resolvedDimensionContent, '123-123-123', 'en') ->willThrow(StructureMetadataNotFoundException::class); $this->assertEmpty($contentRouteDefaultsProvider->getByEntity(Example::class, '123-123-123', 'en')); diff --git a/Tests/Unit/Content/Infrastructure/Sulu/SmartContent/DataItem/ContentDataItemTest.php b/Tests/Unit/Content/Infrastructure/Sulu/SmartContent/DataItem/ContentDataItemTest.php index 1c9cc268..35d850f7 100644 --- a/Tests/Unit/Content/Infrastructure/Sulu/SmartContent/DataItem/ContentDataItemTest.php +++ b/Tests/Unit/Content/Infrastructure/Sulu/SmartContent/DataItem/ContentDataItemTest.php @@ -14,17 +14,27 @@ namespace Sulu\Bundle\ContentBundle\Tests\Unit\Content\Infrastructure\Sulu\SmartContent\DataItem; use PHPUnit\Framework\TestCase; +use Prophecy\PhpUnit\ProphecyTrait; use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface; use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\SmartContent\DataItem\ContentDataItem; +use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example; +use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\ExampleDimensionContent; +use Sulu\Bundle\TestBundle\Testing\SetGetPrivatePropertyTrait; class ContentDataItemTest extends TestCase { - use \Prophecy\PhpUnit\ProphecyTrait; + use ProphecyTrait; + use SetGetPrivatePropertyTrait; /** + * @template T of DimensionContentInterface + * + * @param T $dimensionContent * @param mixed[] $data + * + * @return ContentDataItem */ protected function getContentDataItem( DimensionContentInterface $dimensionContent, @@ -35,219 +45,193 @@ protected function getContentDataItem( public function testGetId(): void { - $resource = $this->prophesize(ContentRichEntityInterface::class); - $resource->getId()->willReturn('123-123'); + $resource = new Example(); + static::setPrivateProperty($resource, 'id', '123-123'); + $dimensionContent = new ExampleDimensionContent($resource); - $dimensionContent = $this->prophesize(DimensionContentInterface::class); - $dimensionContent->getResource()->willReturn($resource->reveal()); - - $dataItem = $this->getContentDataItem($dimensionContent->reveal(), []); + $dataItem = $this->getContentDataItem($dimensionContent, []); $this->assertSame('123-123', $dataItem->getId()); } public function testGetTitle(): void { - $resource = $this->prophesize(ContentRichEntityInterface::class); - $resource->getId()->willReturn('123-123'); - - $dimensionContent = $this->prophesize(DimensionContentInterface::class); - $dimensionContent->getResource()->willReturn($resource->reveal()); + $resource = new Example(); + static::setPrivateProperty($resource, 'id', '123-123'); + $dimensionContent = new ExampleDimensionContent($resource); $data = [ 'title' => 'test-title-1', 'name' => 'test-name-1', ]; - $dataItem = $this->getContentDataItem($dimensionContent->reveal(), $data); + $dataItem = $this->getContentDataItem($dimensionContent, $data); $this->assertSame('test-title-1', $dataItem->getTitle()); } public function testGetTitleNull(): void { - $resource = $this->prophesize(ContentRichEntityInterface::class); - $resource->getId()->willReturn('123-123'); - - $dimensionContent = $this->prophesize(DimensionContentInterface::class); - $dimensionContent->getResource()->willReturn($resource->reveal()); + $resource = new Example(); + static::setPrivateProperty($resource, 'id', '123-123'); + $dimensionContent = new ExampleDimensionContent($resource); $data = []; - $dataItem = $this->getContentDataItem($dimensionContent->reveal(), $data); + $dataItem = $this->getContentDataItem($dimensionContent, $data); $this->assertNull($dataItem->getTitle()); } public function testGetNameAsTitle(): void { - $resource = $this->prophesize(ContentRichEntityInterface::class); - $resource->getId()->willReturn('123-123'); - - $dimensionContent = $this->prophesize(DimensionContentInterface::class); - $dimensionContent->getResource()->willReturn($resource->reveal()); + $resource = new Example(); + static::setPrivateProperty($resource, 'id', '123-123'); + $dimensionContent = new ExampleDimensionContent($resource); $data = [ 'title' => null, 'name' => 'test-name-1', ]; - $dataItem = $this->getContentDataItem($dimensionContent->reveal(), $data); + $dataItem = $this->getContentDataItem($dimensionContent, $data); $this->assertSame('test-name-1', $dataItem->getTitle()); } public function testGetImage(): void { - $resource = $this->prophesize(ContentRichEntityInterface::class); - $resource->getId()->willReturn('123-123'); - - $dimensionContent = $this->prophesize(DimensionContentInterface::class); - $dimensionContent->getResource()->willReturn($resource->reveal()); + $resource = new Example(); + static::setPrivateProperty($resource, 'id', '123-123'); + $dimensionContent = new ExampleDimensionContent($resource); - $dataItem = $this->getContentDataItem($dimensionContent->reveal(), []); + $dataItem = $this->getContentDataItem($dimensionContent, []); $this->assertNull($dataItem->getImage()); } public function testGetPublished(): void { - $resource = $this->prophesize(ContentRichEntityInterface::class); - $resource->getId()->willReturn('123-123'); + $resource = new Example(); + static::setPrivateProperty($resource, 'id', '123-123'); $published = new \DateTimeImmutable(); - $dimensionContent = $this->prophesize(DimensionContentInterface::class); - $dimensionContent->willImplement(WorkflowInterface::class); - $dimensionContent->getResource()->willReturn($resource->reveal()); - $dimensionContent->getLocale()->willReturn('en'); - $dimensionContent->getWorkflowPublished()->willReturn($published); + $dimensionContent = new ExampleDimensionContent($resource); + $dimensionContent->setLocale('en'); + $dimensionContent->setWorkflowPublished($published); - $dataItem = $this->getContentDataItem($dimensionContent->reveal(), []); + $dataItem = $this->getContentDataItem($dimensionContent, []); $this->assertSame($published, $dataItem->getPublished()); } public function testGetPublishedLocaleNull(): void { - $resource = $this->prophesize(ContentRichEntityInterface::class); - $resource->getId()->willReturn('123-123'); - - $dimensionContent = $this->prophesize(DimensionContentInterface::class); - $dimensionContent->getResource()->willReturn($resource->reveal()); - $dimensionContent->getLocale()->willReturn(null); - - $dataItem = $this->getContentDataItem($dimensionContent->reveal(), []); - - $this->assertNull($dataItem->getPublished()); - } - - public function testGetPublishedNoWorkflow(): void - { - $resource = $this->prophesize(ContentRichEntityInterface::class); - $resource->getId()->willReturn('123-123'); - - $dimensionContent = $this->prophesize(DimensionContentInterface::class); - $dimensionContent->getResource()->willReturn($resource->reveal()); - $dimensionContent->getLocale()->willReturn('en'); + $resource = new Example(); + static::setPrivateProperty($resource, 'id', '123-123'); + $dimensionContent = new ExampleDimensionContent($resource); - $dataItem = $this->getContentDataItem($dimensionContent->reveal(), []); + $dataItem = $this->getContentDataItem($dimensionContent, []); $this->assertNull($dataItem->getPublished()); } public function testGetPublishedState(): void { - $resource = $this->prophesize(ContentRichEntityInterface::class); - $resource->getId()->willReturn('123-123'); + $resource = new Example(); + static::setPrivateProperty($resource, 'id', '123-123'); + $dimensionContent = new ExampleDimensionContent($resource); + $dimensionContent->setLocale('en'); + $dimensionContent->setStage('draft'); + $dimensionContent->setWorkflowPlace(WorkflowInterface::WORKFLOW_PLACE_PUBLISHED); - $dimensionContent = $this->prophesize(DimensionContentInterface::class); - $dimensionContent->willImplement(WorkflowInterface::class); - $dimensionContent->getResource()->willReturn($resource->reveal()); - $dimensionContent->getLocale()->willReturn('en'); - $dimensionContent->getStage()->willReturn('draft'); - $dimensionContent->getWorkflowPlace()->willReturn(WorkflowInterface::WORKFLOW_PLACE_PUBLISHED); - - $dataItem = $this->getContentDataItem($dimensionContent->reveal(), []); + $dataItem = $this->getContentDataItem($dimensionContent, []); $this->assertTrue($dataItem->getPublishedState()); } public function testGetPublishedStateLocaleNull(): void { - $resource = $this->prophesize(ContentRichEntityInterface::class); - $resource->getId()->willReturn('123-123'); + $resource = new Example(); + static::setPrivateProperty($resource, 'id', '123-123'); + $dimensionContent = new ExampleDimensionContent($resource); + $dimensionContent->setLocale(null); - $dimensionContent = $this->prophesize(DimensionContentInterface::class); - $dimensionContent->getResource()->willReturn($resource->reveal()); - $dimensionContent->getLocale()->willReturn(null); - - $dataItem = $this->getContentDataItem($dimensionContent->reveal(), []); + $dataItem = $this->getContentDataItem($dimensionContent, []); $this->assertFalse($dataItem->getPublishedState()); } public function testGetPublishedStateStageLive(): void { - $resource = $this->prophesize(ContentRichEntityInterface::class); - $resource->getId()->willReturn('123-123'); + $resource = new Example(); + static::setPrivateProperty($resource, 'id', '123-123'); + $dimensionContent = new ExampleDimensionContent($resource); + $dimensionContent->setLocale('en'); + $dimensionContent->setStage('live'); - $dimensionContent = $this->prophesize(DimensionContentInterface::class); - $dimensionContent->getResource()->willReturn($resource->reveal()); - $dimensionContent->getLocale()->willReturn('en'); - $dimensionContent->getStage()->willReturn('live'); - - $dataItem = $this->getContentDataItem($dimensionContent->reveal(), []); + $dataItem = $this->getContentDataItem($dimensionContent, []); $this->assertTrue($dataItem->getPublishedState()); } - public function testGetPublishedStateNoWorkflow(): void + public function testGetPublishedStateUnpublished(): void { - $resource = $this->prophesize(ContentRichEntityInterface::class); - $resource->getId()->willReturn('123-123'); + $resource = new Example(); + static::setPrivateProperty($resource, 'id', '123-123'); + $dimensionContent = new ExampleDimensionContent($resource); + $dimensionContent->setLocale('en'); + $dimensionContent->setStage('draft'); + $dimensionContent->setWorkflowPlace(WorkflowInterface::WORKFLOW_PLACE_UNPUBLISHED); - $dimensionContent = $this->prophesize(DimensionContentInterface::class); - $dimensionContent->getResource()->willReturn($resource->reveal()); - $dimensionContent->getLocale()->willReturn('en'); - $dimensionContent->getStage()->willReturn('draft'); + $dataItem = $this->getContentDataItem($dimensionContent, []); - $dataItem = $this->getContentDataItem($dimensionContent->reveal(), []); + $this->assertFalse($dataItem->getPublishedState()); + } - $this->assertTrue($dataItem->getPublishedState()); + public function testGetPublishedStateDraft(): void + { + $resource = new Example(); + static::setPrivateProperty($resource, 'id', '123-123'); + $dimensionContent = new ExampleDimensionContent($resource); + $dimensionContent->setLocale('en'); + $dimensionContent->setStage('draft'); + $dimensionContent->setWorkflowPlace(WorkflowInterface::WORKFLOW_PLACE_DRAFT); + + $dataItem = $this->getContentDataItem($dimensionContent, []); + + $this->assertFalse($dataItem->getPublishedState()); } - public function testGetPublishedStateUnpublished(): void + public function testGetPublishedNoWorkflow(): void { $resource = $this->prophesize(ContentRichEntityInterface::class); $resource->getId()->willReturn('123-123'); $dimensionContent = $this->prophesize(DimensionContentInterface::class); - $dimensionContent->willImplement(WorkflowInterface::class); $dimensionContent->getResource()->willReturn($resource->reveal()); $dimensionContent->getLocale()->willReturn('en'); - $dimensionContent->getStage()->willReturn('draft'); - $dimensionContent->getWorkflowPlace()->willReturn(WorkflowInterface::WORKFLOW_PLACE_UNPUBLISHED); + $this->assertNotInstanceOf(WorkflowInterface::class, $dimensionContent->reveal()); $dataItem = $this->getContentDataItem($dimensionContent->reveal(), []); - $this->assertFalse($dataItem->getPublishedState()); + $this->assertNull($dataItem->getPublished()); } - public function testGetPublishedStateDraft(): void + public function testGetPublishedStateNoWorkflow(): void { $resource = $this->prophesize(ContentRichEntityInterface::class); $resource->getId()->willReturn('123-123'); $dimensionContent = $this->prophesize(DimensionContentInterface::class); - $dimensionContent->willImplement(WorkflowInterface::class); $dimensionContent->getResource()->willReturn($resource->reveal()); $dimensionContent->getLocale()->willReturn('en'); $dimensionContent->getStage()->willReturn('draft'); - $dimensionContent->getWorkflowPlace()->willReturn(WorkflowInterface::WORKFLOW_PLACE_DRAFT); + $this->assertNotInstanceOf(WorkflowInterface::class, $dimensionContent->reveal()); $dataItem = $this->getContentDataItem($dimensionContent->reveal(), []); - $this->assertFalse($dataItem->getPublishedState()); + $this->assertTrue($dataItem->getPublishedState()); } } diff --git a/Tests/phpstan/stubs/ObjectProphecy.phpstub b/Tests/phpstan/stubs/ObjectProphecy.phpstub new file mode 100644 index 00000000..1a8c7b83 --- /dev/null +++ b/Tests/phpstan/stubs/ObjectProphecy.phpstub @@ -0,0 +1,30 @@ + + */ +class ObjectProphecy implements ProphecyInterface +{ + /** + * @return T + */ + public function reveal() + { + } +} diff --git a/Tests/phpstan/stubs/ProphecyInterface.phpstub b/Tests/phpstan/stubs/ProphecyInterface.phpstub new file mode 100644 index 00000000..c357166c --- /dev/null +++ b/Tests/phpstan/stubs/ProphecyInterface.phpstub @@ -0,0 +1,27 @@ +