Skip to content

Commit 69d2c8e

Browse files
committed
Merge branch '2.4'
* 2.4: [HttpKernel] fixed unit tests when using phpunit.phar add expression request matcher tests
2 parents e778cf1 + 1c02456 commit 69d2c8e

File tree

2 files changed

+90
-26
lines changed

2 files changed

+90
-26
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\Tests;
13+
14+
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
15+
use Symfony\Component\HttpFoundation\ExpressionRequestMatcher;
16+
use Symfony\Component\HttpFoundation\Request;
17+
18+
class ExpressionRequestMatcherTest extends \PHPUnit_Framework_TestCase
19+
{
20+
/**
21+
* @expectedException \LogicException
22+
*/
23+
public function testWhenNoExpressionIsSet()
24+
{
25+
$expressionRequestMatcher = new ExpressionRequestMatcher();
26+
$expressionRequestMatcher->matches(new Request());
27+
}
28+
29+
/**
30+
* @dataProvider provideExpressions
31+
*/
32+
public function testMatchesWhenParentMatchesIsTrue($expression, $expected)
33+
{
34+
$request = Request::create('/foo');
35+
$expressionRequestMatcher = new ExpressionRequestMatcher();
36+
37+
$expressionRequestMatcher->setExpression(new ExpressionLanguage(), $expression);
38+
$this->assertSame($expected, $expressionRequestMatcher->matches($request));
39+
}
40+
41+
/**
42+
* @dataProvider provideExpressions
43+
*/
44+
public function testMatchesWhenParentMatchesIsFalse($expression)
45+
{
46+
$request = Request::create('/foo');
47+
$request->attributes->set('foo', 'foo');
48+
$expressionRequestMatcher = new ExpressionRequestMatcher();
49+
$expressionRequestMatcher->matchAttribute('foo', 'bar');
50+
51+
$expressionRequestMatcher->setExpression(new ExpressionLanguage(), $expression);
52+
$this->assertFalse($expressionRequestMatcher->matches($request));
53+
}
54+
55+
public function provideExpressions()
56+
{
57+
return array(
58+
array('request.getMethod() == method', true),
59+
array('request.getPathInfo() == path', true),
60+
array('request.getHost() == host', true),
61+
array('request.getClientIp() == ip', true),
62+
array('request.attributes.all() == attributes', true),
63+
array('request.getMethod() == method && request.getPathInfo() == path && request.getHost() == host && request.getClientIp() == ip && request.attributes.all() == attributes', true),
64+
array('request.getMethod() != method', false),
65+
array('request.getMethod() != method && request.getPathInfo() == path && request.getHost() == host && request.getClientIp() == ip && request.attributes.all() == attributes', false),
66+
);
67+
}
68+
}

src/Symfony/Component/HttpKernel/Tests/KernelTest.php

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,7 @@ public function testBootSetsTheContainerToTheBundles()
7777
public function testBootSetsTheBootedFlagToTrue()
7878
{
7979
// use test kernel to access isBooted()
80-
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')
81-
->setConstructorArgs(array('test', false))
82-
->setMethods(array('initializeBundles', 'initializeContainer'))
83-
->getMock();
84-
80+
$kernel = $this->getKernelForTest(array('initializeBundles', 'initializeContainer'));
8581
$kernel->boot();
8682

8783
$this->assertTrue($kernel->isBooted());
@@ -575,12 +571,7 @@ public function testInitializeBundles()
575571
$child = $this->getBundle(null, 'ParentABundle', 'ChildABundle');
576572

577573
// use test kernel so we can access getBundleMap()
578-
$kernel = $this
579-
->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')
580-
->setMethods(array('registerBundles'))
581-
->setConstructorArgs(array('test', false))
582-
->getMock()
583-
;
574+
$kernel = $this->getKernelForTest(array('registerBundles'));
584575
$kernel
585576
->expects($this->once())
586577
->method('registerBundles')
@@ -599,18 +590,12 @@ public function testInitializeBundlesSupportInheritanceCascade()
599590
$child = $this->getBundle(null, 'ParentBBundle', 'ChildBBundle');
600591

601592
// use test kernel so we can access getBundleMap()
602-
$kernel = $this
603-
->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')
604-
->setMethods(array('registerBundles'))
605-
->setConstructorArgs(array('test', false))
606-
->getMock()
607-
;
593+
$kernel = $this->getKernelForTest(array('registerBundles'));
608594
$kernel
609595
->expects($this->once())
610596
->method('registerBundles')
611597
->will($this->returnValue(array($grandparent, $parent, $child)))
612598
;
613-
614599
$kernel->boot();
615600

616601
$map = $kernel->getBundleMap();
@@ -637,18 +622,12 @@ public function testInitializeBundlesSupportsArbitraryBundleRegistrationOrder()
637622
$child = $this->getBundle(null, 'ParentCBundle', 'ChildCBundle');
638623

639624
// use test kernel so we can access getBundleMap()
640-
$kernel = $this
641-
->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')
642-
->setMethods(array('registerBundles'))
643-
->setConstructorArgs(array('test', false))
644-
->getMock()
645-
;
625+
$kernel = $this->getKernelForTest(array('registerBundles'));
646626
$kernel
647627
->expects($this->once())
648628
->method('registerBundles')
649629
->will($this->returnValue(array($parent, $grandparent, $child)))
650630
;
651-
652631
$kernel->boot();
653632

654633
$map = $kernel->getBundleMap();
@@ -793,17 +772,34 @@ protected function getBundle($dir = null, $parent = null, $className = null, $bu
793772
*/
794773
protected function getKernel(array $methods = array(), array $bundles = array())
795774
{
775+
$methods[] = 'registerBundles';
776+
796777
$kernel = $this
797778
->getMockBuilder('Symfony\Component\HttpKernel\Kernel')
798779
->setMethods($methods)
799780
->setConstructorArgs(array('test', false))
800781
->getMockForAbstractClass()
801782
;
802-
803783
$kernel->expects($this->any())
804784
->method('registerBundles')
805785
->will($this->returnValue($bundles))
806786
;
787+
$p = new \ReflectionProperty($kernel, 'rootDir');
788+
$p->setAccessible(true);
789+
$p->setValue($kernel, __DIR__.'/Fixtures');
790+
791+
return $kernel;
792+
}
793+
794+
protected function getKernelForTest(array $methods = array())
795+
{
796+
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')
797+
->setConstructorArgs(array('test', false))
798+
->setMethods($methods)
799+
->getMock();
800+
$p = new \ReflectionProperty($kernel, 'rootDir');
801+
$p->setAccessible(true);
802+
$p->setValue($kernel, __DIR__.'/Fixtures');
807803

808804
return $kernel;
809805
}

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