From f0b6c3f52fd53c118f86bd0fa1ef680ee18996fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Tue, 10 Sep 2024 10:23:48 +0200 Subject: [PATCH] Add other conversions --- .../Common/State/RemoveProcessorSpec.php | 68 ----- .../Exception/ResourceExistsExceptionSpec.php | 40 --- .../Persistence/InMemoryRepositorySpec.php | 210 --------------- .../Common/State/RemoveProcessorTest.php | 75 ++++++ .../Exception/ResourceExistsExceptionTest.php | 48 ++++ .../Persistence/InMemoryRepositoryTest.php | 249 ++++++++++++++++++ 6 files changed, 372 insertions(+), 318 deletions(-) delete mode 100644 src/Component/spec/Doctrine/Common/State/RemoveProcessorSpec.php delete mode 100644 src/Component/spec/Doctrine/Persistence/Exception/ResourceExistsExceptionSpec.php delete mode 100644 src/Component/spec/Doctrine/Persistence/InMemoryRepositorySpec.php create mode 100644 src/Component/tests/Doctrine/Common/State/RemoveProcessorTest.php create mode 100644 src/Component/tests/Doctrine/Persistence/Exception/ResourceExistsExceptionTest.php create mode 100644 src/Component/tests/Doctrine/Persistence/InMemoryRepositoryTest.php diff --git a/src/Component/spec/Doctrine/Common/State/RemoveProcessorSpec.php b/src/Component/spec/Doctrine/Common/State/RemoveProcessorSpec.php deleted file mode 100644 index f1d8973eb..000000000 --- a/src/Component/spec/Doctrine/Common/State/RemoveProcessorSpec.php +++ /dev/null @@ -1,68 +0,0 @@ -beConstructedWith($managerRegistry); - } - - function it_is_initializable(): void - { - $this->shouldHaveType(RemoveProcessor::class); - } - - function it_removes_data( - ManagerRegistry $managerRegistry, - Operation $operation, - ObjectManager $manager, - ): void { - $data = new \stdClass(); - - $managerRegistry->getManagerForClass(\stdClass::class)->willReturn($manager); - - $manager->contains($data)->willReturn(false); - - $manager->remove($data)->shouldBeCalled(); - $manager->flush()->shouldBeCalled(); - - $this->process($data, $operation, new Context()); - } - - function it_does_nothing_when_data_is_not_managed_by_doctrine( - ManagerRegistry $managerRegistry, - Operation $operation, - ): void { - $data = new \stdClass(); - - $managerRegistry->getManagerForClass(\stdClass::class)->willReturn(null); - - $this->process($data, $operation, new Context())->shouldReturn(null); - } - - function it_does_nothing_when_data_is_not_an_object( - Operation $operation, - ): void { - $this->process(1, $operation, new Context())->shouldReturn(null); - } -} diff --git a/src/Component/spec/Doctrine/Persistence/Exception/ResourceExistsExceptionSpec.php b/src/Component/spec/Doctrine/Persistence/Exception/ResourceExistsExceptionSpec.php deleted file mode 100644 index 8c3a02515..000000000 --- a/src/Component/spec/Doctrine/Persistence/Exception/ResourceExistsExceptionSpec.php +++ /dev/null @@ -1,40 +0,0 @@ -shouldHaveType(\Exception::class); - } - - function it_extends_runtime_exception(): void - { - $this->shouldHaveType(\RuntimeException::class); - } - - function it_implements_exception_interface(): void - { - $this->shouldImplement(ExceptionInterface::class); - } - - function it_has_a_message(): void - { - $this->getMessage()->shouldReturn('Given resource already exists in the repository.'); - } -} diff --git a/src/Component/spec/Doctrine/Persistence/InMemoryRepositorySpec.php b/src/Component/spec/Doctrine/Persistence/InMemoryRepositorySpec.php deleted file mode 100644 index 23b7e9816..000000000 --- a/src/Component/spec/Doctrine/Persistence/InMemoryRepositorySpec.php +++ /dev/null @@ -1,210 +0,0 @@ -beConstructedWith(SampleBookResourceInterface::class); - } - - function it_throws_unexpected_type_exception_when_constructing_without_resource_interface(): void - { - $this->beConstructedWith(\stdClass::class); - - $this->shouldThrow(UnexpectedTypeException::class)->duringInstantiation(); - } - - function it_implements_repository_interface(): void - { - $this->shouldImplement(RepositoryInterface::class); - } - - function it_throws_invalid_argument_exception_when_adding_wrong_resource_type(ResourceInterface $resource): void - { - $this->shouldThrow(\InvalidArgumentException::class)->during('add', [$resource]); - } - - function it_adds_an_object(SampleBookResourceInterface $monocle): void - { - $monocle->getId()->willReturn(2); - - $this->add($monocle); - $this->findOneBy(['id' => 2])->shouldReturn($monocle); - } - - function it_throws_existing_resource_exception_on_adding_a_resource_which_is_already_in_repository(SampleBookResourceInterface $bike): void - { - $this->add($bike); - $this->shouldThrow(ResourceExistsException::class)->during('add', [$bike]); - } - - function it_removes_a_resource(SampleBookResourceInterface $shirt): void - { - $shirt->getId()->willReturn(5); - - $this->add($shirt); - $this->remove($shirt); - - $this->findOneBy(['id' => 5])->shouldReturn(null); - } - - function it_finds_object_by_id(SampleBookResourceInterface $monocle): void - { - $monocle->getId()->willReturn(2); - - $this->add($monocle); - $this->find(2)->shouldReturn($monocle); - } - - function it_returns_null_if_cannot_find_object_by_id(): void - { - $this->find(2)->shouldReturn(null); - } - - function it_returns_all_objects_when_finding_by_an_empty_parameter_array( - SampleBookResourceInterface $book, - SampleBookResourceInterface $shirt, - ): void { - $book->getId()->willReturn(10); - $book->getName()->willReturn('Book'); - - $shirt->getId()->willReturn(5); - $shirt->getName()->willReturn('Shirt'); - - $this->add($book); - $this->add($shirt); - - $this->findBy([])->shouldReturn([$book, $shirt]); - } - - function it_finds_many_objects_by_multiple_criteria_orders_a_limit_and_an_offset( - SampleBookResourceInterface $firstBook, - SampleBookResourceInterface $secondBook, - SampleBookResourceInterface $thirdBook, - SampleBookResourceInterface $fourthBook, - SampleBookResourceInterface $wrongIdBook, - SampleBookResourceInterface $wrongNameBook, - ): void { - $id = 80; - $name = 'Book'; - - $firstBook->getId()->willReturn($id); - $secondBook->getId()->willReturn($id); - $thirdBook->getId()->willReturn($id); - $fourthBook->getId()->willReturn($id); - $wrongNameBook->getId()->willReturn($id); - $wrongIdBook->getId()->willReturn(100); - - $firstBook->getName()->willReturn($name); - $secondBook->getName()->willReturn($name); - $thirdBook->getName()->willReturn($name); - $fourthBook->getName()->willReturn($name); - $wrongIdBook->getName()->willReturn($name); - $wrongNameBook->getName()->willReturn('Tome'); - - $firstBook->getRating()->willReturn(3); - $secondBook->getRating()->willReturn(2); - $thirdBook->getRating()->willReturn(2); - $fourthBook->getRating()->willReturn(4); - - $firstBook->getTitle()->willReturn('World War Z'); - $secondBook->getTitle()->willReturn('World War Z'); - $thirdBook->getTitle()->willReturn('Call of Cthulhu'); - $fourthBook->getTitle()->willReturn('Art of War'); - - $this->add($firstBook); - $this->add($secondBook); - $this->add($thirdBook); - $this->add($fourthBook); - $this->add($wrongIdBook); - $this->add($wrongNameBook); - - $this->findBy( - $criteria = [ - 'name' => $name, - 'id' => $id, - ], - $orderBy = [ - 'rating' => RepositoryInterface::ORDER_ASCENDING, - 'title' => RepositoryInterface::ORDER_DESCENDING, - ], - $limit = 2, - $offset = 1, - )->shouldReturn([$thirdBook, $firstBook]); - } - - function it_throws_invalid_argument_exception_when_finding_one_object_with_empty_parameter_array(): void - { - $this->shouldThrow(\InvalidArgumentException::class)->during('findOneBy', [[]]); - } - - function it_finds_one_object_by_parameter(SampleBookResourceInterface $book, SampleBookResourceInterface $shirt): void - { - $book->getName()->willReturn('Book'); - $shirt->getName()->willReturn('Shirt'); - - $this->add($book); - $this->add($shirt); - - $this->findOneBy(['name' => 'Book'])->shouldReturn($book); - } - - function it_returns_first_result_while_finding_one_by_parameters( - SampleBookResourceInterface $book, - SampleBookResourceInterface $secondBook, - ): void { - $book->getName()->willReturn('Book'); - $secondBook->getName()->willReturn('Book'); - - $this->add($book); - $this->add($secondBook); - - $this->findOneBy(['name' => 'Book'])->shouldReturn($book); - } - - function it_finds_all_objects_in_memory(SampleBookResourceInterface $book, SampleBookResourceInterface $shirt): void - { - $this->add($book); - $this->add($shirt); - - $this->findAll()->shouldReturn([$book, $shirt]); - } - - function it_return_empty_array_when_memory_is_empty(): void - { - $this->findAll()->shouldReturn([]); - } - - function it_creates_paginator(): void - { - $this->createPaginator()->shouldHaveType(Pagerfanta::class); - } - - function it_returns_stated_class_name(): void - { - $this->getClassName()->shouldReturn(SampleBookResourceInterface::class); - } -} diff --git a/src/Component/tests/Doctrine/Common/State/RemoveProcessorTest.php b/src/Component/tests/Doctrine/Common/State/RemoveProcessorTest.php new file mode 100644 index 000000000..48eaecece --- /dev/null +++ b/src/Component/tests/Doctrine/Common/State/RemoveProcessorTest.php @@ -0,0 +1,75 @@ +managerRegistry = $this->prophesize(ManagerRegistry::class); + $this->removeProcessor = new RemoveProcessor($this->managerRegistry->reveal()); + } + + public function testItIsInitializable(): void + { + $this->assertInstanceOf(RemoveProcessor::class, $this->removeProcessor); + } + + public function testItRemovesData(): void + { + $data = new \stdClass(); + $operation = $this->prophesize(Operation::class)->reveal(); + $manager = $this->prophesize(ObjectManager::class); + + $this->managerRegistry->getManagerForClass(\stdClass::class)->willReturn($manager->reveal()); + $manager->contains($data)->willReturn(false); + + $manager->remove($data)->shouldBeCalled(); + $manager->flush()->shouldBeCalled(); + + $this->removeProcessor->process($data, $operation, new Context()); + } + + public function testItDoesNothingWhenDataIsNotManagedByDoctrine(): void + { + $data = new \stdClass(); + $operation = $this->prophesize(Operation::class)->reveal(); + + $this->managerRegistry->getManagerForClass(\stdClass::class)->willReturn(null); + + $this->assertNull($this->removeProcessor->process($data, $operation, new Context())); + } + + public function testItDoesNothingWhenDataIsNotAnObject(): void + { + $operation = $this->prophesize(Operation::class)->reveal(); + + $this->assertNull($this->removeProcessor->process(1, $operation, new Context())); + } +} diff --git a/src/Component/tests/Doctrine/Persistence/Exception/ResourceExistsExceptionTest.php b/src/Component/tests/Doctrine/Persistence/Exception/ResourceExistsExceptionTest.php new file mode 100644 index 000000000..7c4def742 --- /dev/null +++ b/src/Component/tests/Doctrine/Persistence/Exception/ResourceExistsExceptionTest.php @@ -0,0 +1,48 @@ +assertInstanceOf(\Exception::class, $exception); + } + + public function testItExtendsRuntimeException(): void + { + $exception = new ResourceExistsException(); + $this->assertInstanceOf(\RuntimeException::class, $exception); + } + + public function testItImplementsExceptionInterface(): void + { + $exception = new ResourceExistsException(); + $this->assertInstanceOf(ExceptionInterface::class, $exception); + } + + public function testItHasAMessage(): void + { + $exception = new ResourceExistsException(); + $this->assertEquals('Given resource already exists in the repository.', $exception->getMessage()); + } +} diff --git a/src/Component/tests/Doctrine/Persistence/InMemoryRepositoryTest.php b/src/Component/tests/Doctrine/Persistence/InMemoryRepositoryTest.php new file mode 100644 index 000000000..9b9770139 --- /dev/null +++ b/src/Component/tests/Doctrine/Persistence/InMemoryRepositoryTest.php @@ -0,0 +1,249 @@ +repository = new InMemoryRepository(SampleBookResourceInterface::class); + } + + public function testItThrowsUnexpectedTypeExceptionWhenConstructingWithoutResourceInterface(): void + { + $this->expectException(UnexpectedTypeException::class); + new InMemoryRepository(\stdClass::class); + } + + public function testItImplementsRepositoryInterface(): void + { + $this->assertInstanceOf(RepositoryInterface::class, $this->repository); + } + + public function testItThrowsInvalidArgumentExceptionWhenAddingWrongResourceType(): void + { + /** @var ObjectProphecy $resource */ + $resource = $this->prophesize(ResourceInterface::class); + + $this->expectException(\InvalidArgumentException::class); + $this->repository->add($resource->reveal()); + } + + public function testItAddsAnObject(): void + { + /** @var ObjectProphecy $monocle */ + $monocle = $this->prophesize(SampleBookResourceInterface::class); + + $monocle->getId()->willReturn(2); + $this->repository->add($monocle->reveal()); + $this->assertSame($monocle->reveal(), $this->repository->findOneBy(['id' => 2])); + } + + public function testItThrowsExistingResourceExceptionOnAddingAResourceWhichIsAlreadyInRepository(): void + { + /** @var ObjectProphecy $bike */ + $bike = $this->prophesize(SampleBookResourceInterface::class); + + $this->repository->add($bike->reveal()); + $this->expectException(ResourceExistsException::class); + $this->repository->add($bike->reveal()); + } + + public function testItRemovesAResource(): void + { + /** @var ObjectProphecy $shirt */ + $shirt = $this->prophesize(SampleBookResourceInterface::class); + $shirt->getId()->willReturn(5); + + $this->repository->add($shirt->reveal()); + $this->repository->remove($shirt->reveal()); + + $this->assertNull($this->repository->findOneBy(['id' => 5])); + } + + public function testItFindsObjectById(): void + { + /** @var ObjectProphecy $monocle */ + $monocle = $this->prophesize(SampleBookResourceInterface::class); + $monocle->getId()->willReturn(2); + + $this->repository->add($monocle->reveal()); + $this->assertSame($monocle->reveal(), $this->repository->find(2)); + } + + public function testItReturnsNullIfCannotFindObjectById(): void + { + $this->assertNull($this->repository->find(2)); + } + + public function testItReturnsAllObjectsWhenFindingByAnEmptyParameterArray(): void + { + /** @var ObjectProphecy $book */ + $book = $this->prophesize(SampleBookResourceInterface::class); + + /** @var ObjectProphecy $shirt */ + $shirt = $this->prophesize(SampleBookResourceInterface::class); + + $book->getId()->willReturn(10); + $book->getName()->willReturn('Book'); + + $shirt->getId()->willReturn(5); + $shirt->getName()->willReturn('Shirt'); + + $this->repository->add($book->reveal()); + $this->repository->add($shirt->reveal()); + + $this->assertSame([$book->reveal(), $shirt->reveal()], $this->repository->findBy([])); + } + + public function testItFindsManyObjectsByMultipleCriteriaOrdersALimitAndAnOffset(): void + { + /** @var ObjectProphecy $firstBook */ + $firstBook = $this->prophesize(SampleBookResourceInterface::class); + + /** @var ObjectProphecy $secondBook */ + $secondBook = $this->prophesize(SampleBookResourceInterface::class); + + /** @var ObjectProphecy $thirdBook */ + $thirdBook = $this->prophesize(SampleBookResourceInterface::class); + + /** @var ObjectProphecy $fourthBook */ + $fourthBook = $this->prophesize(SampleBookResourceInterface::class); + + /** @var ObjectProphecy $wrongIdBook */ + $wrongIdBook = $this->prophesize(SampleBookResourceInterface::class); + + /** @var ObjectProphecy $wrongNameBook */ + $wrongNameBook = $this->prophesize(SampleBookResourceInterface::class); + + $id = 80; + $name = 'Book'; + + $firstBook->getId()->willReturn($id); + $secondBook->getId()->willReturn($id); + $thirdBook->getId()->willReturn($id); + $fourthBook->getId()->willReturn($id); + $wrongNameBook->getId()->willReturn($id); + $wrongIdBook->getId()->willReturn(100); + + $firstBook->getName()->willReturn($name); + $secondBook->getName()->willReturn($name); + $thirdBook->getName()->willReturn($name); + $fourthBook->getName()->willReturn($name); + $wrongIdBook->getName()->willReturn($name); + $wrongNameBook->getName()->willReturn('Tome'); + + $firstBook->getRating()->willReturn(3); + $secondBook->getRating()->willReturn(2); + $thirdBook->getRating()->willReturn(2); + $fourthBook->getRating()->willReturn(4); + + $firstBook->getTitle()->willReturn('World War Z'); + $secondBook->getTitle()->willReturn('World War Z'); + $thirdBook->getTitle()->willReturn('Call of Cthulhu'); + $fourthBook->getTitle()->willReturn('Art of War'); + + $this->repository->add($firstBook->reveal()); + $this->repository->add($secondBook->reveal()); + $this->repository->add($thirdBook->reveal()); + $this->repository->add($fourthBook->reveal()); + $this->repository->add($wrongIdBook->reveal()); + $this->repository->add($wrongNameBook->reveal()); + + $this->assertSame( + [$thirdBook->reveal(), $firstBook->reveal()], + $this->repository->findBy( + ['name' => $name, 'id' => $id], + ['rating' => RepositoryInterface::ORDER_ASCENDING, 'title' => RepositoryInterface::ORDER_DESCENDING], + 2, + 1, + ), + ); + } + + public function testItThrowsInvalidArgumentExceptionWhenFindingOneObjectWithEmptyParameterArray(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->repository->findOneBy([]); + } + + public function testItFindsOneObjectByParameter(): void + { + $book = $this->prophesize(SampleBookResourceInterface::class); + $shirt = $this->prophesize(SampleBookResourceInterface::class); + + $book->getName()->willReturn('Book'); + $shirt->getName()->willReturn('Shirt'); + + $this->repository->add($book->reveal()); + $this->repository->add($shirt->reveal()); + + $this->assertSame($book->reveal(), $this->repository->findOneBy(['name' => 'Book'])); + } + + public function testItReturnsFirstResultWhileFindingOneByParameters(): void + { + $book = $this->prophesize(SampleBookResourceInterface::class); + $secondBook = $this->prophesize(SampleBookResourceInterface::class); + + $book->getName()->willReturn('Book'); + $secondBook->getName()->willReturn('Book'); + + $this->repository->add($book->reveal()); + $this->repository->add($secondBook->reveal()); + + $this->assertSame($book->reveal(), $this->repository->findOneBy(['name' => 'Book'])); + } + + public function testItFindsAllObjectsInMemory(): void + { + $book = $this->prophesize(SampleBookResourceInterface::class); + $shirt = $this->prophesize(SampleBookResourceInterface::class); + + $this->repository->add($book->reveal()); + $this->repository->add($shirt->reveal()); + + $this->assertSame([$book->reveal(), $shirt->reveal()], $this->repository->findAll()); + } + + public function testItReturnsEmptyArrayWhenMemoryIsEmpty(): void + { + $this->assertSame([], $this->repository->findAll()); + } + + public function testItCreatesPaginator(): void + { + $this->assertInstanceOf(Pagerfanta::class, $this->repository->createPaginator()); + } + + public function testItReturnsStatedClassName(): void + { + $this->assertSame(SampleBookResourceInterface::class, $this->repository->getClassName()); + } +}