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

[DX][Internal] Init ApiTestCase #972

Open
wants to merge 1 commit into
base: 1.13
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@
"Sylius\\Component\\Resource\\spec\\": "src/Component/legacy/spec/",
"Sylius\\Component\\Resource\\Tests\\": "src/Component/legacy/tests/",
"Sylius\\Resource\\Tests\\": "src/Component/tests/",
"App\\": "tests/Application/src/"
"App\\": "tests/Application/src/",
"Tests\\": "tests/"
}
},
"scripts": {
Expand Down
51 changes: 51 additions & 0 deletions tests/ApiTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Tests;

use Coduo\PHPMatcher\Backtrace\VoidBacktrace;
use Coduo\PHPMatcher\Factory\MatcherFactory;
use Coduo\PHPMatcher\Matcher;
use Coduo\PHPMatcher\PHPUnit\PHPMatcherAssertions;
use PHPUnit\Framework\Attributes\Before;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

abstract class ApiTestCase extends WebTestCase
{
use PHPMatcherAssertions;

protected KernelBrowser $client;

protected Matcher $matcher;

#[Before]
protected function setupClient(): void
{
$this->client = self::createClient();
}

#[Before]
protected static function _createMatcher(): Matcher
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this change possible, the same as with createClient/setupClient change.

Suggested change
protected static function _createMatcher(): Matcher
protected static function createMatcher(): Matcher
protected static function setupMatcher(): Matcher

{
return (new MatcherFactory())->createMatcher(new VoidBacktrace());
}

protected function assertResponseMatchesPattern(string $pattern): void
{
$response = $this->client->getResponse();
$content = $response->getContent();

self::assertMatchesPattern($pattern, $content);
}
}
9 changes: 9 additions & 0 deletions tests/Application/config/packages/doctrine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ doctrine:
driver: "%database_driver%"
path: "%database_path%"
charset: UTF8

profiling_collect_backtrace: '%kernel.debug%'
use_savepoints: true
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
Expand All @@ -24,3 +27,9 @@ doctrine:
type: attribute
dir: '%kernel.project_dir%/src/Subscription/Entity'
prefix: 'App\Subscription\Entity'

when@test:
doctrine:
dbal:
# "TEST_TOKEN" is typically set by ParaTest
dbname_suffix: '_test%env(default::TEST_TOKEN)%'
5 changes: 5 additions & 0 deletions tests/Application/config/packages/zenstruck_foundry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
when@dev: &dev
# See full configuration: https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#full-default-bundle-configuration
zenstruck_foundry:

when@test: *dev
84 changes: 70 additions & 14 deletions tests/Application/src/Tests/Controller/BlogPostApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,40 @@

namespace App\Tests\Controller;

use ApiTestCase\JsonApiTestCase;
use App\Foundry\Factory\BlogPostFactory;
use PHPUnit\Framework\Attributes\Test;
use Sylius\Bundle\ResourceBundle\ResourceBundleInterface;
use Symfony\Component\HttpFoundation\Response;
use Tests\ApiTestCase;
use Tests\PurgeDatabaseTrait;
use Zenstruck\Foundry\Test\Factories;

final class BlogPostApiTest extends JsonApiTestCase
final class BlogPostApiTest extends ApiTestCase
{
use Factories;
use PurgeDatabaseTrait;

#[Test]
public function it_allows_creating_a_blog_post(): void
{
$this->markAsSkippedIfNecessary();

$this->client->request('POST', '/blog-posts/', [], [], ['CONTENT_TYPE' => 'application/json'], '{}');
$response = $this->client->getResponse();
$this->assertResponse($response, 'blog-posts/create_response', Response::HTTP_CREATED);

$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(Response::HTTP_CREATED);
$this->assertResponseHeaderSame('content-type', 'application/json');

$this->assertResponseMatchesPattern(
<<<'JSON'
{
"id": @integer@,
"current_place": {
"draft": 1
}
}
JSON
);
}

#[Test]
Expand All @@ -45,8 +60,21 @@ public function it_allows_reviewing_a_blog_post(): void
;

$this->client->request('PUT', '/blog-posts/' . $blogPost->getId() . '/to_review', [], [], ['CONTENT_TYPE' => 'application/json'], '{}');
$response = $this->client->getResponse();
$this->assertResponse($response, 'blog-posts/to_review_response', Response::HTTP_OK);

$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
$this->assertResponseHeaderSame('content-type', 'application/json');

$this->assertResponseMatchesPattern(
<<<'JSON'
{
"id": @integer@,
"current_place": {
"reviewed": 1
}
}
JSON
);
}

#[Test]
Expand All @@ -60,8 +88,21 @@ public function it_allows_publishing_a_blog_post(): void
;

$this->client->request('PUT', '/blog-posts/' . $blogPost->getId() . '/publish', [], [], ['CONTENT_TYPE' => 'application/json'], '{}');
$response = $this->client->getResponse();
$this->assertResponse($response, 'blog-posts/publish_response', Response::HTTP_OK);

$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
$this->assertResponseHeaderSame('content-type', 'application/json');

$this->assertResponseMatchesPattern(
<<<'JSON'
{
"id": @integer@,
"current_place": {
"published": 1
}
}
JSON
);
}

#[Test]
Expand All @@ -75,8 +116,21 @@ public function it_allows_rejecting_a_blog_post(): void
;

$this->client->request('PUT', '/blog-posts/' . $blogPost->getId() . '/reject', [], [], ['CONTENT_TYPE' => 'application/json'], '{}');
$response = $this->client->getResponse();
$this->assertResponse($response, 'blog-posts/reject_response', Response::HTTP_OK);

$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
$this->assertResponseHeaderSame('content-type', 'application/json');

$this->assertResponseMatchesPattern(
<<<'JSON'
{
"id": @integer@,
"current_place": {
"rejected": 1
}
}
JSON
);
}

#[Test]
Expand All @@ -90,8 +144,9 @@ public function it_does_not_allow_to_publish_a_blog_post_with_draft_status(): vo
;

$this->client->request('PUT', '/blog-posts/' . $blogPost->getId() . '/publish', [], [], ['CONTENT_TYPE' => 'application/json'], '{}');
$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_BAD_REQUEST);

$this->assertResponseStatusCodeSame(Response::HTTP_BAD_REQUEST);
$this->assertResponseHeaderSame('content-type', 'application/json');
}

#[Test]
Expand All @@ -105,8 +160,9 @@ public function it_does_not_allow_to_reject_a_blog_post_with_draft_status(): voi
;

$this->client->request('PUT', '/blog-posts/' . $blogPost->getId() . '/reject', [], [], ['CONTENT_TYPE' => 'application/json'], '{}');
$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_BAD_REQUEST);

$this->assertResponseStatusCodeSame(Response::HTTP_BAD_REQUEST);
$this->assertResponseHeaderSame('content-type', 'application/json');
}

private function markAsSkippedIfNecessary(): void
Expand Down
Loading
Loading