Skip to content

Commit

Permalink
Start add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 committed Jan 23, 2015
1 parent 8633261 commit 309a18c
Show file tree
Hide file tree
Showing 22 changed files with 658 additions and 94 deletions.
2 changes: 1 addition & 1 deletion Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* It allows usage of this module even without composer.
* The original Module.php is in 'src/ZfrRest' in order to respect PSR-0
*/
require_once __DIR__ . '/src/ZfrRest/Module.php';
require_once __DIR__ . '/src/Module.php';
7 changes: 1 addition & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
"minimum-stability": "beta",
"require": {
"php": ">=5.5",
"jms/metadata": "~1.5",
"doctrine/common": "~2.4",
"doctrine/doctrine-module": "~0.8",
"zendframework/zend-cache": "~2.2",
"zendframework/zend-http": "~2.2",
"zendframework/zend-inputfilter": "~2.2",
"zendframework/zend-mvc": "~2.2",
Expand All @@ -41,8 +37,7 @@
},
"require-dev": {
"zendframework/zendframework": "~2.2",
"doctrine/doctrine-orm-module": "~0.8",
"phpunit/phpunit": "~4.4",
"phpunit/phpunit": "~4.1",
"squizlabs/php_codesniffer": "1.4.*"
},
"autoload": {
Expand Down
14 changes: 1 addition & 13 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Zend\EventManager\EventInterface;
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\DependencyIndicatorInterface;
use ZfrRest\Mvc\HttpExceptionListener;
use ZfrRest\Mvc\HttpMethodOverrideListener;
use ZfrRest\Mvc\ResourceResponseListener;
Expand All @@ -32,10 +31,7 @@
*
* @license MIT
*/
class Module implements
BootstrapListenerInterface,
ConfigProviderInterface,
DependencyIndicatorInterface
class Module implements BootstrapListenerInterface, ConfigProviderInterface
{
/**
* {@inheritDoc}
Expand Down Expand Up @@ -65,12 +61,4 @@ public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}

/**
* {@inheritDoc}
*/
public function getModuleDependencies()
{
return ['DoctrineModule'];
}
}
12 changes: 3 additions & 9 deletions src/Mvc/Controller/AbstractRestfulController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* @author Michaël Gallego <[email protected]>
* @licence MIT
*/
class AbstractRestfulController extends AbstractController
abstract class AbstractRestfulController extends AbstractController
{
/**
* {@inheritDoc}
Expand Down Expand Up @@ -156,14 +156,8 @@ private function extractErrorMessages(InputFilterInterface $inputFilter)
*/
private function getAllowedVerbs()
{
$genericVerbs = ['get', 'head', 'put', 'post', 'patch', 'delete', 'options'];
$supportedVerbs = array_intersect(get_class_methods($this), $genericVerbs);
$genericVerbs = ['delete', 'get', 'head', 'options', 'patch', 'post', 'put'];

// We normalize the verbs by uppercasing them, as this is common practice for HTTP verbs
foreach ($supportedVerbs as &$supportedVerb) {
$supportedVerb = strtoupper($supportedVerb);
}

return $supportedVerbs;
return array_intersect(get_class_methods($this), $genericVerbs);
}
}
8 changes: 3 additions & 5 deletions src/Mvc/ResourceResponseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,13 @@ public function attach(EventManagerInterface $events)
*/
public function finishResponse(MvcEvent $event)
{
$request = $event->getRequest();
$viewModel = $event->getViewModel();
$response = $event->getResponse();

if (!$request instanceof HttpRequest || !$viewModel instanceof ResourceViewModel) {
if (!$response instanceof HttpResponse) {
return;
}

$method = strtolower($request->getMethod());
$response = $event->getResponse();
$method = strtolower($event->getRequest()->getMethod());

switch ($method) {
case 'delete':
Expand Down
24 changes: 6 additions & 18 deletions src/View/Helper/RenderResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,16 @@ public function __invoke($template, array $variables, $version = null)
{
// If a version name has explicitly been set, we reuse this one, otherwise we use the one
// defined in the "current view model"
$templatePath = $this->inflectTemplatePath($template, $version);
if (null === $version) {
$version = $this->view->viewModel()->getCurrent()->getVersion();
}

$templatePath = $version . '/' . $template . '.php';

// We create a new resource view model
$resourceViewModel = new ResourceViewModel($variables, ['version' => 'default']);
$resourceViewModel = new ResourceViewModel($variables, ['version' => $version]);
$resourceViewModel->setTemplate($templatePath);

return $this->view->render($resourceViewModel);
}

/**
* Inflect a template path from template and version
*
* @param string $template
* @param string|null $version
* @return string
*/
private function inflectTemplatePath($template, $version = null)
{
if (null === $version) {
$version = $this->view->viewModel()->getCurrent()->getVersion();
}

return $version . '/' . $template . '.php';
}
}
3 changes: 0 additions & 3 deletions tests/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
* and is licensed under the MIT license.
*/

use ZfrRestTest\Util\ServiceManagerFactory;

ini_set('error_reporting', E_ALL);

$files = [__DIR__ . '/../vendor/autoload.php', __DIR__ . '/../../../autoload.php'];
Expand Down Expand Up @@ -46,5 +44,4 @@
}
}

ServiceManagerFactory::setApplicationConfig($config);
unset($files, $file, $loader, $configFiles, $configFile, $config);
43 changes: 43 additions & 0 deletions tests/ZfrRestTest/Asset/Controller/SimpleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace ZfrRestTest\Asset\Controller;

use ZfrRest\Mvc\Controller\AbstractRestfulController;

/**
* @author Michaël Gallego <[email protected]>
* @licence MIT
*/
class SimpleController extends AbstractRestfulController
{
public function get($params)
{
return 'get:' . $params['user_id'];
}

public function delete()
{
return 'delete';
}

public function fooAction()
{
return 'fooAction';
}
}
29 changes: 29 additions & 0 deletions tests/ZfrRestTest/Asset/HttpException/SimpleException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace ZfrRestTest\Asset\HttpException;

use ZfrRest\Http\Exception\AbstractHttpException;

/**
* @author Michaël Gallego <[email protected]>
* @licence MIT
*/
class SimpleException extends AbstractHttpException
{
}
15 changes: 8 additions & 7 deletions tests/ZfrRestTest/Http/Exception/Client/ClientExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace ZfrRestTest\Http\Exception\Client;

use PHPUnit_Framework_TestCase;
use ZfrRest\Http\Exception;

/**
* @license MIT
Expand All @@ -39,31 +40,31 @@ public function exceptionProvider()
{
return [
[
'exception' => 'ZfrRest\Http\Exception\Client\BadRequestException',
'exception' => Exception\Client\BadRequestException::class,
'statusCode' => 400
],
[
'exception' => 'ZfrRest\Http\Exception\Client\ConflictException',
'exception' => Exception\Client\ConflictException::class,
'statusCode' => 409
],
[
'exception' => 'ZfrRest\Http\Exception\Client\ForbiddenException',
'exception' => Exception\Client\ForbiddenException::class,
'statusCode' => 403
],
[
'exception' => 'ZfrRest\Http\Exception\Client\GoneException',
'exception' => Exception\Client\GoneException::class,
'statusCode' => 410
],
[
'exception' => 'ZfrRest\Http\Exception\Client\MethodNotAllowedException',
'exception' => Exception\Client\MethodNotAllowedException::class,
'statusCode' => 405
],
[
'exception' => 'ZfrRest\Http\Exception\Client\NotFoundException',
'exception' => Exception\Client\NotFoundException::class,
'statusCode' => 404
],
[
'exception' => 'ZfrRest\Http\Exception\Client\UnauthorizedException',
'exception' => Exception\Client\UnauthorizedException::class,
'statusCode' => 401
]
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace ZfrRestTest\Http\Exception\Client;

use PHPUnit_Framework_TestCase;
use Zend\Http\Response as HttpResponse;
use ZfrRest\Http\Exception\Client\MethodNotAllowedException;
use ZfrRest\Http\Exception\Client\UnauthorizedException;

/**
* @license MIT
* @author Michaël Gallego <[email protected]>
*
* @group Coverage
* @covers \ZfrRest\Http\Exception\Client\MethodNotAllowedException
*/
class MethodNotAllowedExceptionTest extends PHPUnit_Framework_TestCase
{
public function testFillResponse()
{
$exception = new MethodNotAllowedException('', null, ['OPTIONS', 'GET', 'POST']);
$response = new HttpResponse();

$exception->prepareResponse($response);

$this->assertEquals(405, $response->getStatusCode());
$this->assertEquals(MethodNotAllowedException::DEFAULT_MESSAGE, $response->getReasonPhrase());
$this->assertTrue($response->getHeaders()->has('Allow'));
}
}
5 changes: 3 additions & 2 deletions tests/ZfrRestTest/Http/Exception/ClientErrorExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace ZfrRestTest\Http\Exception;

use PHPUnit_Framework_TestCase;
use ZfrRest\Exception\InvalidArgumentException;
use ZfrRest\Http\Exception\ClientErrorException;

/**
Expand All @@ -33,7 +34,7 @@ class ClientExceptionTest extends PHPUnit_Framework_TestCase
public function testThrowExceptionIfStatusCodeIsOverRange()
{
$this->setExpectedException(
'ZfrRest\Exception\InvalidArgumentException',
InvalidArgumentException::class,
'Status code for client errors must be between 400 and 499, "500" given'
);

Expand All @@ -43,7 +44,7 @@ public function testThrowExceptionIfStatusCodeIsOverRange()
public function testThrowExceptionIfStatusCodeIsBelowRange()
{
$this->setExpectedException(
'ZfrRest\Exception\InvalidArgumentException',
InvalidArgumentException::class,
'Status code for client errors must be between 400 and 499, "399" given'
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace ZfrRestTest\Http\Exception\Client;

use PHPUnit_Framework_TestCase;
use ZfrRest\Http\Exception;

/**
* @license MIT
Expand All @@ -35,15 +36,15 @@ public function exceptionProvider()
{
return [
[
'exception' => 'ZfrRest\Http\Exception\Server\InternalServerErrorException',
'exception' => Exception\Server\InternalServerErrorException::class,
'statusCode' => 500
],
[
'exception' => 'ZfrRest\Http\Exception\Server\NotImplementedException',
'exception' => Exception\Server\NotImplementedException::class,
'statusCode' => 501
],
[
'exception' => 'ZfrRest\Http\Exception\Server\ServiceUnavailableException',
'exception' => Exception\Server\ServiceUnavailableException::class,
'statusCode' => 503
],
];
Expand Down
Loading

0 comments on commit 309a18c

Please sign in to comment.