Skip to content

Commit

Permalink
Improved testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Marko Kallio committed Jan 6, 2016
1 parent 1545f14 commit 1b9fd5d
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 19 deletions.
16 changes: 5 additions & 11 deletions src/YamlRoute/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private function _newRoute($name, $route)
}

Router::$method(
$path, $options, function ($routes) use ($route, $name) {
$path, $options, function ($routes) use ($route, $name, $method) {
$exclude = ['pass', 'validate', 'routes', 'extensions', 'default_route_class', 'path'];

if (isset($route['config'])) {
Expand All @@ -229,25 +229,19 @@ private function _newRoute($name, $route)

$thirdParam = $this->_buildThirdParam($name, $route);

$path = $method == 'scope' ? $route['path'] : '/';

/* @var \Cake\Routing\Router $routes */
$routes->connect('/', $opts, $thirdParam);
$routes->connect($path, $opts, $thirdParam);

// Debugging
if ($this->_debug) {
$this->_addToDump("\t" . '$routes->connect(\'/\', ' . $this->_arrToStr($opts) . ', ' . $this->_arrToStr($thirdParam) . ');');
$this->_addToDump("\t" . '$routes->connect(\'' . $path . '\', ' . $this->_arrToStr($opts) . ', ' . $this->_arrToStr($thirdParam) . ');');
}
}
if (isset($route['config']['routes'])) {
foreach ($route['config']['routes'] as $key => $x) {
if (isset($x['config'])) {

if (isset($x['config']['extensions']) && is_array($x['config']['extensions'])) {
/* @var \Cake\Routing\Router $routes */
$routes->extensions($x['config']['extensions']);
if ($this->_debug) {
$this->_addToDump("\t" . '$routes->extensions(' . $this->_arrToStr($x['config']['extensions']) . ');');
}
}
$x = self::_createPassParams($x);
$opts = [];
foreach ($x['config'] as $k => $item) {
Expand Down
8 changes: 4 additions & 4 deletions src/YamlRoute/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class Validator
*/
public static function run($data)
{
if (count($data['route']) === 0) {
if (!isset($data['route']) || count($data['route']) === 0) {
throw new ValidatorException('Invalid routing data in file \'' . $data['file'] . '\'!');
}
foreach ($data['route'] as $name => $route) {
self::checkRoute($name, $route, true);
self::_checkRoute($name, $route, true);
}

return true;
Expand All @@ -40,7 +40,7 @@ public static function run($data)
*
* @throws \makallio85\YamlRoute\Exception\ValidatorException
*/
private static function checkRoute($name, $route, $root)
private static function _checkRoute($name, $route, $root)
{
if (!isset($route['path'])) {
throw new ValidatorException("Route path missing for route '$name''!");
Expand All @@ -51,7 +51,7 @@ private static function checkRoute($name, $route, $root)
}
if (isset($route['config']['routes'])) {
foreach ($route['config']['routes'] as $name => $route) {
self::checkRoute($name, $route, false);
self::_checkRoute($name, $route, false);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/YamlRoute/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @package makallio85\YamlRoute\Test
*/
class GeneratorTest extends \PHPUnit_Framework_TestCase
class GeneratorTest extends YamlRouteTest
{
/**
* @expectedException \makallio85\YamlRoute\Exception\GeneratorException
Expand Down
7 changes: 6 additions & 1 deletion tests/YamlRoute/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @package makallio85\YamlRoute\Test
*/
class IntegrationTest extends \PHPUnit_Framework_TestCase
class IntegrationTest extends YamlRouteTest
{
/**
* Basic integration test
Expand All @@ -25,6 +25,11 @@ public function testIntegration()
$assert .= "\t" . '$routes->fallbacks(\'DashedRoute\');' . "\n";
$assert .= '});' . "\n\n";

$assert .= '\Cake\Routing\Router::scope(\'/\', [], function ($routes) {' . "\n";
$assert .= "\t" . '$routes->connect(\'/bar\', [\'controller\' => \'Fizz\'], [\'_name\' => \'foo\']);' . "\n";
$assert .= "\t" . '$routes->fallbacks(\'DashedRoute\');' . "\n";
$assert .= '});' . "\n\n";

$assert .= '\Cake\Routing\Router::plugin(\'PluginCars\', [\'path\' => \'/cars\'], function ($routes) {' . "\n";
$assert .= "\t" . '$routes->extensions([\'0\' => \'json\', \'1\' => \'xml\']);' . "\n";
$assert .= "\t" . '$routes->connect(\'/\', [\'plugin\' => \'PluginCars\', \'controller\' => \'Cars\', \'action\' => \'index\'], [\'_name\' => \'cars\']);' . "\n";
Expand Down
2 changes: 1 addition & 1 deletion tests/YamlRoute/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @package makallio85\YamlRoute\Test
*/
class PluginTest extends \PHPUnit_Framework_TestCase
class PluginTest extends YamlRouteTest
{
/**
* @expectedException \makallio85\YamlRoute\Exception\PluginException
Expand Down
48 changes: 48 additions & 0 deletions tests/YamlRoute/ValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace makallio85\YamlRoute\Test;

use makallio85\YamlRoute\Validator;

/**
* Class ValidatorTest
*
* @package makallio85\YamlRoute\Test
*/
class ValidatorTest extends YamlRouteTest
{
/**
* @expectedException \makallio85\YamlRoute\Exception\ValidatorException
*/
public function testEmptyRoute()
{
Validator::run(['file' => 'foo.bar']);
}

/**
* @expectedException \makallio85\YamlRoute\Exception\ValidatorException
*/
public function testRoutePathMissing()
{
$Validator = new Validator();
$this->_invokeMethod($Validator, '_checkRoute', ['foo', ['bar'], false]);
}

/**
* @expectedException \makallio85\YamlRoute\Exception\ValidatorException
*/
public function testActionPresentButControllerNot()
{
$Validator = new Validator();
$this->_invokeMethod($Validator, '_checkRoute', ['foo', ['path' => 'bar', 'config' => ['action' => 'fizz']], false]);
}

/**
* @expectedException \makallio85\YamlRoute\Exception\ValidatorException
*/
public function testRouteConfigMissing()
{
$Validator = new Validator();
$this->_invokeMethod($Validator, '_checkRoute', ['foo', ['path' => 'bar'], false]);
}
}
26 changes: 26 additions & 0 deletions tests/YamlRoute/YamlRouteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace makallio85\YamlRoute\Test;

class YamlRouteTest extends \PHPUnit_Framework_TestCase
{
/**
* Call protected/private method of a class.
*
* @param object &$object Instantiated object that we will run method on.
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
*
* @return mixed Method return.
*/
protected function _invokeMethod(&$object, $methodName, array $parameters = [])
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);

return $method->invokeArgs($object, $parameters);
}

public function testInit() {}
}
6 changes: 5 additions & 1 deletion tests/config/routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ root:
path: /
config:
default_route_class: DashedRoute
fallbacks: DashedRoute
fallbacks: DashedRoute

foo:
path: /bar
config: 'PluginCars.foo'
File renamed without changes.
1 change: 1 addition & 0 deletions tests/plugins/PluginCars/config/foo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
controller: Fizz

0 comments on commit 1b9fd5d

Please sign in to comment.