Skip to content

Commit

Permalink
Merge pull request #6 from larriereguichet/feature/tests
Browse files Browse the repository at this point in the history
Feature/tests
  • Loading branch information
johnkrovitch authored Mar 1, 2019
2 parents 4b89a8c + 08c596f commit 612f07f
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Resources/config/handlers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ services:
class: LAG\SmokerBundle\Response\Handler\ResponseCodeHandler
arguments:
- '%lag_smoker.routes%'

LAG\SmokerBundle\Response\Handler\ResponseHandlerInterface: '@LAG\SmokerBundle\Response\Handler\ResponseCodeHandler'
7 changes: 5 additions & 2 deletions src/Response/Handler/ResponseCodeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ public function supports(string $routeName): bool
public function handle(string $routeName, Crawler $crawler, Client $client, array $options = []): void
{
$expectedResponseCode = Response::HTTP_OK;
$configuration = $this->routes[$routeName]['handlers'][$this->name];

if (1 <= count($options)) {
$expectedResponseCode = $options[0];
if (!is_array($configuration)) {
$expectedResponseCode = $configuration;
} elseif (key_exists('code', $configuration)) {
$expectedResponseCode = $configuration['code'];
}

if (null === $client->getResponse()) {
Expand Down
6 changes: 5 additions & 1 deletion src/Response/Handler/ResponseHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace LAG\SmokerBundle\Response\Handler;

use Goutte\Client;
use LAG\SmokerBundle\Exception\Exception;
use Symfony\Component\DomCrawler\Crawler;

interface ResponseHandlerInterface
Expand All @@ -18,12 +19,15 @@ public function supports(string $routeName): bool;

/**
* Handle the response according to the given route name. If response data does not match
* expectations, an exception will be thrown.
* expectations, an exception will be thrown. An exception will be thrown if the dom crawler is not
* initialized before passing it to the response handler.
*
* @param string $routeName
* @param Crawler $crawler
* @param Client $client
* @param array $options
*
* @throws Exception
*/
public function handle(string $routeName, Crawler $crawler, Client $client, array $options = []): void;

Expand Down
2 changes: 1 addition & 1 deletion tests/SmokerBundle/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected function assertServiceExists(string $serviceClass)
}
}

$this->assertTrue(false);
$this->assertTrue(false, 'The service for the class "'.$serviceClass.'" does not exists or is not configured');
}

/**
Expand Down
136 changes: 136 additions & 0 deletions tests/SmokerBundle/Response/Handler/ResponseCodeHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

namespace LAG\SmokerBundle\Tests\Response\Handler;

use Goutte\Client;
use LAG\SmokerBundle\Exception\Exception;
use LAG\SmokerBundle\Response\Handler\ResponseCodeHandler;
use LAG\SmokerBundle\Response\Handler\ResponseHandlerInterface;
use LAG\SmokerBundle\Tests\BaseTestCase;
use Symfony\Component\BrowserKit\Response;
use Symfony\Component\DomCrawler\Crawler;

class ResponseCodeHandlerTest extends BaseTestCase
{
public function testServiceExists()
{
$this->assertServiceExists(ResponseCodeHandler::class);
$this->assertServiceExists(ResponseHandlerInterface::class);
}

public function testSupports()
{
$handler = $this->createResponseHandler([
'existent_route' => [
'handlers' => [
'response_code' => '200',
],
],
'bad_bad_route' => [],
]);

$this->assertFalse($handler->supports('non_existent_route'));
$this->assertFalse($handler->supports('bad_bad_route'));
$this->assertTrue($handler->supports('existent_route'));
}

public function testHandle()
{
$crawler = $this->createMock(Crawler::class);
$goutte = $this->createMock(Client::class);
$goutte
->expects($this->exactly(4))
->method('getResponse')
->willReturn(new Response())
;

$handler = $this->createResponseHandler([
'existent_route' => [
'handlers' => [
'response_code' => '200',
],
],
'existent_route_too' => [
'handlers' => [
'response_code' => [
'code' => '200'
],
],
],
]);
$handler->handle('existent_route', $crawler, $goutte);
$handler->handle('existent_route_too', $crawler, $goutte);
}

public function testHandleExpecting302()
{
$crawler = $this->createMock(Crawler::class);
$goutte = $this->createMock(Client::class);
$goutte
->expects($this->exactly(2))
->method('getResponse')
->willReturn(new Response('', 302))
;

$handler = $this->createResponseHandler([
'existent_route' => [
'handlers' => [
'response_code' => '302',
],
]
]);
$handler->handle('existent_route', $crawler, $goutte);
}

public function testHandleInvalidStatusCode()
{
$crawler = $this->createMock(Crawler::class);
$goutte = $this->createMock(Client::class);
$goutte
->expects($this->exactly(2))
->method('getResponse')
->willReturn(new Response('', 302))
;

$handler = $this->createResponseHandler([
'existent_route' => [
'handlers' => [
'response_code' => '200',
],
]
]);
$this->assertExceptionRaised(Exception::class, function () use ($handler, $crawler, $goutte) {
$handler->handle('existent_route', $crawler, $goutte);
});
}

public function testHandleWithInvalidCrawler()
{
$crawler = $this->createMock(Crawler::class);
$goutte = $this->createMock(Client::class);

$handler = $this->createResponseHandler([
'existent_route' => [
'handlers' => [
'response_code' => '200',
],
]
]);

$this->assertExceptionRaised(Exception::class, function () use ($handler, $crawler, $goutte) {
$handler->handle('existent_route', $crawler, $goutte);
});
}

public function testGetName()
{
$this->assertEquals('response_code', $this->createResponseHandler()->getName());
}

private function createResponseHandler(array $routes = [])
{
$handler = new ResponseCodeHandler($routes);

return $handler;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace LAG\SmokerBundle\Tests\Response\Registry;

use LAG\SmokerBundle\Exception\Exception;
use LAG\SmokerBundle\Response\Handler\ResponseHandlerInterface;
use LAG\SmokerBundle\Response\Registry\ResponseHandlerRegistry;
use LAG\SmokerBundle\Tests\BaseTestCase;

class ResponseHandlerRegistryTest extends BaseTestCase
{
public function testServiceExists()
{
$this->assertServiceExists(ResponseHandlerRegistry::class);
}

public function testRegistry()
{
$handler = $this->createMock(ResponseHandlerInterface::class);

$registry = new ResponseHandlerRegistry();
$registry->add('my_handler', $handler);

$this->assertEquals($handler, $registry->get('my_handler'));
$this->assertTrue($registry->has('my_handler'));
$this->assertFalse($registry->has(('wrong_handler')));

$this->assertCount(1, $registry->all());
$this->assertEquals($handler, $registry->all()['my_handler']);

$this->assertExceptionRaised(Exception::class, function () use ($registry) {
$registry->get('wrong_handler');
});
}
}

0 comments on commit 612f07f

Please sign in to comment.