Skip to content

Commit 119f887

Browse files
Nyholmdbu
authored andcommitted
Dev tests (#98)
More tests for the DI extension * Added test for discovery strategy * Added test for no-debug * Test if profiling is enabled when it should be
1 parent a318500 commit 119f887

File tree

4 files changed

+146
-4
lines changed

4 files changed

+146
-4
lines changed

Tests/Functional/ServiceInstantiationTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66

77
class ServiceInstantiationTest extends WebTestCase
88
{
9-
public static function setUpBeforeClass()
9+
public function testHttpClient()
1010
{
1111
static::bootKernel();
12+
$container = static::$kernel->getContainer();
13+
$this->assertTrue($container->has('httplug.client'));
14+
$client = $container->get('httplug.client');
15+
$this->assertInstanceOf('Http\Client\HttpClient', $client);
1216
}
1317

14-
public function testHttpClient()
18+
public function testHttpClientNoDebug()
1519
{
20+
static::bootKernel(['debug' => false]);
1621
$container = static::$kernel->getContainer();
1722
$this->assertTrue($container->has('httplug.client'));
1823
$client = $container->get('httplug.client');

Tests/Unit/DependencyInjection/HttplugExtensionTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
/**
99
* @author David Buchmann <mail@davidbu.ch>
10+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
1011
*/
1112
class HttplugExtensionTest extends AbstractExtensionTestCase
1213
{
@@ -52,4 +53,78 @@ public function testConfigLoadService()
5253
$this->assertContainerBuilderHasAlias("httplug.$type", "my_{$type}_service");
5354
}
5455
}
56+
57+
public function testNoProfilingWhenToolbarIsDisabled()
58+
{
59+
$this->load(
60+
[
61+
'toolbar' => [
62+
'enabled' => false,
63+
],
64+
'clients' => [
65+
'acme' => [
66+
'factory' => 'httplug.factory.curl',
67+
'plugins' => ['foo'],
68+
],
69+
],
70+
]
71+
);
72+
73+
$this->verifyProfilingDisabled();
74+
}
75+
76+
public function testNoProfilingWhenNotInDebugMode()
77+
{
78+
$this->setParameter('kernel.debug', false);
79+
$this->load(
80+
[
81+
'clients' => [
82+
'acme' => [
83+
'factory' => 'httplug.factory.curl',
84+
'plugins' => ['foo'],
85+
],
86+
],
87+
]
88+
);
89+
90+
$this->verifyProfilingDisabled();
91+
}
92+
93+
public function testProfilingWhenToolbarIsSpecificallyOn()
94+
{
95+
$this->setParameter('kernel.debug', false);
96+
$this->load(
97+
[
98+
'toolbar' => [
99+
'enabled' => true,
100+
],
101+
'clients' => [
102+
'acme' => [
103+
'factory' => 'httplug.factory.curl',
104+
'plugins' => ['foo'],
105+
],
106+
],
107+
]
108+
);
109+
110+
$def = $this->container->findDefinition('httplug.client');
111+
$arguments = $def->getArguments();
112+
113+
$this->assertTrue(isset($arguments[3]));
114+
$this->assertTrue(isset($arguments[3]['debug_plugins']));
115+
$this->assertFalse(empty($arguments[3]['debug_plugins']));
116+
}
117+
118+
private function verifyProfilingDisabled()
119+
{
120+
$def = $this->container->findDefinition('httplug.client');
121+
$arguments = $def->getArguments();
122+
123+
if (isset($arguments[3])) {
124+
$this->assertEmpty(
125+
$arguments[3],
126+
'Parameter 3 to the PluginClient must not contain any debug_plugin information when profiling is disabled'
127+
);
128+
}
129+
}
55130
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\Tests\Unit\Discovery;
4+
5+
use Http\Client\HttpAsyncClient;
6+
use Http\Client\HttpClient;
7+
use Http\HttplugBundle\Discovery\ConfiguredClientsStrategy;
8+
9+
class ConfiguredClientsStrategyTest extends \PHPUnit_Framework_TestCase
10+
{
11+
public function testGetCandidates()
12+
{
13+
$httpClient = $this->getMock(HttpClient::class);
14+
$httpAsyncClient = $this->getMock(HttpAsyncClient::class);
15+
$strategy = new ConfiguredClientsStrategy($httpClient, $httpAsyncClient);
16+
17+
$candidates = $strategy::getCandidates(HttpClient::class);
18+
$candidate = array_shift($candidates);
19+
$this->assertEquals($httpClient, $candidate['class']());
20+
21+
$candidates = $strategy::getCandidates(HttpAsyncClient::class);
22+
$candidate = array_shift($candidates);
23+
$this->assertEquals($httpAsyncClient, $candidate['class']());
24+
}
25+
26+
public function testGetCandidatesEmpty()
27+
{
28+
$strategy = new ConfiguredClientsStrategy(null, null);
29+
30+
$candidates = $strategy::getCandidates(HttpClient::class);
31+
$this->assertEquals([], $candidates);
32+
33+
$candidates = $strategy::getCandidates(HttpAsyncClient::class);
34+
$this->assertEquals([], $candidates);
35+
}
36+
37+
public function testGetCandidatesEmptyAsync()
38+
{
39+
$httpClient = $this->getMock(HttpClient::class);
40+
$strategy = new ConfiguredClientsStrategy($httpClient, null);
41+
42+
$candidates = $strategy::getCandidates(HttpClient::class);
43+
$candidate = array_shift($candidates);
44+
$this->assertEquals($httpClient, $candidate['class']());
45+
46+
$candidates = $strategy::getCandidates(HttpAsyncClient::class);
47+
$this->assertEquals([], $candidates);
48+
}
49+
50+
public function testGetCandidatesEmptySync()
51+
{
52+
$httpAsyncClient = $this->getMock(HttpAsyncClient::class);
53+
$strategy = new ConfiguredClientsStrategy(null, $httpAsyncClient);
54+
55+
$candidates = $strategy::getCandidates(HttpClient::class);
56+
$this->assertEquals([], $candidates);
57+
58+
$candidates = $strategy::getCandidates(HttpAsyncClient::class);
59+
$candidate = array_shift($candidates);
60+
$this->assertEquals($httpAsyncClient, $candidate['class']());
61+
}
62+
}

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
3131
"twig/twig": "^1.18 || ^2.0"
3232
},
3333
"require-dev": {
34-
"phpunit/phpunit": "^4.5",
34+
"phpunit/phpunit": "^4.5 || ^5.4",
3535
"php-http/curl-client": "^1.0",
3636
"php-http/socket-client": "^1.0",
3737
"php-http/guzzle6-adapter": "^1.1.1",
3838
"php-http/react-adapter": "^0.2.1",
3939
"php-http/buzz-adapter": "^0.3",
4040
"symfony/symfony": "^2.7 || ^3.0",
4141
"polishsymfonycommunity/symfony-mocker-container": "^1.0",
42-
"matthiasnoback/symfony-dependency-injection-test": "^0.7"
42+
"matthiasnoback/symfony-dependency-injection-test": "^1.0"
4343
},
4444
"conflict": {
4545
"php-http/guzzle6-adapter": "<1.1"

0 commit comments

Comments
 (0)