Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test state change #29

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Controller/StateChangeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private function getProviderState(Request $request): ProviderState
throw new BadRequestException("'state' is missing or invalid in state change request.");
}
if (!is_array($params)) {
throw new BadRequestException("'params' is missing or invalid in state change request.");
throw new BadRequestException("'params' is invalid in state change request.");
}

return new ProviderState($state, $params);
Expand Down
163 changes: 163 additions & 0 deletions tests/Application/Controller/StateChangeControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

namespace Tienvx\Bundle\PactProviderBundle\Tests\Application\Controller;

use PHPUnit\Framework\Attributes\TestWith;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Tienvx\Bundle\PactProviderBundle\Enum\Action;

class StateChangeControllerTest extends WebTestCase
{
public function testWrongUrl(): void
{
$client = static::createClient();
$client->request('POST', '/pact-change-state-not-found');
$this->assertResponseStatusCodeSame(404);
}

public function testRequestBodyIsEmpty(): void
{
$client = static::createClient();
$client->request('POST', '/test-pact-change-state');
$this->assertResponseStatusCodeSame(400);
$this->assertStringContainsString('Request body is empty.', $client->getResponse()->getContent());
}

#[TestWith([[]])]
#[TestWith([['state' => 123]])]
public function testMissingOrInvalidProviderStateNameInBody(array $value): void
{
$client = static::createClient();
$client->request('POST', '/test-pact-change-state', [], [], [], json_encode([
'params' => [
'key' => 'value',
],
'action' => Action::SETUP,
...$value,
]));
$this->assertResponseStatusCodeSame(400);
$this->assertStringContainsString("'state' is missing or invalid in state change request.", $client->getResponse()->getContent());
}

public function testInvalidProviderStateParamsInBody(): void
{
$client = static::createClient();
$client->request('POST', '/test-pact-change-state', [], [], [], json_encode([
'state' => 'required state',
'params' => 123,
'action' => Action::SETUP,
]));
$this->assertResponseStatusCodeSame(400);
$this->assertStringContainsString("'params' is invalid in state change request.", $client->getResponse()->getContent());
}

#[TestWith([[]])]
#[TestWith([['action' => 'clean']])]
public function testMissingOrInvalidProviderStateActionInBody(array $value): void
{
$client = static::createClient();
$client->request('POST', '/test-pact-change-state', [], [], [], json_encode([
'state' => 'required state',
'params' => [
'key' => 'value',
],
...$value,
]));
$this->assertResponseStatusCodeSame(400);
$this->assertStringContainsString("'action' is missing or invalid in state change request.", $client->getResponse()->getContent());
}

#[TestWith([Action::SETUP])]
#[TestWith([Action::TEARDOWN])]
public function testNoStateValuesWithBody(string $action): void
{
$client = static::createClient();
$client->request('POST', '/test-pact-change-state', [], [], [], json_encode([
'state' => 'no values',
'params' => [
'key' => 'value',
],
'action' => $action,
]));
$this->assertResponseStatusCodeSame(204);
$this->assertEmpty($client->getResponse()->getContent());
}

public function testHasValuesWithBody(): void
{
$client = static::createClient();
$client->request('POST', '/test-pact-change-state', [], [], [], json_encode([
'state' => 'required state',
'params' => [
'key' => 'value',
],
'action' => Action::SETUP,
]));
$this->assertResponseStatusCodeSame(200);
$this->assertResponseHeaderSame('Content-Type', 'application/json');
$this->assertStringContainsString(json_encode([
'id' => 123,
]), $client->getResponse()->getContent());
}

#[TestWith([[]])]
#[TestWith([['state[]' => 'required state']])]
public function testMissingOrInvalidProviderStateNameInQuery(array $value): void
{
$_ENV['STATE_CHANGE_BODY'] = 'false';
$client = static::createClient();
$client->request('POST', '/test-pact-change-state?'.http_build_query([
'key' => 'value',
'action' => Action::SETUP,
...$value,
]));
$this->assertResponseStatusCodeSame(400);
$this->assertStringContainsString("'state' is missing or invalid in state change request.", $client->getResponse()->getContent());
}

#[TestWith([[]])]
#[TestWith([['action' => 'clean']])]
public function testMissingOrInvalidProviderStateActionInQuery(array $value): void
{
$_ENV['STATE_CHANGE_BODY'] = 'false';
$client = static::createClient();
$client->request('POST', '/test-pact-change-state?'.http_build_query([
'state' => 'required state',
'key' => 'value',
...$value,
]));
$this->assertResponseStatusCodeSame(400);
$this->assertStringContainsString("'action' is missing or invalid in state change request.", $client->getResponse()->getContent());
}

#[TestWith([Action::SETUP])]
#[TestWith([Action::TEARDOWN])]
public function testNoStateValuesWithoutBody(string $action): void
{
$_ENV['STATE_CHANGE_BODY'] = 'false';
$client = static::createClient();
$client->request('POST', '/test-pact-change-state?'.http_build_query([
'state' => 'no values',
'key' => 'value',
'action' => $action,
]));
$this->assertResponseStatusCodeSame(204);
$this->assertEmpty($client->getResponse()->getContent());
}

public function testHasValuesWithoutBody(): void
{
$_ENV['STATE_CHANGE_BODY'] = 'false';
$client = static::createClient();
$client->request('POST', '/test-pact-change-state?'.http_build_query([
'state' => 'required state',
'key' => 'value',
'action' => Action::SETUP,
]));
$this->assertResponseStatusCodeSame(200);
$this->assertResponseHeaderSame('Content-Type', 'application/json');
$this->assertStringContainsString(json_encode([
'id' => 123,
]), $client->getResponse()->getContent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

$container->loadFromExtension('tienvx_pact_provider', [
'state_change' => [
'body' => true,
'body' => '%env(bool:STATE_CHANGE_BODY)%',
'url' => '/test-pact-change-state',
],
'messages_url' => '/test-pact-messages',
Expand Down
2 changes: 2 additions & 0 deletions tests/Application/TestApplication/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

return static function (ContainerConfigurator $container) {
$container->parameters()->set('env(STATE_CHANGE_BODY)', 'true');

$services = $container->services()
->defaults()
->autowire()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Tienvx\Bundle\PactProviderBundle\Tests\Application\TestApplication\StateHandler;

use Tienvx\Bundle\PactProviderBundle\Attribute\AsStateHandler;
use Tienvx\Bundle\PactProviderBundle\Model\StateValues;
use Tienvx\Bundle\PactProviderBundle\StateHandler\SetUpInterface;
use Tienvx\Bundle\PactProviderBundle\StateHandler\TearDownInterface;

#[AsStateHandler(state: 'no values')]
class NoValuesHandler implements SetUpInterface, TearDownInterface
{
public function setUp(array $params): ?StateValues
{
return null;
}

public function tearDown(array $params): void
{
}
}