From 22fb8e7960dc80c5710c4aa3b23131c9aec835d6 Mon Sep 17 00:00:00 2001 From: Zhukov Roman Date: Thu, 31 Oct 2019 21:10:02 +0300 Subject: [PATCH] Add WebActionsCaller, ActionCaller and TagRequest tests (#137) --- tests/Middleware/ActionCallerTest.php | 44 ++++++++++++++ tests/Middleware/TagRequestTest.php | 29 +++++++++ tests/Middleware/WebActionsCallerTest.php | 74 +++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 tests/Middleware/ActionCallerTest.php create mode 100644 tests/Middleware/TagRequestTest.php create mode 100644 tests/Middleware/WebActionsCallerTest.php diff --git a/tests/Middleware/ActionCallerTest.php b/tests/Middleware/ActionCallerTest.php new file mode 100644 index 000000000..aa8f15203 --- /dev/null +++ b/tests/Middleware/ActionCallerTest.php @@ -0,0 +1,44 @@ +request = $this->createMock(ServerRequestInterface::class); + $this->handler = $this->createMock(RequestHandlerInterface::class); + } + + public function testProcess() + { + $container = new Container([self::class => $this]); + + $caller = new ActionCaller(self::class, 'process', $container); + + $response = $caller->process($this->request, $this->handler); + self::assertEquals(204, $response->getStatusCode()); + } + + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + $this->assertSame($this->request, $request); + $this->assertSame($this->handler, $handler); + + return new Response(204); + } +} diff --git a/tests/Middleware/TagRequestTest.php b/tests/Middleware/TagRequestTest.php new file mode 100644 index 000000000..42d34f5c8 --- /dev/null +++ b/tests/Middleware/TagRequestTest.php @@ -0,0 +1,29 @@ +createMock(ServerRequestInterface::class); + + $request + ->expects($this->once()) + ->method('withAttribute') + ->with( + $this->equalTo('requestTag'), + $this->isType('string') + ) + ->willReturnSelf(); + + $handler = $this->createMock(RequestHandlerInterface::class); + + (new TagRequest())->process($request, $handler); + } +} diff --git a/tests/Middleware/WebActionsCallerTest.php b/tests/Middleware/WebActionsCallerTest.php new file mode 100644 index 000000000..b72e4f12d --- /dev/null +++ b/tests/Middleware/WebActionsCallerTest.php @@ -0,0 +1,74 @@ +request = $this->createMock(ServerRequestInterface::class); + $this->handler = $this->createMock(RequestHandlerInterface::class); + $this->container = new Container([self::class => $this]); + } + + public function testProcess() + { + $this->request + ->method('getAttribute') + ->with($this->equalTo('action')) + ->willReturn('process'); + + $response = (new WebActionsCaller(self::class, $this->container))->process($this->request, $this->handler); + $this->assertEquals(204, $response->getStatusCode()); + } + + public function testExceptionOnNullAction() + { + $this->request + ->method('getAttribute') + ->with($this->equalTo('action')) + ->willReturn(null); + + $this->expectException(\RuntimeException::class); + (new WebActionsCaller(self::class, $this->container))->process($this->request, $this->handler); + } + + public function testHandlerInvocation() + { + $this->request + ->method('getAttribute') + ->with($this->equalTo('action')) + ->willReturn('notExistant'); + + $this->handler + ->expects($this->once()) + ->method('handle'); + + (new WebActionsCaller(self::class, $this->container))->process($this->request, $this->handler); + } + + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + $this->assertSame($this->request, $request); + $this->assertSame($this->handler, $handler); + + return new Response(204); + } +}