Skip to content

Commit 8c571dc

Browse files
author
Designcise
committed
Instance of RouteGroup is now bound to the route group callback
1 parent 2bca9b8 commit 8c571dc

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "designcise/bitframe",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "PHP PSR-15 / PSR-7 middleware dispatcher microframework",
55
"type": "library",
66
"keywords": ["Designcise", "BitFrame", "Microframework", "Framework", "Middleware Dispatcher", "PSR-7", "PSR-15"],

src/Router/RouteGroup.php

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function __construct(
5353
*/
5454
public function __invoke()
5555
{
56-
call_user_func_array($this->callback, [$this]);
56+
call_user_func_array($this->callback->bindTo($this), [$this]);
5757
}
5858

5959
/**

test/RouteGroupTest.php

100644100755
Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
use \PHPUnit\Framework\TestCase;
1818

19-
use \BitFrame\Router\RouteGroup;
19+
use \BitFrame\Router\{RouteGroup, RouteCollectionInterface};
2020

2121
/**
2222
* @covers \BitFrame\Router\RouteGroup
@@ -28,7 +28,7 @@ class RouteGroupTest extends TestCase
2828
*/
2929
public function testGroupIsInvokedAndAddsRoutesToCollection()
3030
{
31-
$callback = function() {};
31+
$callback = function() {};
3232
$collection = $this->createMock('\BitFrame\Router\RouteCollectionInterface');
3333
$route = $this->createMock('\BitFrame\Router\Route');
3434

@@ -86,7 +86,13 @@ public function testGroupIsInvokedAndAddsRoutesToCollection()
8686
->with($this->equalTo('HEAD'), $this->equalTo('/acme/route'), $this->equalTo($callback))
8787
->will($this->returnValue($route));
8888

89-
$group = new RouteGroup('/acme', function ($route) use ($callback) {
89+
$phpunit = $this;
90+
91+
$group = new RouteGroup('/acme', function (RouteCollectionInterface $route) use ($callback, $phpunit) {
92+
// RouteGroup implements RouteCollectionInterface, so...
93+
$phpunit->assertInstanceOf(RouteGroup::class, $this);
94+
$phpunit->assertInstanceOf(RouteCollectionInterface::class, $this);
95+
9096
$route->get('/route', $callback)->setHost('example.com')->setScheme('https');
9197
$route->post('/route', $callback);
9298
$route->put('/route', $callback);
@@ -98,4 +104,29 @@ public function testGroupIsInvokedAndAddsRoutesToCollection()
98104

99105
$group();
100106
}
107+
108+
public function testRouteGroupClassContextIsBoundToRouteGroupCollection()
109+
{
110+
$callback = function() {};
111+
$collection = $this->createMock('\BitFrame\Router\RouteCollectionInterface');
112+
$route = $this->createMock('\BitFrame\Router\Route');
113+
114+
$phpunit = $this;
115+
116+
$group = new RouteGroup('/acme', function () use ($callback, $phpunit) {
117+
// RouteGroup implements RouteCollectionInterface, so...
118+
$phpunit->assertInstanceOf(RouteGroup::class, $this);
119+
$phpunit->assertInstanceOf(RouteCollectionInterface::class, $this);
120+
121+
$this->get('/route', $callback)->setHost('example.com')->setScheme('https');
122+
$this->post('/route', $callback);
123+
$this->put('/route', $callback);
124+
$this->patch('/route', $callback);
125+
$this->delete('/route', $callback);
126+
$this->options('/route', $callback);
127+
$this->head('/route', $callback);
128+
}, $collection);
129+
130+
$group();
131+
}
101132
}

0 commit comments

Comments
 (0)