diff --git a/Tests/Functional/ServiceInstantiationTest.php b/Tests/Functional/ServiceInstantiationTest.php index ebbfff42..1bd5abd9 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'); diff --git a/Tests/Unit/DependencyInjection/HttplugExtensionTest.php b/Tests/Unit/DependencyInjection/HttplugExtensionTest.php index b88d795b..54a2bd8f 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/Tests/Unit/Discovery/ConfiguredClientsStrategyTest.php b/Tests/Unit/Discovery/ConfiguredClientsStrategyTest.php new file mode 100644 index 00000000..ecf3dbc7 --- /dev/null +++ b/Tests/Unit/Discovery/ConfiguredClientsStrategyTest.php @@ -0,0 +1,62 @@ +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']()); + } +} diff --git a/composer.json b/composer.json index 0808a51f..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", @@ -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"