X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;ds=sidebyside;f=tests%2Fphpunit%2Fincludes%2Fconfig%2FEtcdConfigTest.php;h=07dbd002c1bd5a9c55bc0a8b36d7c48ed9c298e8;hb=2a77862c522dd0de76498f905aede9d334618e44;hp=3e70a07819b6c6bd54b44d0d3f6861c33b24d5fb;hpb=6bd1e23f4a281b05e6c171653a715c394aac36d6;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/config/EtcdConfigTest.php b/tests/phpunit/includes/config/EtcdConfigTest.php index 3e70a07819..07dbd002c1 100644 --- a/tests/phpunit/includes/config/EtcdConfigTest.php +++ b/tests/phpunit/includes/config/EtcdConfigTest.php @@ -2,7 +2,9 @@ use Wikimedia\TestingAccessWrapper; -class EtcdConfigTest extends PHPUnit_Framework_TestCase { +class EtcdConfigTest extends PHPUnit\Framework\TestCase { + + use MediaWikiCoversValidator; private function createConfigMock( array $options = [] ) { return $this->getMockBuilder( EtcdConfig::class ) @@ -15,14 +17,23 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { ->getMock(); } - private function createSimpleConfigMock( array $config ) { + private static function createEtcdResponse( array $response ) { + $baseResponse = [ + 'config' => null, + 'error' => null, + 'retry' => false, + 'modifiedIndex' => 0, + ]; + return array_merge( $baseResponse, $response ); + } + + private function createSimpleConfigMock( array $config, $index = 0 ) { $mock = $this->createConfigMock(); $mock->expects( $this->once() )->method( 'fetchAllFromEtcd' ) - ->willReturn( [ - $config, - null, // error - false // retry? - ] ); + ->willReturn( self::createEtcdResponse( [ + 'config' => $config, + 'modifiedIndex' => $index, + ] ) ); return $mock; } @@ -68,6 +79,17 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { $config->get( 'unknown' ); } + /** + * @covers EtcdConfig::getModifiedIndex + */ + public function testGetModifiedIndex() { + $config = $this->createSimpleConfigMock( + [ 'some' => 'value' ], + 123 + ); + $this->assertSame( 123, $config->getModifiedIndex() ); + } + /** * @covers EtcdConfig::__construct */ @@ -79,6 +101,7 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { ->willReturn( [ 'config' => [ 'known' => 'from-cache' ], 'expires' => INF, + 'modifiedIndex' => 123 ] ); $config = $this->createConfigMock( [ 'cache' => $cache ] ); @@ -93,11 +116,8 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { 'class' => HashBagOStuff::class ] ] ); $config->expects( $this->once() )->method( 'fetchAllFromEtcd' ) - ->willReturn( [ - [ 'known' => 'from-fetch' ], - null, // error - false // retry? - ] ); + ->willReturn( self::createEtcdResponse( + [ 'config' => [ 'known' => 'from-fetch' ], ] ) ); $this->assertSame( 'from-fetch', $config->get( 'known' ) ); } @@ -164,7 +184,8 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { 'cache' => $cache, ] ); $mock->expects( $this->once() )->method( 'fetchAllFromEtcd' ) - ->willReturn( [ [ 'known' => 'from-fetch' ], null, false ] ); + ->willReturn( + self::createEtcdResponse( [ 'config' => [ 'known' => 'from-fetch' ] ] ) ); $this->assertSame( 'from-fetch', $mock->get( 'known' ) ); } @@ -189,7 +210,7 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { 'cache' => $cache, ] ); $mock->expects( $this->once() )->method( 'fetchAllFromEtcd' ) - ->willReturn( [ null, 'Fake error', false ] ); + ->willReturn( self::createEtcdResponse( [ 'error' => 'Fake error', ] ) ); $this->setExpectedException( ConfigException::class ); $mock->get( 'key' ); @@ -211,6 +232,7 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { [ 'config' => [ 'known' => 'from-cache' ], 'expires' => INF, + 'modifiedIndex' => 123 ] ) ); // .. misses lock @@ -239,6 +261,7 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { ->willReturn( [ 'config' => [ 'known' => 'from-cache' ], 'expires' => INF, + 'modifiedIndex' => 0, ] ); $cache->expects( $this->never() )->method( 'lock' ); @@ -264,6 +287,7 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { ->willReturn( [ 'config' => [ 'known' => 'from-cache' ], 'expires' => INF, + 'modifiedIndex' => 0, ] ); $cache->expects( $this->never() )->method( 'lock' ); @@ -290,6 +314,7 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { [ 'config' => [ 'known' => 'from-cache-expired' ], 'expires' => -INF, + 'modifiedIndex' => 0, ] ); // .. gets lock @@ -301,7 +326,7 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { 'cache' => $cache, ] ); $mock->expects( $this->once() )->method( 'fetchAllFromEtcd' ) - ->willReturn( [ [ 'known' => 'from-fetch' ], null, false ] ); + ->willReturn( self::createEtcdResponse( [ 'config' => [ 'known' => 'from-fetch' ] ] ) ); $this->assertSame( 'from-fetch', $mock->get( 'known' ) ); } @@ -319,6 +344,7 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { [ 'config' => [ 'known' => 'from-cache-expired' ], 'expires' => -INF, + 'modifiedIndex' => 0, ] ); // .. gets lock @@ -330,7 +356,7 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { 'cache' => $cache, ] ); $mock->expects( $this->once() )->method( 'fetchAllFromEtcd' ) - ->willReturn( [ null, 'Fake failure', true ] ); + ->willReturn( self::createEtcdResponse( [ 'error' => 'Fake failure', 'retry' => true ] ) ); $this->assertSame( 'from-cache-expired', $mock->get( 'known' ) ); } @@ -348,6 +374,7 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { ->willReturn( [ 'config' => [ 'known' => 'from-cache-expired' ], 'expires' => -INF, + 'modifiedIndex' => 0, ] ); // .. misses lock $cache->expects( $this->once() )->method( 'lock' ) @@ -372,16 +399,16 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { 'body' => json_encode( [ 'node' => [ 'nodes' => [ [ 'key' => '/example/foo', - 'value' => json_encode( [ 'val' => true ] ) + 'value' => json_encode( [ 'val' => true ] ), + 'modifiedIndex' => 123 ], ] ] ] ), 'error' => '', ], - 'expect' => [ - [ 'foo' => true ], // data - null, - false // retry - ], + 'expect' => self::createEtcdResponse( [ + 'config' => [ 'foo' => true ], // data + 'modifiedIndex' => 123 + ] ), ], '200 OK - Empty dir' => [ 'http' => [ @@ -391,25 +418,27 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { 'body' => json_encode( [ 'node' => [ 'nodes' => [ [ 'key' => '/example/foo', - 'value' => json_encode( [ 'val' => true ] ) + 'value' => json_encode( [ 'val' => true ] ), + 'modifiedIndex' => 123 ], [ 'key' => '/example/sub', 'dir' => true, + 'modifiedIndex' => 234, 'nodes' => [], ], [ 'key' => '/example/bar', - 'value' => json_encode( [ 'val' => false ] ) + 'value' => json_encode( [ 'val' => false ] ), + 'modifiedIndex' => 125 ], ] ] ] ), 'error' => '', ], - 'expect' => [ - [ 'foo' => true, 'bar' => false ], // data - null, - false // retry - ], + 'expect' => self::createEtcdResponse( [ + 'config' => [ 'foo' => true, 'bar' => false ], // data + 'modifiedIndex' => 125 // largest modified index + ] ), ], '200 OK - Recursive' => [ 'http' => [ @@ -420,25 +449,28 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { [ 'key' => '/example/a', 'dir' => true, + 'modifiedIndex' => 124, 'nodes' => [ [ 'key' => 'b', 'value' => json_encode( [ 'val' => true ] ), + 'modifiedIndex' => 123, + ], [ 'key' => 'c', 'value' => json_encode( [ 'val' => false ] ), + 'modifiedIndex' => 123, ], ], ], ] ] ] ), 'error' => '', ], - 'expect' => [ - [ 'a/b' => true, 'a/c' => false ], // data - null, - false // retry - ], + 'expect' => self::createEtcdResponse( [ + 'config' => [ 'a/b' => true, 'a/c' => false ], // data + 'modifiedIndex' => 123 // largest modified index + ] ), ], '200 OK - Missing nodes at second level' => [ 'http' => [ @@ -449,15 +481,32 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { [ 'key' => '/example/a', 'dir' => true, + 'modifiedIndex' => 0, ], ] ] ] ), 'error' => '', ], - 'expect' => [ - null, - "Unexpected JSON response in dir 'a'; missing 'nodes' list.", - false // retry + 'expect' => self::createEtcdResponse( [ + 'error' => "Unexpected JSON response in dir 'a'; missing 'nodes' list.", + ] ), + ], + '200 OK - Directory with non-array "nodes" key' => [ + 'http' => [ + 'code' => 200, + 'reason' => 'OK', + 'headers' => [], + 'body' => json_encode( [ 'node' => [ 'nodes' => [ + [ + 'key' => '/example/a', + 'dir' => true, + 'nodes' => 'not an array' + ], + ] ] ] ), + 'error' => '', ], + 'expect' => self::createEtcdResponse( [ + 'error' => "Unexpected JSON response in dir 'a'; 'nodes' is not an array.", + ] ), ], '200 OK - Correctly encoded garbage response' => [ 'http' => [ @@ -467,11 +516,9 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { 'body' => json_encode( [ 'foo' => 'bar' ] ), 'error' => '', ], - 'expect' => [ - null, - "Unexpected JSON response: Missing or invalid node at top level.", - false // retry - ], + 'expect' => self::createEtcdResponse( [ + 'error' => "Unexpected JSON response: Missing or invalid node at top level.", + ] ), ], '200 OK - Bad value' => [ 'http' => [ @@ -481,30 +528,27 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { 'body' => json_encode( [ 'node' => [ 'nodes' => [ [ 'key' => '/example/foo', - 'value' => ';"broken{value' + 'value' => ';"broken{value', + 'modifiedIndex' => 123, ] ] ] ] ), 'error' => '', ], - 'expect' => [ - null, // data - "Failed to parse value for 'foo'.", - false // retry - ], + 'expect' => self::createEtcdResponse( [ + 'error' => "Failed to parse value for 'foo'.", + ] ), ], '200 OK - Empty node list' => [ 'http' => [ 'code' => 200, 'reason' => 'OK', 'headers' => [], - 'body' => '{"node":{"nodes":[]}}', + 'body' => '{"node":{"nodes":[], "modifiedIndex": 12 }}', 'error' => '', ], - 'expect' => [ - [], // data - null, - false // retry - ], + 'expect' => self::createEtcdResponse( [ + 'config' => [], // data + ] ), ], '200 OK - Invalid JSON' => [ 'http' => [ @@ -514,11 +558,9 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { 'body' => '', 'error' => '(curl error: no status set)', ], - 'expect' => [ - null, // data - "Error unserializing JSON response.", - false // retry - ], + 'expect' => self::createEtcdResponse( [ + 'error' => "Error unserializing JSON response.", + ] ), ], '404 Not Found' => [ 'http' => [ @@ -528,11 +570,9 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { 'body' => '', 'error' => '', ], - 'expect' => [ - null, // data - 'HTTP 404 (Not Found)', - false // retry - ], + 'expect' => self::createEtcdResponse( [ + 'error' => 'HTTP 404 (Not Found)', + ] ), ], '400 Bad Request - custom error' => [ 'http' => [ @@ -542,11 +582,10 @@ class EtcdConfigTest extends PHPUnit_Framework_TestCase { 'body' => '', 'error' => 'No good reason', ], - 'expect' => [ - null, // data - 'No good reason', - true // retry - ], + 'expect' => self::createEtcdResponse( [ + 'error' => 'No good reason', + 'retry' => true, // retry + ] ), ], ]; }