From d8e1d3ad4bee73273a6e40a2e10f31ac90b55522 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Thu, 21 Jul 2016 16:18:26 +0200 Subject: [PATCH 1/5] Added test for discovery strategy --- .../ConfiguredClientsStrategyTest.php | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Tests/Unit/Discovery/ConfiguredClientsStrategyTest.php diff --git a/Tests/Unit/Discovery/ConfiguredClientsStrategyTest.php b/Tests/Unit/Discovery/ConfiguredClientsStrategyTest.php new file mode 100644 index 00000000..6dea80bf --- /dev/null +++ b/Tests/Unit/Discovery/ConfiguredClientsStrategyTest.php @@ -0,0 +1,63 @@ +getMock(HttpClient::class); + $httpAsyncClient = $this->getMock(HttpAsyncClient::class); + $strategy = new ConfiguredClientsStrategy($httpClient, $httpAsyncClient); + + $candidates = $strategy::getCandidates(HttpClient::class); + $candidate = array_shift($candidates); + $this->assertEquals($httpClient, $candidate['class']()); + + $candidates = $strategy::getCandidates(HttpAsyncClient::class); + $candidate = array_shift($candidates); + $this->assertEquals($httpAsyncClient, $candidate['class']()); + } + + public function testGetCandidatesEmpty() + { + $strategy = new ConfiguredClientsStrategy(null, null); + + $candidates = $strategy::getCandidates(HttpClient::class); + $this->assertEquals([], $candidates); + + $candidates = $strategy::getCandidates(HttpAsyncClient::class); + $this->assertEquals([], $candidates); + } + + public function testGetCandidatesEmptyAsync() + { + $httpClient = $this->getMock(HttpClient::class); + $strategy = new ConfiguredClientsStrategy($httpClient, null); + + $candidates = $strategy::getCandidates(HttpClient::class); + $candidate = array_shift($candidates); + $this->assertEquals($httpClient, $candidate['class']()); + + $candidates = $strategy::getCandidates(HttpAsyncClient::class); + $this->assertEquals([], $candidates); + } + + + public function testGetCandidatesEmptySync() + { + $httpAsyncClient = $this->getMock(HttpAsyncClient::class); + $strategy = new ConfiguredClientsStrategy(null, $httpAsyncClient); + + $candidates = $strategy::getCandidates(HttpClient::class); + $this->assertEquals([], $candidates); + + $candidates = $strategy::getCandidates(HttpAsyncClient::class); + $candidate = array_shift($candidates); + $this->assertEquals($httpAsyncClient, $candidate['class']()); + } +} From 9b89ecf66e4f1c5a7972f158c8fa9381c281056d Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Thu, 21 Jul 2016 16:18:41 +0200 Subject: [PATCH 2/5] Added test for no-debug --- Tests/Functional/ServiceInstantiationTest.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Tests/Functional/ServiceInstantiationTest.php b/Tests/Functional/ServiceInstantiationTest.php index ebbfff42..0843cc57 100644 --- a/Tests/Functional/ServiceInstantiationTest.php +++ b/Tests/Functional/ServiceInstantiationTest.php @@ -6,13 +6,18 @@ class ServiceInstantiationTest extends WebTestCase { - public static function setUpBeforeClass() + public function testHttpClient() { static::bootKernel(); + $container = static::$kernel->getContainer(); + $this->assertTrue($container->has('httplug.client')); + $client = $container->get('httplug.client'); + $this->assertInstanceOf('Http\Client\HttpClient', $client); } - public function testHttpClient() + public function testHttpClientNoDebug() { + static::bootKernel(['debug'=>false]); $container = static::$kernel->getContainer(); $this->assertTrue($container->has('httplug.client')); $client = $container->get('httplug.client'); From 8dc7714256870a21aeb1a3d38bcd2519a1339080 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Thu, 21 Jul 2016 17:03:12 +0200 Subject: [PATCH 3/5] Test if profiling is enabled when it should be --- .../HttplugExtensionTest.php | 75 +++++++++++++++++++ composer.json | 2 +- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/Tests/Unit/DependencyInjection/HttplugExtensionTest.php b/Tests/Unit/DependencyInjection/HttplugExtensionTest.php index b88d795b..f509a245 100644 --- a/Tests/Unit/DependencyInjection/HttplugExtensionTest.php +++ b/Tests/Unit/DependencyInjection/HttplugExtensionTest.php @@ -7,6 +7,7 @@ /** * @author David Buchmann + * @author Tobias Nyholm */ class HttplugExtensionTest extends AbstractExtensionTestCase { @@ -52,4 +53,78 @@ public function testConfigLoadService() $this->assertContainerBuilderHasAlias("httplug.$type", "my_{$type}_service"); } } + + public function testNoProfilingWhenToolbarIsDisabled() + { + $this->load( + [ + 'toolbar' => [ + 'enabled' => false, + ], + 'clients' => [ + 'acme' => [ + 'factory' => 'httplug.factory.curl', + 'plugins' => ['foo'] + ], + ], + ] + ); + + $this->verifyProfilingDisabled(); + } + + public function testNoProfilingWhenNotInDebugMode() + { + $this->setParameter('kernel.debug', false); + $this->load( + [ + 'clients' => [ + 'acme' => [ + 'factory' => 'httplug.factory.curl', + 'plugins' => ['foo'] + ], + ], + ] + ); + + $this->verifyProfilingDisabled(); + } + public function testProfilingWhenToolbarIsSpecificallyOn() + { + $this->setParameter('kernel.debug', false); + $this->load( + [ + 'toolbar' => [ + 'enabled' => true, + ], + 'clients' => [ + 'acme' => [ + 'factory' => 'httplug.factory.curl', + 'plugins' => ['foo'] + ], + ], + ] + ); + + $def = $this->container->findDefinition('httplug.client'); + $arguments = $def->getArguments(); + + $this->assertTrue(isset($arguments[3])); + $this->assertTrue(isset($arguments[3]['debug_plugins'])); + $this->assertFalse(empty($arguments[3]['debug_plugins'])); + } + + private function verifyProfilingDisabled() + { + $def = $this->container->findDefinition('httplug.client'); + $arguments = $def->getArguments(); + + if (isset($arguments[3])) { + $this->assertEmpty( + $arguments[3], + 'Parameter 3 to the PluginClient must not contain any debug_plugin information when profiling is disabled' + ); + } + } + } diff --git a/composer.json b/composer.json index 0808a51f..3fea2308 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "php-http/buzz-adapter": "^0.3", "symfony/symfony": "^2.7 || ^3.0", "polishsymfonycommunity/symfony-mocker-container": "^1.0", - "matthiasnoback/symfony-dependency-injection-test": "^0.7" + "matthiasnoback/symfony-dependency-injection-test": "^1.0" }, "conflict": { "php-http/guzzle6-adapter": "<1.1" From 5f90891a6b9fcde5bc712fb77ec416a81fa2d991 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Thu, 21 Jul 2016 17:11:01 +0200 Subject: [PATCH 4/5] Allow new phpunit --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3fea2308..0ca830a4 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "twig/twig": "^1.18 || ^2.0" }, "require-dev": { - "phpunit/phpunit": "^4.5", + "phpunit/phpunit": "^4.5 || ^5.4", "php-http/curl-client": "^1.0", "php-http/socket-client": "^1.0", "php-http/guzzle6-adapter": "^1.1.1", From 36600c80a96f711955a3b45225dae6f8af26ffb7 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Thu, 21 Jul 2016 17:13:30 +0200 Subject: [PATCH 5/5] Style fix --- Tests/Functional/ServiceInstantiationTest.php | 2 +- Tests/Unit/DependencyInjection/HttplugExtensionTest.php | 8 ++++---- Tests/Unit/Discovery/ConfiguredClientsStrategyTest.php | 9 ++++----- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Tests/Functional/ServiceInstantiationTest.php b/Tests/Functional/ServiceInstantiationTest.php index 0843cc57..1bd5abd9 100644 --- a/Tests/Functional/ServiceInstantiationTest.php +++ b/Tests/Functional/ServiceInstantiationTest.php @@ -17,7 +17,7 @@ public function testHttpClient() public function testHttpClientNoDebug() { - static::bootKernel(['debug'=>false]); + static::bootKernel(['debug' => false]); $container = static::$kernel->getContainer(); $this->assertTrue($container->has('httplug.client')); $client = $container->get('httplug.client'); diff --git a/Tests/Unit/DependencyInjection/HttplugExtensionTest.php b/Tests/Unit/DependencyInjection/HttplugExtensionTest.php index f509a245..54a2bd8f 100644 --- a/Tests/Unit/DependencyInjection/HttplugExtensionTest.php +++ b/Tests/Unit/DependencyInjection/HttplugExtensionTest.php @@ -64,7 +64,7 @@ public function testNoProfilingWhenToolbarIsDisabled() 'clients' => [ 'acme' => [ 'factory' => 'httplug.factory.curl', - 'plugins' => ['foo'] + 'plugins' => ['foo'], ], ], ] @@ -81,7 +81,7 @@ public function testNoProfilingWhenNotInDebugMode() 'clients' => [ 'acme' => [ 'factory' => 'httplug.factory.curl', - 'plugins' => ['foo'] + 'plugins' => ['foo'], ], ], ] @@ -89,6 +89,7 @@ public function testNoProfilingWhenNotInDebugMode() $this->verifyProfilingDisabled(); } + public function testProfilingWhenToolbarIsSpecificallyOn() { $this->setParameter('kernel.debug', false); @@ -100,7 +101,7 @@ public function testProfilingWhenToolbarIsSpecificallyOn() 'clients' => [ 'acme' => [ 'factory' => 'httplug.factory.curl', - 'plugins' => ['foo'] + 'plugins' => ['foo'], ], ], ] @@ -126,5 +127,4 @@ private function verifyProfilingDisabled() ); } } - } diff --git a/Tests/Unit/Discovery/ConfiguredClientsStrategyTest.php b/Tests/Unit/Discovery/ConfiguredClientsStrategyTest.php index 6dea80bf..ecf3dbc7 100644 --- a/Tests/Unit/Discovery/ConfiguredClientsStrategyTest.php +++ b/Tests/Unit/Discovery/ConfiguredClientsStrategyTest.php @@ -12,7 +12,7 @@ public function testGetCandidates() { $httpClient = $this->getMock(HttpClient::class); $httpAsyncClient = $this->getMock(HttpAsyncClient::class); - $strategy = new ConfiguredClientsStrategy($httpClient, $httpAsyncClient); + $strategy = new ConfiguredClientsStrategy($httpClient, $httpAsyncClient); $candidates = $strategy::getCandidates(HttpClient::class); $candidate = array_shift($candidates); @@ -25,7 +25,7 @@ public function testGetCandidates() public function testGetCandidatesEmpty() { - $strategy = new ConfiguredClientsStrategy(null, null); + $strategy = new ConfiguredClientsStrategy(null, null); $candidates = $strategy::getCandidates(HttpClient::class); $this->assertEquals([], $candidates); @@ -37,7 +37,7 @@ public function testGetCandidatesEmpty() public function testGetCandidatesEmptyAsync() { $httpClient = $this->getMock(HttpClient::class); - $strategy = new ConfiguredClientsStrategy($httpClient, null); + $strategy = new ConfiguredClientsStrategy($httpClient, null); $candidates = $strategy::getCandidates(HttpClient::class); $candidate = array_shift($candidates); @@ -47,11 +47,10 @@ public function testGetCandidatesEmptyAsync() $this->assertEquals([], $candidates); } - public function testGetCandidatesEmptySync() { $httpAsyncClient = $this->getMock(HttpAsyncClient::class); - $strategy = new ConfiguredClientsStrategy(null, $httpAsyncClient); + $strategy = new ConfiguredClientsStrategy(null, $httpAsyncClient); $candidates = $strategy::getCandidates(HttpClient::class); $this->assertEquals([], $candidates);