Skip to content

Commit 8e8c34c

Browse files
committed
Cleanup tests
1 parent 966ffe6 commit 8e8c34c

File tree

2 files changed

+61
-120
lines changed

2 files changed

+61
-120
lines changed

src/Symfony/Component/HttpKernel/Tests/Controller/ArgumentResolverManagerTest.php

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,96 +22,110 @@ class ArgumentResolverManagerTest extends \PHPUnit_Framework_TestCase
2222

2323
public function setUp()
2424
{
25-
$this->manager = new ArgumentResolverManager();
2625
$this->resolver1 = $this->getMock('Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface');
2726
$this->resolver2 = $this->getMock('Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface');
27+
28+
$this->manager = new ArgumentResolverManager();
2829
$this->manager->add($this->resolver1);
2930
$this->manager->add($this->resolver2);
3031

3132
$this->request = $this->getMock('Symfony\Component\HttpFoundation\Request');
3233
}
3334

34-
public function testGetArgumentsFirstResolverAccepts()
35+
public function testGetArgumentsWithoutControllerParameters()
3536
{
36-
$this->resolver1->expects($this->any())->method('accepts')->will($this->returnValue(true));
37-
$this->resolver1->expects($this->any())
38-
->method('resolve')
39-
->will($this->returnValue('resolved_value'));
37+
$this->assertArguments(array(), function () { });
38+
}
4039

41-
$controller = $this->getControllerWithOneArgument();
40+
public function testGetArgumentsFirstResolverAccepts()
41+
{
42+
$this->promiseResolverToMatch($this->resolver1, 'resolved_value');
4243

43-
$arguments = $this->manager->getArguments($this->request, $controller);
44-
$this->assertEquals(array('resolved_value'), $arguments);
44+
$this->assertArguments(array('resolved_value'), $this->getControllerWithOneParameter());
4545
}
4646

4747
public function testGetArgumentsSecondResolverAccepts()
4848
{
49-
$this->resolver1->expects($this->any())->method('accepts')->will($this->returnValue(false));
50-
$this->resolver2->expects($this->any())->method('accepts')->will($this->returnValue(true));
51-
$this->resolver2->expects($this->any())
52-
->method('resolve')
53-
->will($this->returnValue('resolved_value'));
54-
55-
$controller = $this->getControllerWithOneArgument();
49+
$this->promiseResolverToNotMatch($this->resolver1);
50+
$this->promiseResolverToMatch($this->resolver2, 'resolved_value');
5651

57-
$arguments = $this->manager->getArguments($this->request, $controller);
58-
$this->assertEquals(array('resolved_value'), $arguments);
52+
$this->assertArguments(array('resolved_value'), $this->getControllerWithOneParameter());
5953
}
6054

6155
/**
6256
* @expectedException RuntimeException
6357
*/
6458
public function testGetArgumentsFailsIfNoResolverAccepts()
6559
{
66-
$this->resolver1->expects($this->any())->method('accepts')->will($this->returnValue(false));
67-
$this->resolver2->expects($this->any())->method('accepts')->will($this->returnValue(false));
60+
$this->promiseResolverToNotMatch($this->resolver1);
61+
$this->promiseResolverToNotMatch($this->resolver2);
6862

69-
$controller = $this->getControllerWithOneArgument();
70-
$this->manager->getArguments($this->request, $controller);
63+
$this->manager->getArguments($this->request, $this->getControllerWithOneParameter());
7164
}
7265

