Refactor Tests
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Controller;
|
||||
|
||||
use Codeception\Test\Unit;
|
||||
use Exception;
|
||||
use PDO;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
use Slim\Psr7\Request;
|
||||
use Slim\Psr7\Response;
|
||||
|
||||
class AbstractControllerTest extends Unit
|
||||
{
|
||||
|
||||
protected const FICTIONAL_STATUS_CODE = 666;
|
||||
|
||||
protected ContainerInterface $container;
|
||||
protected Request $request;
|
||||
protected Response|MockObject $response;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
* @throws \PHPUnit\Framework\MockObject\Exception
|
||||
*/
|
||||
protected function _before(): void
|
||||
{
|
||||
parent::_before();
|
||||
/** @noinspection SpellCheckingInspection */
|
||||
$this->container = $this->makeEmpty(ContainerInterface::class, [
|
||||
'has' => true,
|
||||
'get' => new PDO(
|
||||
'pgsql:host=psql.torsten-hettstedt.net;port=5432;dbname=testdb;user=bruce;password=mypass'
|
||||
),
|
||||
]);
|
||||
$this->response = $this->createMock(Response::class);
|
||||
$this->response->expects($this->any())->method('getStatusCode')->willReturn(self::FICTIONAL_STATUS_CODE);
|
||||
$this->response->expects($this->any())->method('getBody')->willReturn(
|
||||
$this->makeEmpty(StreamInterface::class, [
|
||||
'write' => function (mixed $data) {
|
||||
$this->assertIsString($data);
|
||||
$this->assertJson($data);
|
||||
return strlen($data);
|
||||
},
|
||||
'getContents' => 'abc',
|
||||
])
|
||||
);
|
||||
$this->response->expects($this->any())->method('withHeader')->willReturn($this->response);
|
||||
$this->response->expects($this->any())->method('withStatus')->willReturn($this->response);
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,13 @@
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Controller;
|
||||
|
||||
use Codeception\Example;
|
||||
use Codeception\Test\Unit;
|
||||
use Codeception\Attribute\DataProvider;
|
||||
use Exception;
|
||||
use PDO;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Slim\Exception\HttpBadRequestException;
|
||||
use Slim\Exception\HttpInternalServerErrorException;
|
||||
use Slim\Exception\HttpNotFoundException;
|
||||
use Slim\Psr7\Request;
|
||||
use Slim\Psr7\Response;
|
||||
@@ -17,51 +16,38 @@ use TorstenHettstedt\TimekeepingApi\Controller\HttpConflictRequestException;
|
||||
use TorstenHettstedt\TimekeepingApi\Controller\NotDatabasesException;
|
||||
use TorstenHettstedt\TimekeepingApi\Controller\WorkingHoursController;
|
||||
|
||||
class WorkingHoursControllerTest extends Unit
|
||||
class WorkingHoursControllerTest extends AbstractControllerTest
|
||||
{
|
||||
|
||||
const EXISTING_DATE = '2020-01-28';
|
||||
const EXISTING_INTERVAL = '07:20:00';
|
||||
const NEW_DATE = '2020-04-01';
|
||||
const NEW_INTERVAL = '07:59:00';
|
||||
protected const FICTIONAL_STATUS_CODE = 666;
|
||||
public const EXISTING_DATE = '2020-01-28';
|
||||
public const EXISTING_INTERVAL = '07:20:00';
|
||||
public const NEW_DATE = '2020-04-01';
|
||||
public const NEW_INTERVAL = '07:59:00';
|
||||
|
||||
protected ContainerInterface $container;
|
||||
protected Request $request;
|
||||
protected Response $response;
|
||||
protected ContainerInterface $container;
|
||||
protected Request $request;
|
||||
protected Response|MockObject $response;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
* @throws \PHPUnit\Framework\MockObject\Exception
|
||||
*/
|
||||
protected function _before(): void
|
||||
{
|
||||
parent::_before();
|
||||
/** @noinspection SpellCheckingInspection */
|
||||
$this->container = $this->makeEmpty(ContainerInterface::class, [
|
||||
'has' => true,
|
||||
'get' => new PDO('pgsql:host=psql.torsten-hettstedt.net;port=5432;dbname=testdb;user=bruce;password=mypass'),
|
||||
]);
|
||||
$this->request = $this->makeEmpty(Request::class, [
|
||||
'getParsedBody' => [
|
||||
'workingDay' => self::EXISTING_DATE,
|
||||
'workingTime' => self::EXISTING_INTERVAL,
|
||||
],
|
||||
]);
|
||||
$this->response = $this->makeEmpty(Response::class, [
|
||||
'getBody' => $this->makeEmpty(StreamInterface::class, [
|
||||
'write' => function (mixed $data) {
|
||||
$this->assertIsString($data);
|
||||
$this->assertJson($data);
|
||||
return strlen($data);
|
||||
},
|
||||
]),
|
||||
'withHeader' => $this->makeEmpty(Response::class, [
|
||||
'withStatus' => $this->makeEmpty(Response::class),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotDatabasesException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testConstructWithNonDatabase(): void
|
||||
@@ -104,12 +90,12 @@ class WorkingHoursControllerTest extends Unit
|
||||
/**
|
||||
* @param array<string, string> $queryParams
|
||||
*
|
||||
* @dataProvider valideFilterDataProvider
|
||||
*
|
||||
* @throws HttpInternalServerErrorException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotDatabasesException
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
#[DataProvider('valideFilterDataProvider')]
|
||||
public function testBrowseWithValideParameter(array $queryParams): void
|
||||
{
|
||||
$this->request = $this->makeEmpty(Request::class, [
|
||||
@@ -117,7 +103,8 @@ class WorkingHoursControllerTest extends Unit
|
||||
]);
|
||||
$controller = new WorkingHoursController($this->container);
|
||||
$response = $controller->browse($this->request, $this->response, []);
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertEquals(self::FICTIONAL_STATUS_CODE, $response->getStatusCode());
|
||||
$this->assertNotEmpty($response->getBody()->getContents());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -247,12 +234,12 @@ class WorkingHoursControllerTest extends Unit
|
||||
/**
|
||||
* @param array<string, string> $queryParams
|
||||
*
|
||||
* @dataProvider invalideFilterDataProvider
|
||||
*
|
||||
* @throws HttpInternalServerErrorException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotDatabasesException
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
#[DataProvider('invalideFilterDataProvider')]
|
||||
public function testBrowseWithInvalideParameter(array $queryParams): void
|
||||
{
|
||||
$this->request = $this->makeEmpty(Request::class, [
|
||||
@@ -264,9 +251,10 @@ class WorkingHoursControllerTest extends Unit
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotDatabasesException
|
||||
* @throws HttpBadRequestException
|
||||
* @throws HttpNotFoundException
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testUpdateExistRecord(): void
|
||||
{
|
||||
@@ -274,13 +262,15 @@ class WorkingHoursControllerTest extends Unit
|
||||
$response = $controller->update($this->request, $this->response, [
|
||||
'id' => self::EXISTING_DATE,
|
||||
]);
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertEquals(self::FICTIONAL_STATUS_CODE, $response->getStatusCode());
|
||||
$this->assertNotEmpty($response->getBody()->getContents());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotDatabasesException
|
||||
* @throws HttpBadRequestException
|
||||
* @throws HttpNotFoundException
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testUpdateNotExistRecord(): void
|
||||
{
|
||||
@@ -312,14 +302,12 @@ class WorkingHoursControllerTest extends Unit
|
||||
* @param string $date
|
||||
* @param string $time
|
||||
*
|
||||
* @dataProvider invalidCreatDataProvider
|
||||
*
|
||||
* @throws HttpBadRequestException
|
||||
* @throws HttpConflictRequestException
|
||||
* @throws HttpInternalServerErrorException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotDatabasesException
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
#[DataProvider('invalidCreatDataProvider')]
|
||||
public function testCreatInvalidData(string $date, string $time): void
|
||||
{
|
||||
$this->request = $this->makeEmpty(Request::class, [
|
||||
@@ -334,10 +322,9 @@ class WorkingHoursControllerTest extends Unit
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws HttpBadRequestException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotDatabasesException
|
||||
* @throws HttpInternalServerErrorException
|
||||
* @throws HttpConflictRequestException
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testCreatNewRecord(): void
|
||||
@@ -350,14 +337,14 @@ class WorkingHoursControllerTest extends Unit
|
||||
]);
|
||||
$controller = new WorkingHoursController($this->container);
|
||||
$response = $controller->creat($this->request, $this->response, []);
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertEquals(self::FICTIONAL_STATUS_CODE, $response->getStatusCode());
|
||||
$this->assertNotEmpty($response->getBody()->getContents());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws HttpBadRequestException
|
||||
* @throws HttpConflictRequestException
|
||||
* @throws HttpInternalServerErrorException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotDatabasesException
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testCreatExistRecord(): void
|
||||
@@ -374,9 +361,10 @@ class WorkingHoursControllerTest extends Unit
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws HttpInternalServerErrorException
|
||||
* @throws HttpNotFoundException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotDatabasesException
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testReadExistRecord(): void
|
||||
{
|
||||
@@ -384,13 +372,15 @@ class WorkingHoursControllerTest extends Unit
|
||||
$response = $controller->read($this->request, $this->response, [
|
||||
'id' => self::EXISTING_DATE,
|
||||
]);
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertEquals(self::FICTIONAL_STATUS_CODE, $response->getStatusCode());
|
||||
$this->assertNotEmpty($response->getBody()->getContents());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws HttpInternalServerErrorException
|
||||
* @throws HttpNotFoundException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotDatabasesException
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testReadNotExistRecord(): void
|
||||
{
|
||||
|
||||
@@ -2,50 +2,30 @@
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Controller;
|
||||
|
||||
use Codeception\Test\Unit;
|
||||
use Exception;
|
||||
use PDO;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Slim\Psr7\Request;
|
||||
use Slim\Psr7\Response;
|
||||
use TorstenHettstedt\TimekeepingApi\Controller\NotDatabasesException;
|
||||
use TorstenHettstedt\TimekeepingApi\Controller\WorkingHoursViewController;
|
||||
|
||||
class WorkingHoursViewControllerTest extends Unit
|
||||
class WorkingHoursViewControllerTest extends AbstractControllerTest
|
||||
{
|
||||
protected ContainerInterface $container;
|
||||
protected Request $request;
|
||||
protected Response $response;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
* @throws \PHPUnit\Framework\MockObject\Exception
|
||||
*/
|
||||
protected function _before(): void
|
||||
{
|
||||
parent::_before();
|
||||
/** @noinspection SpellCheckingInspection */
|
||||
$this->container = $this->makeEmpty(ContainerInterface::class, [
|
||||
'has' => true,
|
||||
'get' => new PDO('pgsql:host=psql.torsten-hettstedt.net;port=5432;dbname=testdb;user=bruce;password=mypass')
|
||||
]);
|
||||
$this->request = $this->makeEmpty(Request::class, []);
|
||||
$this->response = $this->makeEmpty(Response::class, [
|
||||
'getBody' => $this->makeEmpty(StreamInterface::class, [
|
||||
'write' => function (mixed $data) {
|
||||
$this->assertIsString($data);
|
||||
$this->assertJson($data);
|
||||
return strlen($data);
|
||||
},
|
||||
]),
|
||||
'withHeader' => $this->makeEmpty(Response::class, [
|
||||
'withStatus' => $this->makeEmpty(Response::class),
|
||||
]),
|
||||
]);
|
||||
$this->request = $this->makeEmpty(Request::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotDatabasesException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testConstructWithNonDatabase(): void
|
||||
@@ -58,32 +38,44 @@ class WorkingHoursViewControllerTest extends Unit
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotDatabasesException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testBrowseMonthly(): void
|
||||
{
|
||||
$controller = new WorkingHoursViewController($this->container);
|
||||
$response = $controller->browseMonthly($this->request, $this->response, []);
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertEquals(self::FICTIONAL_STATUS_CODE, $response->getStatusCode());
|
||||
$this->assertNotEmpty($response->getBody()->getContents());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotDatabasesException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testBrowseYearly(): void
|
||||
{
|
||||
$controller = new WorkingHoursViewController($this->container);
|
||||
$response = $controller->browseYearly($this->request, $this->response, []);
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertEquals(self::FICTIONAL_STATUS_CODE, $response->getStatusCode());
|
||||
$this->assertNotEmpty($response->getBody()->getContents());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotDatabasesException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testBrowseWeekly(): void
|
||||
{
|
||||
$controller = new WorkingHoursViewController($this->container);
|
||||
$response = $controller->browseWeekly($this->request, $this->response, []);
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertEquals(self::FICTIONAL_STATUS_CODE, $response->getStatusCode());
|
||||
$this->assertNotEmpty($response->getBody()->getContents());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user