Skip to content

Commit 238d815

Browse files
author
Augusto Pascutti
committed
More unit tests for *By* routine.
1 parent cb92aa4 commit 238d815

4 files changed

Lines changed: 84 additions & 18 deletions

File tree

library/Respect/Rest/Routines/AbstractRoutine.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ public function __construct($callback)
2020
$this->callback = $callback;
2121
}
2222

23+
protected function getCallback()
24+
{
25+
return $this->callback;
26+
}
27+
2328
}

library/Respect/Rest/Routines/AbstractSyncedRoutine.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ public function getParameters()
2121
/** Returns a concrete ReflectionFunctionAbstract for this routine callback */
2222
protected function getReflection()
2323
{
24-
if (is_array($this->callback))
25-
return new ReflectionMethod($this->callback[0], $this->callback[1]);
24+
$callback = $this->getCallback();
25+
if (is_array($callback))
26+
return new ReflectionMethod($callback[0], $callback[1]);
2627
else
27-
return new ReflectionFunction($this->callback);
28+
return new ReflectionFunction($callback);
2829
}
2930

3031
}

tests/legacy/Respect/Rest/Routines/AbstractSyncedRoutineTest.php

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* @covers Respect\Rest\Routines\ParamSynced
66
* @author Nick Lombard <github@jigsoft.co.za>
77
*/
8-
use \ReflectionParameterr;
9-
108
class AbstractSyncedRoutineTest extends \PHPUnit_Framework_TestCase
119
{
1210
/**
@@ -37,7 +35,7 @@ protected function tearDown()
3735
}
3836

3937
/**
40-
* @covers Respect\Rest\Routines\AbstractSyncedRoutine::getParameters
38+
* @covers Respect\Rest\Routines\AbstractSyncedRoutine
4139
*/
4240
public function testGetParameters()
4341
{
@@ -48,4 +46,56 @@ public function testGetParameters()
4846
$this->assertEquals('blogId', $parameters[1]->name);
4947
$this->assertInstanceOf('ReflectionParameter', $parameters[0]);
5048
}
49+
50+
/**
51+
* @covers Respect\Rest\Routines\AbstractSyncedRoutine
52+
* @covers Respect\Rest\Routines\AbstractRoutine
53+
*/
54+
public function test_getParameters_with_an_array()
55+
{
56+
$class = 'Respect\Rest\Routines\AbstractSyncedRoutine';
57+
$callback = array('DateTime', 'createFromFormat');
58+
$stub = $this->getMockBuilder($class)
59+
->setMethods(array('getCallback'))
60+
->disableOriginalConstructor()
61+
->getMock();
62+
$stub->expects($this->once())
63+
->method('getCallback')
64+
->will($this->returnValue($callback));
65+
66+
$this->assertContainsOnlyInstancesOf(
67+
$expected = 'ReflectionParameter',
68+
$result = $stub->getParameters()
69+
);
70+
$this->assertCount(
71+
$expected = 3,
72+
$result
73+
);
74+
}
75+
76+
/**
77+
* @covers Respect\Rest\Routines\AbstractSyncedRoutine
78+
* @covers Respect\Rest\Routines\AbstractRoutine
79+
*/
80+
public function test_getParameters_with_function()
81+
{
82+
$class = 'Respect\Rest\Routines\AbstractSyncedRoutine';
83+
$callback = function($name) { return 'Hello '.$name; };
84+
$stub = $this->getMockBuilder($class)
85+
->setMethods(array('getCallback'))
86+
->disableOriginalConstructor()
87+
->getMock();
88+
$stub->expects($this->once())
89+
->method('getCallback')
90+
->will($this->returnValue($callback));
91+
92+
$this->assertContainsOnlyInstancesOf(
93+
$expected = 'ReflectionParameter',
94+
$result = $stub->getParameters()
95+
);
96+
$this->assertCount(
97+
$expected = 1,
98+
$result
99+
);
100+
}
51101
}

tests/legacy/Respect/Rest/Routines/ByTest.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
<?php
22
namespace Respect\Rest\Routines;
33

4-
use Respect\Rest\Request;
4+
use Respect\Rest\Request,
5+
Respect\Rest\Router;
56

67
/**
78
* @covers Respect\Rest\Routines\By
89
* @author Nick Lombard <github@jigsoft.co.za>
910
*/
1011
class ByTest extends \PHPUnit_Framework_TestCase
1112
{
12-
/**
13-
* @var By
14-
*/
15-
protected $object;
16-
1713
/**
1814
* Sets up the fixture, for example, opens a network connection.
1915
* This method is called before a test is executed.
@@ -37,12 +33,26 @@ protected function tearDown()
3733
/**
3834
* @covers Respect\Rest\Routines\By::by
3935
*/
40-
public function testBy()
36+
public function test_by_with_an_anonymous_function()
37+
{
38+
$request = new Request();
39+
$params = array();
40+
$routine = new By(function() { return 'from by callback'; });
41+
$this->assertEquals('from by callback', $routine->by($request, $params));
42+
}
43+
44+
/**
45+
* @covers Respect\Rest\Routines\By
46+
*/
47+
public function test_by_on_a_route()
4148
{
42-
$request = @new Request();
43-
$params = array();
44-
$alias = &$this->object;
45-
$this->assertEquals('from by callback',
46-
$alias->by($request, $params));
49+
$router = new Router();
50+
$router->get('/', function() { return 'route'; })
51+
->by(function() { return 'by'; });
52+
// By does not affect the output of the route.
53+
$this->assertEquals(
54+
$expected = 'route',
55+
(string) $router->dispatch('GET', '/')
56+
);
4757
}
4858
}

0 commit comments

Comments
 (0)