|
| 1 | +<?php |
| 2 | + |
| 3 | +class Github_Tests_ClientTest extends PHPUnit_Framework_TestCase |
| 4 | +{ |
| 5 | + public function testInstanciateWithoutHttpClient() |
| 6 | + { |
| 7 | + $client = new Github_Client(); |
| 8 | + |
| 9 | + $this->assertInstanceOf('Github_HttpClientInterface', $client->getHttpClient()); |
| 10 | + } |
| 11 | + |
| 12 | + public function testInstanciateWithHttpClient() |
| 13 | + { |
| 14 | + $httpClient = $this->getHttpClientMock(); |
| 15 | + $client = new Github_Client($httpClient); |
| 16 | + |
| 17 | + $this->assertEquals($httpClient, $client->getHttpClient()); |
| 18 | + } |
| 19 | + |
| 20 | + public function testAuthenticate() |
| 21 | + { |
| 22 | + $login = 'login'; |
| 23 | + $secret = 'secret'; |
| 24 | + $method = 'method'; |
| 25 | + |
| 26 | + $httpClient = $this->getHttpClientMock(); |
| 27 | + $httpClient->expects($this->exactly(3)) |
| 28 | + ->method('setOption') |
| 29 | + ->will($this->returnValue($httpClient)); |
| 30 | + |
| 31 | + $client = $this->getClientMockBuilder() |
| 32 | + ->setMethods(array('getHttpClient')) |
| 33 | + ->getMock(); |
| 34 | + $client->expects($this->once()) |
| 35 | + ->method('getHttpClient') |
| 36 | + ->with() |
| 37 | + ->will($this->returnValue($httpClient)); |
| 38 | + |
| 39 | + $client->authenticate($login, $secret, $method); |
| 40 | + } |
| 41 | + |
| 42 | + public function testDeauthenticate() |
| 43 | + { |
| 44 | + $client = $this->getClientMockBuilder() |
| 45 | + ->setMethods(array('authenticate')) |
| 46 | + ->getMock(); |
| 47 | + $client->expects($this->once()) |
| 48 | + ->method('authenticate') |
| 49 | + ->with(null, null, null); |
| 50 | + |
| 51 | + $client->deAuthenticate(); |
| 52 | + } |
| 53 | + |
| 54 | + public function testGet() |
| 55 | + { |
| 56 | + $route = '/some/route'; |
| 57 | + $parameters = array('a' => 'b'); |
| 58 | + $options = array('c' => 'd'); |
| 59 | + |
| 60 | + $httpClient = $this->getHttpClientMock(); |
| 61 | + $httpClient->expects($this->once()) |
| 62 | + ->method('get') |
| 63 | + ->with($route, $parameters, $options); |
| 64 | + |
| 65 | + $client = $this->getClientMockBuilder() |
| 66 | + ->setMethods(array('getHttpClient')) |
| 67 | + ->getMock(); |
| 68 | + $client->expects($this->once()) |
| 69 | + ->method('getHttpClient') |
| 70 | + ->with() |
| 71 | + ->will($this->returnValue($httpClient)); |
| 72 | + |
| 73 | + $client->get($route, $parameters, $options); |
| 74 | + } |
| 75 | + |
| 76 | + public function testPost() |
| 77 | + { |
| 78 | + $route = '/some/route'; |
| 79 | + $parameters = array('a' => 'b'); |
| 80 | + $options = array('c' => 'd'); |
| 81 | + |
| 82 | + $httpClient = $this->getHttpClientMock(); |
| 83 | + $httpClient->expects($this->once()) |
| 84 | + ->method('post') |
| 85 | + ->with($route, $parameters, $options); |
| 86 | + |
| 87 | + $client = $this->getClientMockBuilder() |
| 88 | + ->setMethods(array('getHttpClient')) |
| 89 | + ->getMock(); |
| 90 | + $client->expects($this->once()) |
| 91 | + ->method('getHttpClient') |
| 92 | + ->with() |
| 93 | + ->will($this->returnValue($httpClient)); |
| 94 | + |
| 95 | + $client->post($route, $parameters, $options); |
| 96 | + } |
| 97 | + |
| 98 | + public function testDefaultApi() |
| 99 | + { |
| 100 | + $client = new Github_Client(); |
| 101 | + |
| 102 | + $this->assertInstanceOf('Github_Api_User', $client->getUserApi()); |
| 103 | + } |
| 104 | + |
| 105 | + public function testInjectApi() |
| 106 | + { |
| 107 | + $client = new Github_Client(); |
| 108 | + |
| 109 | + $userApiMock = $this->getMockBuilder('Github_ApiInterface') |
| 110 | + ->getMock(); |
| 111 | + |
| 112 | + $client->setApi('user', $userApiMock); |
| 113 | + |
| 114 | + $this->assertSame($userApiMock, $client->getUserApi()); |
| 115 | + } |
| 116 | + |
| 117 | + protected function getClientMockBuilder() |
| 118 | + { |
| 119 | + return $this->getMockBuilder('Github_Client') |
| 120 | + ->disableOriginalConstructor(); |
| 121 | + } |
| 122 | + |
| 123 | + protected function getHttpClientMock() |
| 124 | + { |
| 125 | + return $this->getMockBuilder('Github_HttpClientInterface') |
| 126 | + ->getMock(); |
| 127 | + } |
| 128 | +} |
0 commit comments