|
7 | 7 |
|
8 | 8 | class HttpConnectionTest extends \PHPUnit_Framework_TestCase |
9 | 9 | { |
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() |
11 | 22 | { |
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 | + } |
25 | 55 |
|
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 | + ) |
46 | 76 | ); |
47 | 77 | } |
48 | 78 |
|
49 | | - protected function createResponse($content) |
| 79 | + |
| 80 | + /* TEST SEND RESPONSE |
| 81 | + *************************************************************************/ |
| 82 | + /** |
| 83 | + * @dataProvider providerSendResponse |
| 84 | + */ |
| 85 | + public function testSendResponse($responseArray) |
50 | 86 | { |
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); |
53 | 89 |
|
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 | + ); |
55 | 108 | } |
56 | 109 | } |
0 commit comments