Skip to content

Commit

Permalink
UHF-10941: Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrsky committed Nov 19, 2024
1 parent e306bcb commit 07a2005
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ title }}
{{ message }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ title }}
{{ message }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\helfi_hakuvahti\Kernel;

use Drupal\Core\Url;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\helfi_api_base\Traits\ApiTestTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Prophecy\Argument;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\Traits\PropertyTrait;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;

/**
* Tests for hakuvahti controller.
*/
class HakuvahtiControllerTest extends KernelTestBase {

use ApiTestTrait;
use UserCreationTrait;
use PropertyTrait;

/**
* {@inheritdoc}
*/
protected static $modules = [
'user',
'system',
'helfi_hakuvahti',
];

/**
* {@inheritDoc}
*/
public function setUp(): void {
parent::setUp();

$this->setUpCurrentUser(permissions: ['access content']);
}

/**
* Tests handleConfirmFormSubmission.
*/
public function testHandleConfirmFormSubmission(): void {
$this->container->set(ClientInterface::class, $this->createMockHttpClient([
new Response(200, body: 'success'),
new Response(404, body: 'not found'),
new Response(500, body: 'fail'),
new RequestException("womp womp", new Request('POST', 'test')),
]));

$logger = $this->prophesize(LoggerInterface::class);
$this->container->set('logger.channel.helfi_hakuvahti', $logger->reveal());

// Get request.
$response = $this->makeRequest('GET', 'helfi_hakuvahti.confirm', ['hash' => 'a', 'subscription' => 'b']);
$this->assertEquals(200, $response->getStatusCode());
$this->assertStringContainsString('Confirm saved search', $response->getContent() ?? '');

// Success.
$response = $this->makeRequest('POST', 'helfi_hakuvahti.confirm', ['hash' => 'a', 'subscription' => 'b']);
$this->assertEquals(200, $response->getStatusCode());
$this->assertStringContainsString('Search saved successfully', $response->getContent() ?? '');

// Not found.
$response = $this->makeRequest('POST', 'helfi_hakuvahti.confirm', ['hash' => 'a', 'subscription' => 'b']);
$this->assertEquals(200, $response->getStatusCode());
$this->assertStringContainsString('Confirmation failed', $response->getContent() ?? '');

// Server error.
$response = $this->makeRequest('POST', 'helfi_hakuvahti.confirm', ['hash' => 'a', 'subscription' => 'b']);
$this->assertEquals(200, $response->getStatusCode());
$this->assertStringContainsString('Confirmation failed', $response->getContent() ?? '');

// Guzzle exception.
$response = $this->makeRequest('POST', 'helfi_hakuvahti.confirm', ['hash' => 'a', 'subscription' => 'b']);
$this->assertEquals(200, $response->getStatusCode());
$this->assertStringContainsString('Confirmation failed', $response->getContent() ?? '');
}

/**
* Process a request.
*
* @param string $method
* HTTP method.
* @param string $route
* Drupal route.
* @param array $query
* Query parameters.
*
* @return \Symfony\Component\HttpFoundation\Response
* Controller response.
*/
private function makeRequest(string $method, string $route, array $query = []): SymfonyResponse {
$url = Url::fromRoute($route, options: [
'query' => $query,
]);

$request = $this->getMockedRequest($url->toString(), $method);

return $this->processRequest($request);
}

}

0 comments on commit 07a2005

Please sign in to comment.