diff options
author | Arnold Daniels <arnold@jasny.net> | 2017-02-09 15:06:41 +0100 |
---|---|---|
committer | Arnold Daniels <arnold@jasny.net> | 2017-02-09 15:06:41 +0100 |
commit | c6cdff29b1a56ed530f93628d18d5ab44074ae98 (patch) | |
tree | 31c199ff91436594acc35c6484798c6319715641 | |
parent | b3628e20a477ea0cc132f752378866a4172dbd93 (diff) | |
download | controller-c6cdff29b1a56ed530f93628d18d5ab44074ae98.zip controller-c6cdff29b1a56ed530f93628d18d5ab44074ae98.tar.gz controller-c6cdff29b1a56ed530f93628d18d5ab44074ae98.tar.bz2 |
Added test for ContentNegotiation::getNegotiator()
-rw-r--r-- | tests/Controller/ContentNegotiationTest.php | 108 |
1 files changed, 69 insertions, 39 deletions
diff --git a/tests/Controller/ContentNegotiationTest.php b/tests/Controller/ContentNegotiationTest.php index 4775306..f760bcd 100644 --- a/tests/Controller/ContentNegotiationTest.php +++ b/tests/Controller/ContentNegotiationTest.php @@ -5,7 +5,7 @@ namespace Jasny\Controller; use Jasny\Controller\ContentNegotiation; use Psr\Http\Message\ServerRequestInterface; use Jasny\Controller\TestHelper; -use Negotiation\Negotiator; +use Negotiation; use Negotiation\BaseAccept; /** @@ -16,36 +16,6 @@ class ContentNegotiationTest extends \PHPUnit_Framework_TestCase use TestHelper; /** - * Test negotiation - * - * @dataProvider negotiateProvider - * @param string $result - * @param array $header - * @param array $priorities - */ - public function testNegotiate($method, $negotiatorClass, $type, $expected, $headerName, array $headerValue, array $priorities) - { - $request = $this->createMock(ServerRequestInterface::class); - $request->expects($this->once())->method('getHeader')->with($this->equalTo($headerName))->will($this->returnValue($headerValue)); - - $expectedObj = $this->createMock(BaseAccept::class); - $expectedObj->expects($this->once())->method('getType')->will($this->returnValue($expected)); - - $negotiator = $this->createMock($negotiatorClass); - $negotiator->expects($this->once())->method('getBest')->with($this->equalTo(join(', ', $headerValue)), $this->equalTo($priorities))->will($this->returnValue($expectedObj)); - - $trait = $this->getController(['getRequest', 'getNegotiator']); - $trait->expects($this->once())->method('getRequest')->will($this->returnValue($request)); - $trait->expects($this->once())->method('getNegotiator')->with($this->equalTo($type))->will($this->returnValue($negotiator)); - - $buildClass = $this->callPrivateMethod($trait, 'getNegotiatorName', [$type]); - $result = $trait->{$method}($priorities); - - $this->assertEquals($buildClass, $negotiatorClass, "Obtained wrong negotiator class"); - $this->assertEquals($result, $expected, "Obtained result does not match expected result"); - } - - /** * Provide data for testing negotiation * * @return array @@ -55,7 +25,7 @@ class ContentNegotiationTest extends \PHPUnit_Framework_TestCase return [ [ 'negotiateContentType', - 'Negotiation\\Negotiator', + Negotiation\Negotiator::class, '', 'text/html', 'Accept', @@ -64,7 +34,7 @@ class ContentNegotiationTest extends \PHPUnit_Framework_TestCase ], [ 'negotiateContentType', - 'Negotiation\\Negotiator', + Negotiation\Negotiator::class, '', '', 'Accept', @@ -73,7 +43,7 @@ class ContentNegotiationTest extends \PHPUnit_Framework_TestCase ], [ 'negotiateLanguage', - 'Negotiation\\LanguageNegotiator', + Negotiation\LanguageNegotiator::class, 'language', 'en', 'Accept-Language', @@ -82,7 +52,7 @@ class ContentNegotiationTest extends \PHPUnit_Framework_TestCase ], [ 'negotiateLanguage', - 'Negotiation\\LanguageNegotiator', + Negotiation\LanguageNegotiator::class, 'language', '', 'Accept-Language', @@ -91,7 +61,7 @@ class ContentNegotiationTest extends \PHPUnit_Framework_TestCase ], [ 'negotiateEncoding', - 'Negotiation\\EncodingNegotiator', + Negotiation\EncodingNegotiator::class, 'encoding', 'gzip', 'Accept-Encoding', @@ -100,7 +70,7 @@ class ContentNegotiationTest extends \PHPUnit_Framework_TestCase ], [ 'negotiateEncoding', - 'Negotiation\\EncodingNegotiator', + Negotiation\EncodingNegotiator::class, 'encoding', '', 'Accept-Encoding', @@ -109,7 +79,7 @@ class ContentNegotiationTest extends \PHPUnit_Framework_TestCase ], [ 'negotiateCharset', - 'Negotiation\\CharsetNegotiator', + Negotiation\CharsetNegotiator::class, 'charset', 'utf-8', 'Accept-Charset', @@ -118,7 +88,7 @@ class ContentNegotiationTest extends \PHPUnit_Framework_TestCase ], [ 'negotiateCharset', - 'Negotiation\\CharsetNegotiator', + Negotiation\CharsetNegotiator::class, 'charset', '', 'Accept-Charset', @@ -129,6 +99,66 @@ class ContentNegotiationTest extends \PHPUnit_Framework_TestCase } /** + * Test negotiation + * @dataProvider negotiateProvider + * + * @param string $method + * @param string $negotiatorClass + * @param string $type + * @param string $expected + * @param string $headerName + * @param array $headerValue + * @param array $priorities + */ + public function testNegotiate( + $method, + $negotiatorClass, + $type, + $expected, + $headerName, + array $headerValue, + array $priorities + ) { + $request = $this->createMock(ServerRequestInterface::class); + $request->expects($this->once())->method('getHeader')->with($this->equalTo($headerName)) + ->will($this->returnValue($headerValue)); + + $expectedObj = $this->createMock(BaseAccept::class); + $expectedObj->expects($this->once())->method('getType')->will($this->returnValue($expected)); + + $negotiator = $this->createMock($negotiatorClass); + $negotiator->expects($this->once())->method('getBest') + ->with($this->equalTo(join(', ', $headerValue)), $this->equalTo($priorities)) + ->will($this->returnValue($expectedObj)); + + $trait = $this->getController(['getRequest', 'getNegotiator']); + $trait->expects($this->once())->method('getRequest')->will($this->returnValue($request)); + $trait->expects($this->once())->method('getNegotiator')->with($this->equalTo($type)) + ->will($this->returnValue($negotiator)); + + $buildClass = $this->callPrivateMethod($trait, 'getNegotiatorName', [$type]); + $result = $trait->{$method}($priorities); + + $this->assertEquals($buildClass, $negotiatorClass, "Obtained wrong negotiator class"); + $this->assertEquals($result, $expected, "Obtained result does not match expected result"); + } + + /** + * Test negotiation + * @dataProvider negotiateProvider + * + * @param string $method + * @param string $negotiatorClass + * @param string $type + */ + public function testGetNegotiator($method, $negotiatorClass, $type) + { + $controller = $this->getController(); + + $this->assertInstanceOf($negotiatorClass, $this->callPrivateMethod($controller, 'getNegotiator', [$type])); + } + + /** * Get the controller class * * @return string |