From 1b8c176dcf23d15456019dcbcbeb2966c0e95c00 Mon Sep 17 00:00:00 2001 From: Mike Decker Date: Fri, 8 Sep 2023 15:18:01 -0700 Subject: [PATCH] fixed unit tests --- .../Plugin/InstallTask/SiteSettingsTest.php | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/tests/src/Kernel/Plugin/InstallTask/SiteSettingsTest.php b/tests/src/Kernel/Plugin/InstallTask/SiteSettingsTest.php index e4c74aa30..c984bc801 100644 --- a/tests/src/Kernel/Plugin/InstallTask/SiteSettingsTest.php +++ b/tests/src/Kernel/Plugin/InstallTask/SiteSettingsTest.php @@ -13,6 +13,7 @@ use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\GuzzleException; +use GuzzleHttp\Psr7\Stream; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Drupal\stanford_profile\Plugin\InstallTask\SiteSettings; @@ -41,7 +42,7 @@ class SiteSettingsTest extends KernelTestBase { /** * The response guzzle mock object will return. * - * @var mixed + * @var \GuzzleHttp\Psr7\Stream */ protected $guzzleResponse; @@ -115,7 +116,7 @@ public function setup(): void { drupal_flush_all_caches(); - $this->guzzleResponse = json_encode([ + $data = json_encode([ 'result' => [ [ [ @@ -140,6 +141,11 @@ public function setup(): void { ], ], ]); + + $resource = fopen('php://memory', 'r+'); + fwrite($resource, $data); + rewind($resource); + $this->guzzleResponse = new Stream($resource); } /** @@ -182,11 +188,12 @@ protected function getMockLogger() { protected function getMockGuzzle($throw_guzzle_exception = NULL) { $client = $this->createMock(ClientInterface::class); $response = $this->createMock(ResponseInterface::class); + $request = $this->createMock(RequestInterface::class); switch ($throw_guzzle_exception) { case GuzzleException::class: $response->method('getBody') - ->willThrowException(new ClientException('Failed here', $this->createMock(RequestInterface::class))); + ->willThrowException(new ClientException('Failed here', $request, $response)); break; case Exception::class: @@ -222,14 +229,20 @@ public function testValidInstallTasks() { ->getStorage('user') ->loadByProperties(['name' => ['barfoo', 'bazbar']]); $this->assertCount(2, $users); - $this->assertEquals('https://foo bar.sites.stanford.edu', \Drupal::state()->get('xmlsitemap_base_url')); + $this->assertEquals('https://foo bar.sites.stanford.edu', \Drupal::state() + ->get('xmlsitemap_base_url')); } /** * When the API can't find the site, no changes will be made. */ public function testSiteNotFound() { - $this->guzzleResponse = json_encode(['result' => [['message' => 'no records found']]]); + $data = json_encode(['result' => [['message' => 'no records found']]]); + $resource = fopen('php://memory', 'r+'); + fwrite($resource, $data); + rewind($resource); + $this->guzzleResponse = new Stream($resource); + $this->runInstallTask(); drupal_flush_all_caches(); @@ -241,7 +254,11 @@ public function testSiteNotFound() { * When the API doesn't return a json object, no changes will be made. */ public function testIncorrectApiResponse() { - $this->guzzleResponse = $this->randomString(); + $resource = fopen('php://memory', 'r+'); + fwrite($resource, $this->randomString()); + rewind($resource); + $this->guzzleResponse = new Stream($resource); + $this->runInstallTask(); drupal_flush_all_caches();