Permalink
Browse files

What is code must have tests

  • Loading branch information...
jacobemerick committed Jan 26, 2019
1 parent e951c4e commit eef513ce5313d85a4781ece4a9f928027d873627
@@ -2,6 +2,7 @@
namespace Jacobemerick\LifestreamService\Controller;
use AvalancheDevelopment\Peel\HttpError\NotFound;
use Interop\Container\ContainerInterface as Container;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
@@ -74,7 +74,8 @@ public function getEvents(
* @param integer $id
* @return array
*/
public function findById($id) {
public function findById($id)
{
$query = "
SELECT `event`.`id`, `description`, `description_html`, `datetime`, `metadata`,
`user`.`id` AS `user_id`, `user`.`name` AS `user_name`,
@@ -0,0 +1,304 @@
<?php
namespace Jacobemerick\LifestreamService\Controller;
use AvalancheDevelopment\SwaggerRouterMiddleware\ParsedSwaggerInterface;
use Interop\Container\ContainerInterface as Container;
use Jacobemerick\LifestreamService\Model\Event as EventModel;
use Jacobemerick\LifestreamService\Serializer\Event as EventSerializer;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\StreamInterface as Stream;
use ReflectionClass;
class EventTest extends TestCase
{
public function testIsInstanceOfEvent()
{
$mockContainer = $this->createMock(Container::class);
$controller = new Event($mockContainer);
$this->assertInstanceOf(Event::class, $controller);
}
public function testConstructSetsContainer()
{
$mockContainer = $this->createMock(Container::class);
$controller = new Event($mockContainer);
$this->assertAttributeSame($mockContainer, 'container', $controller);
}
public function testGetEventSendsEventId()
{
$eventId = 927;
$mockEventModel = $this->createMock(EventModel::class);
$mockEventModel->expects($this->once())
->method('findById')
->with(
$this->equalTo($eventId)
)
->willReturn([ 'some value' ]);
$mockEventSerializer = $this->createMock(EventSerializer::class);
$mockContainer = $this->createMock(Container::class);
$mockContainer->method('get')
->will($this->returnValueMap([
[ 'eventModel', $mockEventModel ],
[ 'eventSerializer', $mockEventSerializer ],
]));
$mockSwagger = $this->createMock(ParsedSwaggerInterface::class);
$mockSwagger->expects($this->once())
->method('getParams')
->willReturn([
'event_id' => [
'value' => $eventId,
],
]);
$mockRequest = $this->createMock(Request::class);
$mockRequest->method('getAttribute')
->with('swagger')
->willReturn($mockSwagger);
$mockResponse = $this->createMock(Response::class);
$mockResponse->method('getBody')
->willReturn($this->createMock(Stream::class));
$event = $this->getMockBuilder(Event::class)
->disableOriginalConstructor()
->setMethods()
->getMock();
$reflectedEvent = new ReflectionClass(Event::class);
$reflectedContainerProperty = $reflectedEvent->getProperty('container');
$reflectedContainerProperty->setAccessible(true);
$reflectedContainerProperty->setValue($event, $mockContainer);
$event->getEvent($mockRequest, $mockResponse);
}
/**
* @expectedException AvalancheDevelopment\Peel\HttpError\NotFound
* @expectedExceptionMessage No event found
*/
public function testGetEventBailsOnInvalidEvent()
{
$eventId = 927;
$mockEventModel = $this->createMock(EventModel::class);
$mockEventModel->method('findById')
->willReturn(false);
$mockEventSerializer = $this->createMock(EventSerializer::class);
$mockEventSerializer->expects($this->never())
->method('__invoke');
$mockContainer = $this->createMock(Container::class);
$mockContainer->method('get')
->will($this->returnValueMap([
[ 'eventModel', $mockEventModel ],
[ 'eventSerializer', $mockEventSerializer ],
]));
$mockSwagger = $this->createMock(ParsedSwaggerInterface::class);
$mockSwagger->expects($this->once())
->method('getParams')
->willReturn([
'event_id' => [
'value' => $eventId,
],
]);
$mockRequest = $this->createMock(Request::class);
$mockRequest->method('getAttribute')
->with('swagger')
->willReturn($mockSwagger);
$mockResponse = $this->createMock(Response::class);
$mockResponse->method('getBody')
->willReturn($this->createMock(Stream::class));
$event = $this->getMockBuilder(Event::class)
->disableOriginalConstructor()
->setMethods()
->getMock();
$reflectedEvent = new ReflectionClass(Event::class);
$reflectedContainerProperty = $reflectedEvent->getProperty('container');
$reflectedContainerProperty->setAccessible(true);
$reflectedContainerProperty->setValue($event, $mockContainer);
$event->getEvent($mockRequest, $mockResponse);
}
public function testGetEventPassesResultToSerializer()
{
$mockEvent = [
'name' => 'some event',
'type' => 'puppies',
];
$mockEventModel = $this->createMock(EventModel::class);
$mockEventModel->method('findById')
->willReturn($mockEvent);
$mockEventSerializer = $this->createMock(EventSerializer::class);
$mockEventSerializer->expects($this->once())
->method('__invoke')
->with(
$this->equalTo($mockEvent)
);
$mockContainer = $this->createMock(Container::class);
$mockContainer->method('get')
->will($this->returnValueMap([
[ 'eventModel', $mockEventModel ],
[ 'eventSerializer', $mockEventSerializer ],
]));
$mockSwagger = $this->createMock(ParsedSwaggerInterface::class);
$mockSwagger->expects($this->once())
->method('getParams')
->willReturn([
'event_id' => [
'value' => 123,
],
]);
$mockRequest = $this->createMock(Request::class);
$mockRequest->method('getAttribute')
->with('swagger')
->willReturn($mockSwagger);
$mockResponse = $this->createMock(Response::class);
$mockResponse->method('getBody')
->willReturn($this->createMock(Stream::class));
$event = $this->getMockBuilder(Event::class)
->disableOriginalConstructor()
->setMethods()
->getMock();
$reflectedEvent = new ReflectionClass(Event::class);
$reflectedContainerProperty = $reflectedEvent->getProperty('container');
$reflectedContainerProperty->setAccessible(true);
$reflectedContainerProperty->setValue($event, $mockContainer);
$event->getEvent($mockRequest, $mockResponse);
}
public function testGetEventWritesToResponse()
{
$mockEvent = [
'name' => 'some event',
'type' => 'puppies',
];
$encodedMockEvent = json_encode($mockEvent);
$mockEventModel = $this->createMock(EventModel::class);
$mockEventModel->method('findById')
->willReturn($mockEvent);
$mockEventSerializer = $this->createMock(EventSerializer::class);
$mockEventSerializer->method('__invoke')
->will($this->returnArgument(0));
$mockContainer = $this->createMock(Container::class);
$mockContainer->method('get')
->will($this->returnValueMap([
[ 'eventModel', $mockEventModel ],
[ 'eventSerializer', $mockEventSerializer ],
]));
$mockSwagger = $this->createMock(ParsedSwaggerInterface::class);
$mockSwagger->expects($this->once())
->method('getParams')
->willReturn([
'event_id' => [
'value' => 123,
],
]);
$mockRequest = $this->createMock(Request::class);
$mockRequest->method('getAttribute')
->with('swagger')
->willReturn($mockSwagger);
$mockStream = $this->createMock(Stream::class);
$mockStream->expects($this->once())
->method('write')
->with($this->equalTo($encodedMockEvent));
$mockResponse = $this->createMock(Response::class);
$mockResponse->method('getBody')
->willReturn($mockStream);
$event = $this->getMockBuilder(Event::class)
->disableOriginalConstructor()
->setMethods()
->getMock();
$reflectedEvent = new ReflectionClass(Event::class);
$reflectedContainerProperty = $reflectedEvent->getProperty('container');
$reflectedContainerProperty->setAccessible(true);
$reflectedContainerProperty->setValue($event, $mockContainer);
$event->getEvent($mockRequest, $mockResponse);
}
public function testGetEventsReturnsResponse()
{
$mockEventModel = $this->createMock(EventModel::class);
$mockEventModel->method('findById')
->willReturn([ 'someValue' ]);
$mockEventSerializer = $this->createMock(EventSerializer::class);
$mockContainer = $this->createMock(Container::class);
$mockContainer->method('get')
->will($this->returnValueMap([
[ 'eventModel', $mockEventModel ],
[ 'eventSerializer', $mockEventSerializer ],
]));
$mockSwagger = $this->createMock(ParsedSwaggerInterface::class);
$mockSwagger->expects($this->once())
->method('getParams')
->willReturn([
'event_id' => [
'value' => 123,
],
]);
$mockRequest = $this->createMock(Request::class);
$mockRequest->method('getAttribute')
->with('swagger')
->willReturn($mockSwagger);
$mockResponse = $this->createMock(Response::class);
$mockResponse->method('getBody')
->willReturn($this->createMock(Stream::class));
$event = $this->getMockBuilder(Event::class)
->disableOriginalConstructor()
->setMethods()
->getMock();
$reflectedEvent = new ReflectionClass(Event::class);
$reflectedContainerProperty = $reflectedEvent->getProperty('container');
$reflectedContainerProperty->setAccessible(true);
$reflectedContainerProperty->setValue($event, $mockContainer);
$result = $event->getEvent($mockRequest, $mockResponse);
$this->assertSame($mockResponse, $result);
}
}
@@ -240,6 +240,51 @@ public function testGetEventsReturnsList()
$this->assertSame($types, $result);
}
public function testFindByIdSendsParams()
{
$eventId = 123;
$query = "
SELECT `event`.`id`, `description`, `description_html`, `datetime`, `metadata`,
`user`.`id` AS `user_id`, `user`.`name` AS `user_name`,
`type`.`id` AS `type_id`, `type`.`name` AS `type_name`
FROM `event`
INNER JOIN `user` ON `user`.`id` = `event`.`user_id`
INNER JOIN `type` ON `type`.`id` = `event`.`type_id`
WHERE `event`.`id` = :id
LIMIT 1";
$bindings = [
'id' => $eventId,
];
$mockPdo = $this->createMock(ExtendedPdo::class);
$mockPdo->expects($this->once())
->method('fetchOne')
->with(
$this->equalTo($query),
$this->equalTo($bindings)
);
$model = new Event($mockPdo);
$model->findById($eventId);
}
public function testFindByIdReturnsEvent()
{
$event = [
'id' => 123,
];
$mockPdo = $this->createMock(ExtendedPdo::class);
$mockPdo->method('fetchOne')
->willReturn($event);
$model = new Event($mockPdo);
$result = $model->findById(null);
$this->assertSame($event, $result);
}
public function testGetEventByTypeIdSendsParams()
{
$type = 'type';
Oops, something went wrong.

0 comments on commit eef513c

Please sign in to comment.