Skip to content

Commit a05c9f2

Browse files
committed
tests in phpspec
1 parent 480500d commit a05c9f2

File tree

7 files changed

+112
-10
lines changed

7 files changed

+112
-10
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor
2-
.composer.lock
2+
/composer.lock
3+
/bin

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: php
2+
3+
php:
4+
- 5.4
5+
- 5.5
6+
7+
install:
8+
- composer install
9+
10+
script:
11+
- bin/phpspec run -f dot

composer.json

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
{
22
"name": "yavin/symfony-init-controller",
33
"description": "Runs init controller method before every action",
4+
"license": "mit",
5+
"authors": [
6+
{"name": "Yavin", "email": "[email protected]"}
7+
],
48
"require": {
59
"symfony/http-kernel": "~2.0",
610
"symfony/event-dispatcher": "~2.0"
711
},
8-
"license": "mit",
9-
"authors": [
10-
{
11-
"name": "Yavin",
12-
"email": "[email protected]"
13-
}
14-
],
12+
"require-dev": {
13+
"phpspec/phpspec": "~2.0"
14+
},
1515
"minimum-stability": "stable",
1616
"autoload": {
1717
"psr-0":{
18-
"Yavin":"src/"
18+
"Yavin": "src/"
1919
}
20+
},
21+
"config": {
22+
"bin-dir": "bin"
2023
}
2124
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace spec\Yavin\Symfony\Controller;
4+
5+
require_once __DIR__ . '/NormalController.php';
6+
require_once __DIR__ . '/SampleInitController.php';
7+
8+
use PhpSpec\ObjectBehavior;
9+
use Prophecy\Argument;
10+
use Symfony\Component\HttpFoundation\Request;
11+
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
12+
13+
class InitControllerSubscriberSpec extends ObjectBehavior
14+
{
15+
public function it_should_be_event_subscriber()
16+
{
17+
$this->shouldHaveType('Symfony\Component\EventDispatcher\EventSubscriberInterface');
18+
}
19+
20+
public function it_should_listen_to_kernel_controller_event()
21+
{
22+
$this::getSubscribedEvents()->shouldReturn(array(
23+
'kernel.controller' => 'onKernelController',
24+
));
25+
}
26+
27+
public function it_should_run_init_method(
28+
FilterControllerEvent $event,
29+
Request $request,
30+
SampleInitController $controller
31+
) {
32+
$event->getRequest()->willReturn($request);
33+
$event->getController()->willReturn(array($controller));
34+
$controller->init($request)->shouldBeCalled();
35+
36+
$this->onKernelController($event);
37+
}
38+
39+
public function it_should_not_call_init_when_not_implementing_interface(
40+
FilterControllerEvent $event,
41+
Request $request,
42+
NormalController $controller
43+
) {
44+
$event->getRequest()->willReturn($request);
45+
$event->getController()->willReturn(array($controller));
46+
47+
$this->onKernelController($event);
48+
}
49+
50+
public function it_should_do_nothing_when_controller_is_some_other_callable(
51+
FilterControllerEvent $event,
52+
Request $request
53+
) {
54+
$event->getRequest()->willReturn($request);
55+
$event->getController()->willReturn(array(function() {}));
56+
57+
$this->onKernelController($event);
58+
}
59+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace spec\Yavin\Symfony\Controller;
4+
5+
use Symfony\Component\HttpFoundation\Request;
6+
use Yavin\Symfony\Controller\InitControllerInterface;
7+
8+
class NormalController implements InitControllerInterface
9+
{
10+
public function init(Request $request)
11+
{
12+
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace spec\Yavin\Symfony\Controller;
4+
5+
use Symfony\Component\HttpFoundation\Request;
6+
use Yavin\Symfony\Controller\InitControllerInterface;
7+
8+
class SampleInitController implements InitControllerInterface
9+
{
10+
public function init(Request $request)
11+
{
12+
13+
}
14+
}

src/Yavin/Symfony/Controller/InitControllerSubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function onKernelController(FilterControllerEvent $event)
2626
{
2727
$controller = $event->getController();
2828

29-
if (!is_array($controller)) {
29+
if (empty($controller) || !is_array($controller)) {
3030
// not a object but a different kind of callable. Do nothing
3131
return;
3232
}

0 commit comments

Comments
 (0)