config: Add tests for EtcdConfig::fetchAllFromEtcdServer
authorTimo Tijhof <krinklemail@gmail.com>
Sun, 23 Jul 2017 01:24:33 +0000 (18:24 -0700)
committerKrinkle <krinklemail@gmail.com>
Sun, 23 Jul 2017 01:42:49 +0000 (01:42 +0000)
Follows-up 9b459d29e0110a21ea18.

Bug: T156924
Change-Id: Ib68c55ea3386a12e707f6d7d732da9fe727ccc2f

tests/phpunit/includes/config/EtcdConfigTest.php

index 763bfa8..8e57a01 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use Wikimedia\TestingAccessWrapper;
+
 class EtcConfigTest extends PHPUnit_Framework_TestCase {
 
        private function createConfigMock( array $options = [] ) {
@@ -359,4 +361,51 @@ class EtcConfigTest extends PHPUnit_Framework_TestCase {
 
                $this->assertSame( 'from-cache-expired', $mock->get( 'known' ) );
        }
+
+       public static function provideFetchFromServer() {
+               return [
+                       [
+                               'http' => [
+                                       'code' => 200,
+                                       'reason' => 'OK',
+                                       'headers' => [
+                                               'content-length' => 0,
+                                       ],
+                                       'body' => '',
+                                       'error' => '(curl error: no status set)',
+                               ],
+                               'expect' => [
+                                       // FIXME: Returning 4 values instead of 3
+                                       null,
+                                       200,
+                                       "Unexpected JSON response; missing 'nodes' list.",
+                                       false
+                               ],
+                       ],
+               ];
+       }
+
+       /**
+        * @covers EtcdConfig::fetchAllFromEtcdServer
+        * @dataProvider provideFetchFromServer
+        */
+       public function testFetchFromServer( array $httpResponse, array $expected ) {
+               $http = $this->getMockBuilder( MultiHttpClient::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $http->expects( $this->once() )->method( 'run' )
+                       ->willReturn( array_values( $httpResponse ) );
+
+               $conf = $this->getMockBuilder( EtcdConfig::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               // Access for protected member and method
+               $conf = TestingAccessWrapper::newFromObject( $conf );
+               $conf->http = $http;
+
+               $this->assertSame(
+                       $expected,
+                       $conf->fetchAllFromEtcdServer( 'etcd-tcp.example.net' )
+               );
+       }
 }