73-
public function testGetArgumentResolvingMultipleArguments()
66+
public function testGetArgumentResolvingMultipleParameters()
7467
{
7568
$this->resolver1->expects($this->any())
76-
->method('accepts')
69+
->method('supports')
7770
->will($this->onConsecutiveCalls(false, true, true));
7871
$this->resolver1->expects($this->any())
7972
->method('resolve')
8073
->will($this->onConsecutiveCalls('1st resolved by 1', '2nd resolved by 1'));
8174

8275
$this->resolver2->expects($this->any())
83-
->method('accepts')
76+
->method('supports')
8477
->will($this->onConsecutiveCalls(true, false, true));
8578
$this->resolver2->expects($this->any())
8679
->method('resolve')
8780
->will($this->onConsecutiveCalls('1st resolved by 2', '2nd resolved by 2'));
8881

89-
$controller = function ($a, $b, $c) { };
82+
$this->assertArguments(
83+
array('1st resolved by 2', '1st resolved by 1', '2nd resolved by 1'),
84+
function ($a, $b, $c) { }
85+
);
86+
}
87+
88+
public function testControllerWithOneOptionalParameterWhichDoesNotMatch()
89+
{
90+
$this->promiseResolverToNotMatch($this->resolver1);
91+
$this->promiseResolverToNotMatch($this->resolver2);
92+
93+
$this->assertArguments(array('default'), function ($a = 'default') { });
94+
}
95+
96+
public function testControllerWithOneOptionalParameterWhichDoesMatch()
97+
{
98+
$this->promiseResolverToMatch($this->resolver1, 'resolved by 1');
99+
$this->promiseResolverToNotMatch($this->resolver2);
90100

91-
$arguments = $this->manager->getArguments($this->request, $controller);
92-
$this->assertEquals(array('1st resolved by 2', '1st resolved by 1', '2nd resolved by 1'), $arguments);
101+
$this->assertArguments(array('resolved by 1'), function ($a = 'default') { });
93102
}
94103

95-
public function testControllerWithOneOptionalArgumentWhichDoesNotMatch()
104+
public function testControllerWithOneParameterWithNullDefault()
96105
{
97-
$this->resolver1->expects($this->any())->method('accepts')->will($this->returnValue(false));
98-
$this->resolver2->expects($this->any())->method('accepts')->will($this->returnValue(false));
106+
$this->promiseResolverToNotMatch($this->resolver1);
107+
$this->promiseResolverToNotMatch($this->resolver2);
108+
109+
$this->assertArguments(array(null), function ($a = null) { });
110+
}
99111

100-
$arguments = $this->manager->getArguments($this->request, function ($a = 'default') { });
101-
$this->assertEquals(array('default'), $arguments);
112+
private function assertArguments(array $expected, $controller)
113+
{
114+
$this->assertEquals($expected, $this->manager->getArguments($this->request, $controller));
102115
}
103116

104-
public function testControllerWithOneOptionalArgumentWhichDoMatch()
117+
private function promiseResolverToMatch($resolver, $return)
105118
{
106-
$this->resolver1->expects($this->any())->method('accepts')->will($this->returnValue(true));
107-
$this->resolver1->expects($this->any())->method('resolve')->will($this->returnValue('resolved by 1'));
108-
$this->resolver2->expects($this->any())->method('accepts')->will($this->returnValue(false));
119+
$resolver->expects($this->any())->method('supports')->will($this->returnValue(true));
120+
$resolver->expects($this->any())->method('resolve')->will($this->returnValue($return));
121+
}
109122

110-
$arguments = $this->manager->getArguments($this->request, function ($a = 'default') { });
111-
$this->assertEquals(array('resolved by 1'), $arguments);
123+
private function promiseResolverToNotMatch($resolver)
124+
{
125+
$resolver->expects($this->any())->method('supports')->will($this->returnValue(false));
112126
}
113127

114-
protected function getControllerWithOneArgument()
128+
private function getControllerWithOneParameter()
115129
{
116130
return function ($a) { };
117131
}

src/Symfony/Component/HttpKernel/Tests/Controller/ControllerResolverTest.php

Lines changed: 7 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,27 @@ public function testGetControllerWithObjectAndMethod()
5252
$resolver = $this->createControllerResolver();
5353

5454
$request = Request::create('/');
55-
$request->attributes->set('_controller', array($this, 'controllerMethod1'));
55+
$request->attributes->set('_controller', array($this, 'controllerMethod'));
5656
$controller = $resolver->getController($request);
57-
$this->assertSame(array($this, 'controllerMethod1'), $controller);
57+
$this->assertSame(array($this, 'controllerMethod'), $controller);
5858
}
5959

6060
public function testGetControllerWithClassAndMethod()
6161
{
6262
$resolver = $this->createControllerResolver();
6363

6464
$request = Request::create('/');
65-
$request->attributes->set('_controller', array('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', 'controllerMethod4'));
65+
$request->attributes->set('_controller', array('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', 'staticControllerMethod'));
6666
$controller = $resolver->getController($request);
67-
$this->assertSame(array('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', 'controllerMethod4'), $controller);
67+
$this->assertSame(array('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', 'staticControllerMethod'), $controller);
6868
}
6969

