Source of file ApiLoaderTest.php
Size: 5,347 Bytes - Last Modified: 2021-12-24T06:48:16+00:00
/var/www/docs.ssmods.com/process/src/tests/Util/ApiLoaderTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 | <?php namespace BringYourOwnIdeas\Maintenance\Tests\Util; use RuntimeException; use BringYourOwnIdeas\Maintenance\Util\ApiLoader; use GuzzleHttp\Client; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Response; use SilverStripe\Dev\SapphireTest; use Symfony\Component\Cache\Simple\NullCache; class ApiLoaderTest extends SapphireTest { public function testNon200ErrorCodesAreHandled() { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Could not obtain information about module. Error code 404'); $loader = $this->getLoader(); $loader->setGuzzleClient($this->getMockClient(new Response(404))); $loader->doRequest('foo', function () { // noop }); } public function testNonJsonResponsesAreHandled() { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Could not obtain information about module. Response is not JSON'); $loader = $this->getLoader(); $loader->setGuzzleClient($this->getMockClient(new Response( 200, ['Content-Type' => 'text/html; charset=utf-8'] ))); $loader->doRequest('foo', function () { // noop }); } public function testUnsuccessfulResponsesAreHandled() { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Could not obtain information about module. Response returned unsuccessfully'); $loader = $this->getLoader(); $loader->setGuzzleClient($this->getMockClient(new Response( 200, ['Content-Type' => 'application/json'], json_encode(['success' => false]) ))); $loader->doRequest('foo', function () { // noop }); } /** * Note: contains some logic from SupportedAddonsLoader for context * * @group integration */ public function testAddonsAreParsedAndReturnedCorrectly() { $fakeAddons = ['foo/bar', 'bin/baz']; $loader = $this->getLoader(); $loader->setGuzzleClient($this->getMockClient(new Response( 200, ['Content-Type' => 'application/json'], json_encode(['success' => true, 'addons' => $fakeAddons]) ))); $addons = $loader->doRequest('foo', function ($responseBody) { return $responseBody['addons']; }); $this->assertSame($fakeAddons, $addons); } /** * Note: contains some logic from SupportedAddonsLoader for context * * @group integration */ public function testCacheControlSettingsAreRespected() { $fakeAddons = ['foo/bar', 'bin/baz']; $cacheMock = $this->getMockBuilder(NullCache::class) ->setMethods(['get', 'set']) ->getMock(); $cacheMock->expects($this->once())->method('get')->will($this->returnValue(false)); $cacheMock->expects($this->once()) ->method('set') ->with($this->anything(), json_encode($fakeAddons), 5000) ->will($this->returnValue(true)); $loader = $this->getLoader($cacheMock); $loader->setGuzzleClient($this->getMockClient(new Response( 200, ['Content-Type' => 'application/json', 'Cache-Control' => 'max-age=5000'], json_encode(['success' => true, 'addons' => $fakeAddons]) ))); $loader->doRequest('foo', function ($responseBody) { return $responseBody['addons']; }); } public function testCachedAddonsAreUsedWhenAvailable() { $fakeAddons = ['foo/bar', 'bin/baz']; $cacheMock = $this->getMockBuilder(NullCache::class) ->setMethods(['get', 'set']) ->getMock(); $cacheMock->expects($this->once())->method('get')->will($this->returnValue(json_encode($fakeAddons))); $loader = $this->getLoader($cacheMock); $mockClient = $this->getMockBuilder(Client::class)->setMethods(['send'])->getMock(); $mockClient->expects($this->never())->method('send'); $loader->setGuzzleClient($mockClient); $addons = $loader->doRequest('foo', function () { // noop }); $this->assertSame($fakeAddons, $addons); } /** * @param Response $withResponse * @return Client */ protected function getMockClient(Response $withResponse) { $mock = new MockHandler([ $withResponse ]); $handler = HandlerStack::create($mock); return new Client(['handler' => $handler]); } /** * @param bool $cacheMock * @return ApiLoader */ protected function getLoader($cacheMock = false) { if (!$cacheMock) { $cacheMock = $this->getMockBuilder(NullCache::class) ->setMethods(['get', 'set']) ->getMock(); $cacheMock->expects($this->any())->method('get')->will($this->returnValue(false)); $cacheMock->expects($this->any())->method('set')->will($this->returnValue(true)); } $loader = $this->getMockBuilder(ApiLoader::class) ->getMockForAbstractClass(); $loader->setCache($cacheMock); return $loader; } } |