From dd163ca03a63b5c04d5bae345203cf1f00707ba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Wed, 6 Dec 2023 11:53:04 +0100 Subject: [PATCH] PHPUnit tests for context --- src/Component/tests/Context/ContextTest.php | 80 +++++++++++++++++++ .../Initiator/RequestContextInitiatorTest.php | 47 +++++++++++ .../Context/Option/MetadataOptionTest.php | 42 ++++++++++ .../Context/Option/RequestOptionTest.php | 42 ++++++++++ .../tests/spec/Context/ContextSpec.php | 79 ------------------ .../Initiator/RequestContextInitiatorSpec.php | 39 --------- .../spec/Context/Option/RequestOptionSpec.php | 36 --------- 7 files changed, 211 insertions(+), 154 deletions(-) create mode 100644 src/Component/tests/Context/ContextTest.php create mode 100644 src/Component/tests/Context/Initiator/RequestContextInitiatorTest.php create mode 100644 src/Component/tests/Context/Option/MetadataOptionTest.php create mode 100644 src/Component/tests/Context/Option/RequestOptionTest.php delete mode 100644 src/Component/tests/spec/Context/ContextSpec.php delete mode 100644 src/Component/tests/spec/Context/Initiator/RequestContextInitiatorSpec.php delete mode 100644 src/Component/tests/spec/Context/Option/RequestOptionSpec.php diff --git a/src/Component/tests/Context/ContextTest.php b/src/Component/tests/Context/ContextTest.php new file mode 100644 index 000000000..6cab09e6c --- /dev/null +++ b/src/Component/tests/Context/ContextTest.php @@ -0,0 +1,80 @@ +assertInstanceOf(\IteratorAggregate::class, $context); + } + + /** @test */ + public function it_can_be_constructed_with_options(): void + { + $optionOne = new DummyClassOne(); + $optionTwo = new DummyClassTwo(); + + $context = new Context($optionOne, $optionTwo); + + $this->assertEquals($optionOne, $context->get(DummyClassOne::class)); + $this->assertEquals($optionTwo, $context->get(DummyClassTwo::class)); + } + + /** @test */ + public function it_can_be_with_options(): void + { + $optionOne = new DummyClassOne(); + $optionTwo = new DummyClassTwo(); + + $context = new Context(); + $context = $context->with($optionOne, $optionTwo); + + $this->assertEquals($optionOne, $context->get(DummyClassOne::class)); + $this->assertEquals($optionTwo, $context->get(DummyClassTwo::class)); + } + + /** @test */ + public function it_can_be_without_options(): void + { + $optionOne = new DummyClassOne(); + $optionTwo = new DummyClassTwo(); + + $context = new Context(); + $context = $context->with($optionOne, $optionTwo); + $context = $context->without(DummyClassOne::class, DummyClassTwo::class); + + $this->assertNull($context->get(DummyClassOne::class)); + $this->assertNull($context->get(DummyClassTwo::class)); + } + + /** @test */ + public function it_can_be_iterated(): void + { + $optionOne = new DummyClassOne(); + $optionTwo = new DummyClassTwo(); + + $context = new Context($optionOne, $optionTwo); + + $this->assertEquals($optionOne, $context->getIterator()->current()); + $this->assertEquals(2, iterator_count($context->getIterator())); + } +} diff --git a/src/Component/tests/Context/Initiator/RequestContextInitiatorTest.php b/src/Component/tests/Context/Initiator/RequestContextInitiatorTest.php new file mode 100644 index 000000000..6fea0f5f6 --- /dev/null +++ b/src/Component/tests/Context/Initiator/RequestContextInitiatorTest.php @@ -0,0 +1,47 @@ +requestContextInitiator = new RequestContextInitiator(); + } + + /** @test */ + public function it_initializes_context(): void + { + $request = $this->prophesize(Request::class); + + $request->attributes = new ParameterBag(['_sylius' => ['resource_class' => 'App\Resource']]); + + $context = $this->requestContextInitiator->initializeContext($request->reveal()); + + $this->assertInstanceOf(Context::class, $context); + $this->assertEquals($request->reveal(), $context->get(RequestOption::class)?->request()); + } +} diff --git a/src/Component/tests/Context/Option/MetadataOptionTest.php b/src/Component/tests/Context/Option/MetadataOptionTest.php new file mode 100644 index 000000000..960d28966 --- /dev/null +++ b/src/Component/tests/Context/Option/MetadataOptionTest.php @@ -0,0 +1,42 @@ +metadata = $this->prophesize(MetadataInterface::class); + + $this->metadataOption = new MetadataOption($this->metadata->reveal()); + } + + /** @test */ + public function it_returns_request_configuration(): void + { + $this->assertEquals($this->metadata->reveal(), $this->metadataOption->metadata()); + } +} diff --git a/src/Component/tests/Context/Option/RequestOptionTest.php b/src/Component/tests/Context/Option/RequestOptionTest.php new file mode 100644 index 000000000..2ffe4f2f9 --- /dev/null +++ b/src/Component/tests/Context/Option/RequestOptionTest.php @@ -0,0 +1,42 @@ +request = $this->prophesize(Request::class); + + $this->requestOption = new RequestOption($this->request->reveal()); + } + + /** @test */ + public function it_contains_request(): void + { + $this->assertEquals($this->request->reveal(), $this->requestOption->request()); + } +} diff --git a/src/Component/tests/spec/Context/ContextSpec.php b/src/Component/tests/spec/Context/ContextSpec.php deleted file mode 100644 index 44086b474..000000000 --- a/src/Component/tests/spec/Context/ContextSpec.php +++ /dev/null @@ -1,79 +0,0 @@ -shouldHaveType(Context::class); - } - - function it_is_an_iterator(): void - { - $this->shouldImplement(\IteratorAggregate::class); - } - - function it_can_be_constructed_with_options(): void - { - $optionOne = new DummyClassOne(); - $optionTwo = new DummyClassTwo(); - - $this->beConstructedWith($optionOne, $optionTwo); - - $this->get(DummyClassOne::class)->shouldReturn($optionOne); - $this->get(DummyClassTwo::class)->shouldReturn($optionTwo); - } - - function it_can_be_with_options(): void - { - $optionOne = new DummyClassOne(); - $optionTwo = new DummyClassTwo(); - - $self = $this->with($optionOne, $optionTwo); - - $self->get(DummyClassOne::class)->shouldReturn($optionOne); - $self->get(DummyClassTwo::class)->shouldReturn($optionTwo); - } - - function it_can_be_without_options(): void - { - $optionOne = new DummyClassOne(); - $optionTwo = new DummyClassTwo(); - - $self = $this->with($optionOne, $optionTwo); - $self = $self->without(DummyClassOne::class, DummyClassTwo::class); - - $self->get(DummyClassOne::class)->shouldReturn(null); - $self->get(DummyClassTwo::class)->shouldReturn(null); - } - - function it_can_be_iterated(): void - { - $optionOne = new DummyClassOne(); - $optionTwo = new DummyClassTwo(); - - $this->beConstructedWith($optionOne, $optionTwo); - - $this->getIterator()->shouldHaveKey(0); - $this->getIterator()->shouldHaveKey(1); - - $this->getIterator()->current()->shouldReturn($optionOne); - } -} diff --git a/src/Component/tests/spec/Context/Initiator/RequestContextInitiatorSpec.php b/src/Component/tests/spec/Context/Initiator/RequestContextInitiatorSpec.php deleted file mode 100644 index 290f0ec0d..000000000 --- a/src/Component/tests/spec/Context/Initiator/RequestContextInitiatorSpec.php +++ /dev/null @@ -1,39 +0,0 @@ -shouldHaveType(RequestContextInitiator::class); - } - - function it_initializes_context(Request $request): void - { - $request->attributes = new ParameterBag(['_sylius' => ['resource_class' => 'App\Resource']]); - - $context = $this->initializeContext($request); - $context->shouldHaveType(Context::class); - - $context->get(RequestOption::class)->request()->shouldReturn($request); - } -} diff --git a/src/Component/tests/spec/Context/Option/RequestOptionSpec.php b/src/Component/tests/spec/Context/Option/RequestOptionSpec.php deleted file mode 100644 index ec6458933..000000000 --- a/src/Component/tests/spec/Context/Option/RequestOptionSpec.php +++ /dev/null @@ -1,36 +0,0 @@ -beConstructedWith($request); - } - - function it_is_initializable(): void - { - $this->shouldHaveType(RequestOption::class); - } - - function it_contains_request(Request $request): void - { - $this->request()->shouldReturn($request); - } -}