7070
public function testGetControllerWithObjectAndMethodAsString()
7171
{
7272
$resolver = $this->createControllerResolver();
7373

7474
$request = Request::create('/');
75-
$request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::controllerMethod1');
75+
$request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::controllerMethod');
7676
$controller = $resolver->getController($request);
7777
$this->assertInstanceOf('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', $controller[0], '->getController() returns a PHP callable');
7878
}
@@ -132,67 +132,6 @@ public function getUndefinedControllers()
132132
);
133133
}
134134

135-
public function testGetArguments()
136-
{
137-
$resolver = $this->createControllerResolver();
138-
139-
$request = Request::create('/');
140-
$controller = array(new self(), 'testGetArguments');
141-
$this->assertEquals(array(), $resolver->getArguments($request, $controller), '->getArguments() returns an empty array if the method takes no arguments');
142-
143-
$request = Request::create('/');
144-
$request->attributes->set('foo', 'foo');
145-
$controller = array(new self(), 'controllerMethod1');
146-
$this->assertEquals(array('foo'), $resolver->getArguments($request, $controller), '->getArguments() returns an array of arguments for the controller method');
147-
148-
$request = Request::create('/');
149-
$request->attributes->set('foo', 'foo');
150-
$controller = array(new self(), 'controllerMethod2');
151-
$this->assertEquals(array('foo', null), $resolver->getArguments($request, $controller), '->getArguments() uses default values if present');
152-
153-
$request->attributes->set('bar', 'bar');
154-
$this->assertEquals(array('foo', 'bar'), $resolver->getArguments($request, $controller), '->getArguments() overrides default values if provided in the request attributes');
155-
156-
$request = Request::create('/');
157-
$request->attributes->set('foo', 'foo');
158-
$controller = function ($foo) {};
159-
$this->assertEquals(array('foo'), $resolver->getArguments($request, $controller));
160-
161-
$request = Request::create('/');
162-
$request->attributes->set('foo', 'foo');
163-
$controller = function ($foo, $bar = 'bar') {};
164-
$this->assertEquals(array('foo', 'bar'), $resolver->getArguments($request, $controller));
165-
166-
$request = Request::create('/');
167-
$request->attributes->set('foo', 'foo');
168-
$controller = new self();
169-
$this->assertEquals(array('foo', null), $resolver->getArguments($request, $controller));
170-
$request->attributes->set('bar', 'bar');
171-
$this->assertEquals(array('foo', 'bar'), $resolver->getArguments($request, $controller));
172-
173-
$request = Request::create('/');
174-
$request->attributes->set('foo', 'foo');
175-
$request->attributes->set('foobar', 'foobar');
176-
$controller = 'Symfony\Component\HttpKernel\Tests\Controller\some_controller_function';
177-
$this->assertEquals(array('foo', 'foobar'), $resolver->getArguments($request, $controller));
178-
179-
$request = Request::create('/');
180-
$request->attributes->set('foo', 'foo');
181-
$request->attributes->set('foobar', 'foobar');
182-
$controller = array(new self(), 'controllerMethod3');
183-
184-
try {
185-
$resolver->getArguments($request, $controller);
186-
$this->fail('->getArguments() throws a \RuntimeException exception if it cannot determine the argument value');
187-
} catch (\Exception $e) {
188-
$this->assertInstanceOf('\RuntimeException', $e, '->getArguments() throws a \RuntimeException exception if it cannot determine the argument value');
189-
}
190-
191-
$request = Request::create('/');
192-
$controller = array(new self(), 'controllerMethod5');
193-
$this->assertEquals(array($request), $resolver->getArguments($request, $controller), '->getArguments() injects the request');
194-
}
195-
196135
public function testCreateControllerCanReturnAnyCallable()
197136
{
198137
$mock = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolver', array('createController'));
@@ -212,23 +151,11 @@ public function __invoke($foo, $bar = null)
212151
{
213152
}
214153

215-
public function controllerMethod1($foo)
216-
{
217-
}
218-
219-
protected function controllerMethod2($foo, $bar = null)
220-
{
221-
}
222-
223-
protected function controllerMethod3($foo, $bar = null, $foobar)
224-
{
225-
}
226-
227-
protected static function controllerMethod4()
154+
public function controllerMethod($foo)
228155
{
229156
}
230157

231-
protected function controllerMethod5(Request $request)
158+
protected static function staticControllerMethod()
232159
{
233160
}
234161
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy