Skip to content

Commit 8ec5c36

Browse files
committed
Update tests for Buzz 0.10, make tests smaller & using providers
1 parent 0669d73 commit 8ec5c36

File tree

4 files changed

+123
-62
lines changed

4 files changed

+123
-62
lines changed

tests/Knp/PiwikClient/ClientTest.php

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,24 @@
66

77
class ClientTest extends \PHPUnit_Framework_TestCase
88
{
9+
10+
11+
/* TEST GET CONNECTION
12+
*************************************************************************/
913
public function testConnection()
1014
{
1115
$client = new Client($con = $this->getConnectionMock());
1216

1317
$this->assertSame($con, $client->getConnection());
1418
}
1519

16-
public function testCall()
20+
21+
/* TEST CALL FORMAT JSON
22+
*************************************************************************/
23+
/**
24+
* @dataProvider providerCall
25+
*/
26+
public function testCall($assertResponse, $responseText, $format)
1727
{
1828
$client = new Client($con = $this->getConnectionMock(), '123');
1929

@@ -24,30 +34,28 @@ public function testCall()
2434
'method' => 'API.getReportMetadata',
2535
'token_auth'=> '123',
2636
'idSites' => array(2, 3),
27-
'format' => 'json'
37+
'format' => $format
2838
))
29-
->will($this->returnValue($ret = 'some ret text'));
39+
->will($this->returnValue($responseText));
3040

31-
$this->assertEquals($ret, $client->call('API.getReportMetadata', array('idSites' => array(2, 3)), 'json'));
41+
$actualResponse = $client->call('API.getReportMetadata', array('idSites' => array(2, 3)), $format);
42+
$this->assertEquals($assertResponse, $actualResponse);
3243
}
3344

34-
public function testPhpCall()
45+
public function providerCall()
3546
{
36-
$client = new Client($con = $this->getConnectionMock(), '123');
37-
38-
$con
39-
->expects($this->once())
40-
->method('send')
41-
->with(array(
42-
'method' => 'API.getReportMetadata',
43-
'token_auth'=> '123',
44-
'format' => 'php'
45-
))
46-
->will($this->returnValue(serialize($ret = array('1st' => 1, '2nd' => 2))));
47-
48-
$this->assertEquals($ret, $client->call('API.getReportMetadata'));
47+
$assertResponse = array('1st' => 1, '2nd' => 'string');
48+
return array(
49+
// Unserializing expected with php format
50+
array($assertResponse, serialize($assertResponse), 'php'),
51+
// No treatment expected with other format
52+
array($assertResponse, $assertResponse, 'json')
53+
);
4954
}
5055

56+
57+
/* UTILS
58+
*************************************************************************/
5159
protected function getConnectionMock()
5260
{
5361
return $this->getMockBuilder('Knp\PiwikClient\Connection\HttpConnection')

tests/Knp/PiwikClient/Connection/HttpConnectionTest.php

Lines changed: 91 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,103 @@
77

88
class HttpConnectionTest extends \PHPUnit_Framework_TestCase
99
{
10-
public function testSend()
10+
11+
12+
/* ATTRIBUTES
13+
*************************************************************************/
14+
private $browser;
15+
private $httpConnection;
16+
private $domain = 'http://example.com';
17+
18+
19+
/* UTILS
20+
*************************************************************************/
21+
protected function setUp()
1122
{
12-
$browser = new Buzz\Browser(new Buzz\Client\Mock\LIFO());
13-
$con = new HttpConnection('http://example.com', $browser);
14-
15-
$browser->getClient()->sendToQueue($this->createResponse($resp = serialize(array(
16-
'key1' => 'val1'
17-
))));
18-
$this->assertEquals($resp, $con->send(array('method' => 'VisitsSummary.getVisits', 'format' => 'php')));
19-
20-
$request = $browser->getJournal()->getLastRequest();
21-
$this->assertEquals(
22-
'http://example.com/?method=VisitsSummary.getVisits&format=php&module=API',
23-
$request->getUrl()
24-
);
23+
$this->browser = $this->getMockBuilder('Buzz\\Browser')
24+
->disableOriginalConstructor()
25+
->getMock();
26+
$this->httpConnection = new HttpConnection($this->domain, $this->browser);
27+
}
28+
29+
protected function createResponse($content='')
30+
{
31+
$response = new Buzz\Message\Response();
32+
$response->addHeader('1.0 200 OK');
33+
$response->setContent($content);
34+
return $response;
35+
}
36+
37+
38+
/* TEST SEND URL
39+
*************************************************************************/
40+
/**
41+
* @dataProvider providerSendUrl
42+
*/
43+
public function testSendUrl($params, $url)
44+
{
45+
$response = $this->createResponse();
46+
47+
$this->browser
48+
->expects($this->once())
49+
->method('get')
50+
->with($url)
51+
->will($this->returnValue($response));
52+
53+
$this->httpConnection->send($params);
54+
}
2555

26-
$browser->getClient()->sendToQueue($this->createResponse($resp = serialize(array(
27-
'key1' => 'val1',
28-
'key2' => 'val2'
29-
))));
30-
$this->assertEquals($resp, $con->send(array(
31-
'method' => 'Actions.getOutlinks',
32-
'idSite' => 2,
33-
'period' => 'week',
34-
'bool1' => true,
35-
'bool2' => false,
36-
'date' => new \DateTime('2009/03/12'),
37-
'arr' => array(2, 3, 4),
38-
'format' => 'php'
39-
)));
40-
41-
$request = $browser->getJournal()->getLastRequest();
42-
$this->assertEquals(
43-
'http://example.com/?method=Actions.getOutlinks' .
44-
'&idSite=2&period=week&bool1=1&date=2009-03-12&arr=2,3,4&format=php&module=API',
45-
$request->getUrl()
56+
public function providerSendUrl()
57+
{
58+
return array(
59+
array(
60+
array('method' => 'VisitsSummary.getVisits', 'format' => 'php'),
61+
$this->domain.'?method=VisitsSummary.getVisits&format=php&module=API'
62+
),
63+
array(
64+
array(
65+
'method' => 'Actions.getOutlinks',
66+
'idSite' => 2,
67+
'period' => 'week',
68+
'bool1' => true,
69+
'bool2' => false,
70+
'date' => new \DateTime('2009/03/12', new \DateTimeZone('Europe/Paris')),
71+
'arr' => array(2, 3, 4),
72+
'format' => 'php'
73+
),
74+
$this->domain.'?method=Actions.getOutlinks&idSite=2&period=week&bool1=1&date=2009-03-12&arr=2,3,4&format=php&module=API'
75+
)
4676
);
4777
}
4878

49-
protected function createResponse($content)
79+
80+
/* TEST SEND RESPONSE
81+
*************************************************************************/
82+
/**
83+
* @dataProvider providerSendResponse
84+
*/
85+
public function testSendResponse($responseArray)
5086
{
51-
$resp = new Buzz\Message\Response();
52-
$resp->fromString("HTTP/1.1 200 OK\n\n" . $content);
87+
$assertResponse = serialize($responseArray);
88+
$response = $this->createResponse($assertResponse);
5389

54-
return $resp;
90+
$this->browser
91+
->expects($this->once())
92+
->method('get')
93+
->will($this->returnValue($response));
94+
95+
$actualResponse = $this->httpConnection->send();
96+
$this->assertEquals($assertResponse, $actualResponse);
97+
}
98+
99+
public function providerSendResponse()
100+
{
101+
return array(
102+
array('key1' => 'val1'),
103+
array(
104+
'key1' => 'val1',
105+
'key2' => 'val2'
106+
)
107+
);
55108
}
56109
}

tests/Knp/PiwikClient/Connection/StubConnectionTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
class StubConnectionTest extends \PHPUnit_Framework_TestCase
88
{
9-
public function testMethodCalls()
9+
10+
11+
/* TEST SEND
12+
*************************************************************************/
13+
public function testSend()
1014
{
1115
$con = new StubConnection();
1216
$con->addResponse($resp = 'some resp');

tests/bootstrap.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,4 @@
88
* file that was distributed with this source code.
99
*/
1010

11-
if (is_file(__DIR__.'/../autoload.php')) {
12-
require_once __DIR__ . '/../autoload.php';
13-
} else {
14-
require_once __DIR__ . '/../autoload.php.dist';
15-
}
11+
require_once __DIR__ . '/../vendor/autoload.php';

0 commit comments

Comments
 (0)