Skip to content

Commit 0dc6253

Browse files
committed
do not use PHPUnit mock objects without configured expectations
1 parent ea50a13 commit 0dc6253

File tree

6 files changed

+91
-59
lines changed

6 files changed

+91
-59
lines changed

Tests/Loader/PhpFileLoaderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Config\FileLocator;
16+
use Symfony\Component\Config\FileLocatorInterface;
1617
use Symfony\Component\Config\Loader\LoaderResolver;
1718
use Symfony\Component\Config\Resource\FileResource;
1819
use Symfony\Component\Routing\Loader\AttributeClassLoader;
@@ -26,7 +27,7 @@ class PhpFileLoaderTest extends TestCase
2627
{
2728
public function testSupports()
2829
{
29-
$loader = new PhpFileLoader($this->createMock(FileLocator::class));
30+
$loader = new PhpFileLoader($this->createStub(FileLocatorInterface::class));
3031

3132
$this->assertTrue($loader->supports('foo.php'), '->supports() returns true if the resource is loadable');
3233
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');

Tests/Loader/XmlFileLoaderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Config\FileLocator;
16+
use Symfony\Component\Config\FileLocatorInterface;
1617
use Symfony\Component\Config\Loader\LoaderResolver;
1718
use Symfony\Component\Config\Resource\FileResource;
1819
use Symfony\Component\Routing\Loader\AttributeClassLoader;
@@ -27,7 +28,7 @@ class XmlFileLoaderTest extends TestCase
2728
{
2829
public function testSupports()
2930
{
30-
$loader = new XmlFileLoader($this->createMock(FileLocator::class));
31+
$loader = new XmlFileLoader($this->createStub(FileLocatorInterface::class));
3132

3233
$this->assertTrue($loader->supports('foo.xml'), '->supports() returns true if the resource is loadable');
3334
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');

Tests/Loader/YamlFileLoaderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Config\FileLocator;
16+
use Symfony\Component\Config\FileLocatorInterface;
1617
use Symfony\Component\Config\Loader\LoaderResolver;
1718
use Symfony\Component\Config\Resource\FileResource;
1819
use Symfony\Component\Routing\Loader\AttributeClassLoader;
@@ -26,7 +27,7 @@ class YamlFileLoaderTest extends TestCase
2627
{
2728
public function testSupports()
2829
{
29-
$loader = new YamlFileLoader($this->createMock(FileLocator::class));
30+
$loader = new YamlFileLoader($this->createStub(FileLocatorInterface::class));
3031

3132
$this->assertTrue($loader->supports('foo.yml'), '->supports() returns true if the resource is loadable');
3233
$this->assertTrue($loader->supports('foo.yaml'), '->supports() returns true if the resource is loadable');

Tests/Matcher/CompiledRedirectableUrlMatcherTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@
1919

2020
class CompiledRedirectableUrlMatcherTest extends RedirectableUrlMatcherTest
2121
{
22-
protected function getUrlMatcher(RouteCollection $routes, ?RequestContext $context = null)
22+
protected function getUrlMatcher(RouteCollection $routes, ?RequestContext $context = null, bool $mock = false)
2323
{
2424
$dumper = new CompiledUrlMatcherDumper($routes);
2525
$compiledRoutes = $dumper->getCompiledRoutes();
2626

27+
if (!$mock) {
28+
return new TestCompiledRedirectableUrlMatcher($compiledRoutes, $context ?? new RequestContext());
29+
}
30+
2731
return $this->getMockBuilder(TestCompiledRedirectableUrlMatcher::class)
2832
->setConstructorArgs([$compiledRoutes, $context ?? new RequestContext()])
2933
->onlyMethods(['redirect'])

Tests/Matcher/RedirectableUrlMatcherTest.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function testMissingTrailingSlash()
2424
$coll = new RouteCollection();
2525
$coll->add('foo', new Route('/foo/'));
2626

27-
$matcher = $this->getUrlMatcher($coll);
27+
$matcher = $this->getUrlMatcher($coll, null, true);
2828
$matcher->expects($this->once())->method('redirect')->willReturn([]);
2929
$matcher->match('/foo');
3030
}
@@ -34,7 +34,7 @@ public function testExtraTrailingSlash()
3434
$coll = new RouteCollection();
3535
$coll->add('foo', new Route('/foo'));
3636

37-
$matcher = $this->getUrlMatcher($coll);
37+
$matcher = $this->getUrlMatcher($coll, null, true);
3838
$matcher->expects($this->once())->method('redirect')->willReturn([]);
3939
$matcher->match('/foo/');
4040
}
@@ -56,7 +56,7 @@ public function testSchemeRedirectRedirectsToFirstScheme()
5656
$coll = new RouteCollection();
5757
$coll->add('foo', new Route('/foo', [], [], [], '', ['FTP', 'HTTPS']));
5858

59-
$matcher = $this->getUrlMatcher($coll);
59+
$matcher = $this->getUrlMatcher($coll, null, true);
6060
$matcher
6161
->expects($this->once())
6262
->method('redirect')
@@ -71,7 +71,7 @@ public function testNoSchemaRedirectIfOneOfMultipleSchemesMatches()
7171
$coll = new RouteCollection();
7272
$coll->add('foo', new Route('/foo', [], [], [], '', ['https', 'http']));
7373

74-
$matcher = $this->getUrlMatcher($coll);
74+
$matcher = $this->getUrlMatcher($coll, null, true);
7575
$matcher
7676
->expects($this->never())
7777
->method('redirect');
@@ -83,7 +83,7 @@ public function testSchemeRedirectWithParams()
8383
$coll = new RouteCollection();
8484
$coll->add('foo', new Route('/foo/{bar}', [], [], [], '', ['https']));
8585

86-
$matcher = $this->getUrlMatcher($coll);
86+
$matcher = $this->getUrlMatcher($coll, null, true);
8787
$matcher
8888
->expects($this->once())
8989
->method('redirect')
@@ -98,7 +98,7 @@ public function testSchemeRedirectForRoot()
9898
$coll = new RouteCollection();
9999
$coll->add('foo', new Route('/', [], [], [], '', ['https']));
100100

101-
$matcher = $this->getUrlMatcher($coll);
101+
$matcher = $this->getUrlMatcher($coll, null, true);
102102
$matcher
103103
->expects($this->once())
104104
->method('redirect')
@@ -112,7 +112,7 @@ public function testSlashRedirectWithParams()
112112
$coll = new RouteCollection();
113113
$coll->add('foo', new Route('/foo/{bar}/'));
114114

115-
$matcher = $this->getUrlMatcher($coll);
115+
$matcher = $this->getUrlMatcher($coll, null, true);
116116
$matcher
117117
->expects($this->once())
118118
->method('redirect')
@@ -127,7 +127,7 @@ public function testRedirectPreservesUrlEncoding()
127127
$coll = new RouteCollection();
128128
$coll->add('foo', new Route('/foo:bar/'));
129129

130-
$matcher = $this->getUrlMatcher($coll);
130+
$matcher = $this->getUrlMatcher($coll, null, true);
131131
$matcher->expects($this->once())->method('redirect')->with('/foo%3Abar/')->willReturn([]);
132132
$matcher->match('/foo%3Abar');
133133
}
@@ -136,7 +136,7 @@ public function testSchemeRequirement()
136136
{
137137
$coll = new RouteCollection();
138138
$coll->add('foo', new Route('/foo', [], [], [], '', ['https']));
139-
$matcher = $this->getUrlMatcher($coll, new RequestContext());
139+
$matcher = $this->getUrlMatcher($coll, new RequestContext(), true);
140140
$matcher->expects($this->once())->method('redirect')->with('/foo', 'foo', 'https')->willReturn([]);
141141
$this->assertSame(['_route' => 'foo'], $matcher->match('/foo'));
142142
}
@@ -147,15 +147,15 @@ public function testFallbackPage()
147147
$coll->add('foo', new Route('/foo/'));
148148
$coll->add('bar', new Route('/{name}'));
149149

150-
$matcher = $this->getUrlMatcher($coll);
150+
$matcher = $this->getUrlMatcher($coll, null, true);
151151
$matcher->expects($this->once())->method('redirect')->with('/foo/', 'foo')->willReturn(['_route' => 'foo']);
152152
$this->assertSame(['_route' => 'foo'], $matcher->match('/foo'));
153153

154154
$coll = new RouteCollection();
155155
$coll->add('foo', new Route('/foo'));
156156
$coll->add('bar', new Route('/{name}/'));
157157

158-
$matcher = $this->getUrlMatcher($coll);
158+
$matcher = $this->getUrlMatcher($coll, null, true);
159159
$matcher->expects($this->once())->method('redirect')->with('/foo', 'foo')->willReturn(['_route' => 'foo']);
160160
$this->assertSame(['_route' => 'foo'], $matcher->match('/foo/'));
161161
}
@@ -165,7 +165,7 @@ public function testMissingTrailingSlashAndScheme()
165165
$coll = new RouteCollection();
166166
$coll->add('foo', (new Route('/foo/'))->setSchemes(['https']));
167167

168-
$matcher = $this->getUrlMatcher($coll);
168+
$matcher = $this->getUrlMatcher($coll, null, true);
169169
$matcher->expects($this->once())->method('redirect')->with('/foo/', 'foo', 'https')->willReturn([]);
170170
$matcher->match('/foo');
171171
}
@@ -176,7 +176,7 @@ public function testSlashAndVerbPrecedenceWithRedirection()
176176
$coll->add('a', new Route('/api/customers/{customerId}/contactpersons', [], [], [], '', [], ['post']));
177177
$coll->add('b', new Route('/api/customers/{customerId}/contactpersons/', [], [], [], '', [], ['get']));
178178

179-
$matcher = $this->getUrlMatcher($coll);
179+
$matcher = $this->getUrlMatcher($coll, null, true);
180180
$expected = [
181181
'_route' => 'b',
182182
'customerId' => '123',
@@ -192,7 +192,7 @@ public function testNonGreedyTrailingRequirement()
192192
$coll = new RouteCollection();
193193
$coll->add('a', new Route('/{a}', [], ['a' => '\d+']));
194194

195-
$matcher = $this->getUrlMatcher($coll);
195+
$matcher = $this->getUrlMatcher($coll, null, true);
196196
$matcher->expects($this->once())->method('redirect')->with('/123')->willReturn([]);
197197

198198
$this->assertEquals(['_route' => 'a', 'a' => '123'], $matcher->match('/123/'));
@@ -203,14 +203,23 @@ public function testTrailingRequirementWithDefaultA()
203203
$coll = new RouteCollection();
204204
$coll->add('a', new Route('/fr-fr/{a}', ['a' => 'aaa'], ['a' => '.+']));
205205

206-
$matcher = $this->getUrlMatcher($coll);
206+
$matcher = $this->getUrlMatcher($coll, null, true);
207207
$matcher->expects($this->once())->method('redirect')->with('/fr-fr')->willReturn([]);
208208

209209
$this->assertEquals(['_route' => 'a', 'a' => 'aaa'], $matcher->match('/fr-fr/'));
210210
}
211211

212-
protected function getUrlMatcher(RouteCollection $routes, ?RequestContext $context = null)
212+
protected function getUrlMatcher(RouteCollection $routes, ?RequestContext $context = null, bool $mock = false)
213213
{
214+
if (!$mock) {
215+
return new class($routes, $context ?? new RequestContext()) extends RedirectableUrlMatcher {
216+
public function redirect(string $path, string $route, ?string $scheme = null): array
217+
{
218+
return [];
219+
}
220+
};
221+
}
222+
214223
return $this->getMockBuilder(RedirectableUrlMatcher::class)
215224
->setConstructorArgs([$routes, $context ?? new RequestContext()])
216225
->onlyMethods(['redirect'])

0 commit comments

Comments
 (0)