Skip to content

Commit

Permalink
fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Sep 8, 2023
1 parent c173d12 commit 1b8c176
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions tests/src/Kernel/Plugin/InstallTask/SiteSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,7 +42,7 @@ class SiteSettingsTest extends KernelTestBase {
/**
* The response guzzle mock object will return.
*
* @var mixed
* @var \GuzzleHttp\Psr7\Stream
*/
protected $guzzleResponse;

Expand Down Expand Up @@ -115,7 +116,7 @@ public function setup(): void {

drupal_flush_all_caches();

$this->guzzleResponse = json_encode([
$data = json_encode([
'result' => [
[
[
Expand All @@ -140,6 +141,11 @@ public function setup(): void {
],
],
]);

$resource = fopen('php://memory', 'r+');
fwrite($resource, $data);
rewind($resource);
$this->guzzleResponse = new Stream($resource);
}

/**
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit 1b8c176

Please sign in to comment.