|
3 | 3 | namespace Github\Tests\Api\Repository; |
4 | 4 |
|
5 | 5 | use Github\Tests\Api\TestCase; |
| 6 | +use Github\Exception\TwoFactorAuthenticationRequiredException; |
6 | 7 |
|
7 | 8 | class ContentsTest extends TestCase |
8 | 9 | { |
@@ -38,6 +39,74 @@ public function shouldShowReadme() |
38 | 39 | $this->assertEquals($expectedValue, $api->readme('KnpLabs', 'php-github-api')); |
39 | 40 | } |
40 | 41 |
|
| 42 | + /** |
| 43 | + * @test |
| 44 | + */ |
| 45 | + public function shouldReturnTrueWhenFileExists() |
| 46 | + { |
| 47 | + $responseMock = $this->getMockBuilder('\Guzzle\Http\Message\Response') |
| 48 | + ->disableOriginalConstructor() |
| 49 | + ->getMock(); |
| 50 | + |
| 51 | + $responseMock->expects($this->any()) |
| 52 | + ->method('getStatusCode') |
| 53 | + ->willReturn(200); |
| 54 | + |
| 55 | + $api = $this->getApiMock(); |
| 56 | + $api->expects($this->once()) |
| 57 | + ->method('head') |
| 58 | + ->with('repos/KnpLabs/php-github-api/contents/composer.json', array('ref' => null)) |
| 59 | + ->will($this->returnValue($responseMock)); |
| 60 | + |
| 61 | + $this->assertEquals(true, $api->exists('KnpLabs', 'php-github-api', 'composer.json')); |
| 62 | + } |
| 63 | + |
| 64 | + public function getFailureStubsForExistsTest() |
| 65 | + { |
| 66 | + $nonOkResponseMock =$this->getGuzzleResponseMock(); |
| 67 | + |
| 68 | + $nonOkResponseMock->expects($this->any()) |
| 69 | + ->method('getStatusCode') |
| 70 | + ->willReturn(403); |
| 71 | + |
| 72 | + return array( |
| 73 | + array($this->throwException(new \ErrorException())), |
| 74 | + array($this->returnValue($nonOkResponseMock)) |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @test |
| 80 | + * @dataProvider getFailureStubsForExistsTest |
| 81 | + */ |
| 82 | + public function shouldReturnFalseWhenFileIsNotFound(\PHPUnit_Framework_MockObject_Stub $failureStub) |
| 83 | + { |
| 84 | + $expectedValue = array('some-header' => 'value'); |
| 85 | + |
| 86 | + $api = $this->getApiMock(); |
| 87 | + $api->expects($this->once()) |
| 88 | + ->method('head') |
| 89 | + ->with('repos/KnpLabs/php-github-api/contents/composer.json', array('ref' => null)) |
| 90 | + ->will($failureStub); |
| 91 | + |
| 92 | + $this->assertFalse($api->exists('KnpLabs', 'php-github-api', 'composer.json')); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @test |
| 97 | + * @expectedException \Github\Exception\TwoFactorAuthenticationRequiredException |
| 98 | + */ |
| 99 | + public function shouldBubbleTwoFactorAuthenticationRequiredExceptionsWhenCheckingFileRequiringAuth() |
| 100 | + { |
| 101 | + $api = $this->getApiMock(); |
| 102 | + $api->expects($this->once()) |
| 103 | + ->method('head') |
| 104 | + ->with('repos/KnpLabs/php-github-api/contents/composer.json', array('ref' => null)) |
| 105 | + ->will($this->throwException(new TwoFactorAuthenticationRequiredException(0))); |
| 106 | + |
| 107 | + $api->exists('KnpLabs', 'php-github-api', 'composer.json'); |
| 108 | + } |
| 109 | + |
41 | 110 | /** |
42 | 111 | * @test |
43 | 112 | */ |
@@ -246,4 +315,14 @@ protected function getApiClass() |
246 | 315 | { |
247 | 316 | return 'Github\Api\Repository\Contents'; |
248 | 317 | } |
| 318 | + |
| 319 | + |
| 320 | + private function getGuzzleResponseMock() |
| 321 | + { |
| 322 | + $responseMock = $this->getMockBuilder('\Guzzle\Http\Message\Response') |
| 323 | + ->disableOriginalConstructor() |
| 324 | + ->getMock(); |
| 325 | + |
| 326 | + return $responseMock; |
| 327 | + } |
249 | 328 | } |
0 commit comments