From 217c1b599f5763dbde3a76f04d60b5681a63d2fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Danyi?= Date: Thu, 23 Mar 2017 18:46:04 +0100 Subject: [PATCH] * added some example tests --- src/App/Service/CiConfigService.php | 2 +- test/AppTest/Action/CiConfigActionTest.php | 104 ++++++++++++++++++ test/AppTest/Action/CiConfigFactoryTest.php | 30 +++++ test/AppTest/Action/JcatPackageActionTest.php | 54 +++++++++ .../AppTest/Action/JcatPackageFactoryTest.php | 30 +++++ 5 files changed, 219 insertions(+), 1 deletion(-) create mode 100644 test/AppTest/Action/CiConfigActionTest.php create mode 100644 test/AppTest/Action/CiConfigFactoryTest.php create mode 100644 test/AppTest/Action/JcatPackageActionTest.php create mode 100644 test/AppTest/Action/JcatPackageFactoryTest.php diff --git a/src/App/Service/CiConfigService.php b/src/App/Service/CiConfigService.php index 6c7b318..eb8b5e3 100644 --- a/src/App/Service/CiConfigService.php +++ b/src/App/Service/CiConfigService.php @@ -47,7 +47,7 @@ class CiConfigService /** * @param int $id - * @return bool|object + * @return bool|CiConfig */ public function get(int $id) { diff --git a/test/AppTest/Action/CiConfigActionTest.php b/test/AppTest/Action/CiConfigActionTest.php new file mode 100644 index 0000000..d3028d2 --- /dev/null +++ b/test/AppTest/Action/CiConfigActionTest.php @@ -0,0 +1,104 @@ + 12, + 'configItems' => [] + ]; + + /** @var CiConfigService */ + protected $ciConfigService; + + /** @var CiConfig */ + protected $entity; + + protected function setUp() + { + $this->entity = $this->prophesize(CiConfig::class); + $this->ciConfigService = $this->prophesize(CiConfigService::class); + + $this->ciConfigService->getList()->willReturn([$this->testEntity]); + $this->ciConfigService->get(12)->willReturn($this->testEntity); + $this->ciConfigService->create($this->testEntity)->willReturn($this->testEntity); + $this->ciConfigService->delete(12)->willReturn(true); + } + + public function testResponse() + { + $ciConfigPage = new CiConfigAction($this->ciConfigService->reveal()); + $response = $ciConfigPage(new ServerRequest(), new Response(), function () { + }); + + $this->assertTrue($response instanceof Response); + } + + public function testGetList() + { + $ciConfigPage = new CiConfigAction($this->ciConfigService->reveal()); + $response = $ciConfigPage(new ServerRequest(), new Response(), function () { + }); + + $this->assertTrue($response instanceof JsonCorsResponse); + + $json = json_decode((string) $response->getBody()); + $this->assertTrue(is_array($json)); + $first = array_shift($json); + $this->assertObjectHasAttribute("id", $first); + $this->assertObjectHasAttribute("configItems", $first); + } + + public function testGet() + { + $ciConfigPage = new CiConfigAction($this->ciConfigService->reveal()); + $request = new ServerRequest(); + $response = $ciConfigPage($request->withAttribute("id", 12), new Response(), function () { + }); + + $this->assertTrue($response instanceof JsonCorsResponse); + $json = json_decode((string) $response->getBody(), true); + $this->assertTrue($json == $this->testEntity); + } + + public function testCreate() + { + $ciConfigPage = new CiConfigAction($this->ciConfigService->reveal()); + $request = new ServerRequest(); + + $body = new Stream("php://temp", "r+"); + $body->write(json_encode($this->testEntity)); + $body->rewind(); + + $changedRequest = $request->withHeader("Content-type", "application/json") + ->withMethod("POST") + ->withBody($body); + $response = $ciConfigPage($changedRequest, new Response(), function () { + }); + + $this->assertTrue($response instanceof JsonCorsResponse); + $json = json_decode((string) $response->getBody()); + $this->assertObjectHasAttribute("id", $json); + } + + public function testDelete() + { + $ciConfigPage = new CiConfigAction($this->ciConfigService->reveal()); + $request = new ServerRequest(); + $response = $ciConfigPage($request->withMethod("DELETE")->withAttribute("id", 12), new Response(), function () { + }); + + $this->assertTrue($response instanceof JsonCorsResponse); + $json = json_decode((string) $response->getBody()); + $this->assertTrue($json); + } +} diff --git a/test/AppTest/Action/CiConfigFactoryTest.php b/test/AppTest/Action/CiConfigFactoryTest.php new file mode 100644 index 0000000..98d1f1c --- /dev/null +++ b/test/AppTest/Action/CiConfigFactoryTest.php @@ -0,0 +1,30 @@ +container = $this->prophesize(ContainerInterface::class); + $ciConfigService = $this->prophesize(CiConfigService::class); + + $this->container->get(CiConfigService::class)->willReturn($ciConfigService); + } + + public function testFactory() + { + $factory = new CiConfigFactory(); + $this->assertTrue($factory instanceof CiConfigFactory); + $actionPage = $factory($this->container->reveal()); + $this->assertTrue($actionPage instanceof CiConfigAction); + } +} diff --git a/test/AppTest/Action/JcatPackageActionTest.php b/test/AppTest/Action/JcatPackageActionTest.php new file mode 100644 index 0000000..3b9ae25 --- /dev/null +++ b/test/AppTest/Action/JcatPackageActionTest.php @@ -0,0 +1,54 @@ +ciExecutorService = $this->prophesize(CiExecutorService::class); + $this->ciExecutorService->getJcatPackages()->willReturn($this->testJcatVersions); + } + + public function testResponse() + { + $ciConfigPage = new JcatPackageAction($this->ciExecutorService->reveal()); + $response = $ciConfigPage(new ServerRequest(), new Response(), function () { + }); + + $this->assertTrue($response instanceof Response); + } + + public function testGetList() + { + $ciConfigPage = new JcatPackageAction($this->ciExecutorService->reveal()); + $response = $ciConfigPage(new ServerRequest(), new Response(), function () { + }); + + $this->assertTrue($response instanceof JsonCorsResponse); + + $json = json_decode((string) $response->getBody()); + $this->assertTrue(is_array($json)); + $this->assertTrue($json == $this->testJcatVersions); + } +} diff --git a/test/AppTest/Action/JcatPackageFactoryTest.php b/test/AppTest/Action/JcatPackageFactoryTest.php new file mode 100644 index 0000000..759c5d9 --- /dev/null +++ b/test/AppTest/Action/JcatPackageFactoryTest.php @@ -0,0 +1,30 @@ +container = $this->prophesize(ContainerInterface::class); + $ciExecutorService = $this->prophesize(CiExecutorService::class); + + $this->container->get(CiExecutorService::class)->willReturn($ciExecutorService); + } + + public function testFactory() + { + $factory = new JcatPackageFactory(); + $this->assertTrue($factory instanceof JcatPackageFactory); + $actionPage = $factory($this->container->reveal()); + $this->assertTrue($actionPage instanceof JcatPackageAction); + } +}