Skip to content

Commit

Permalink
refactor #327 Test grids with UI instead of API (loic425)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.13 branch.

Discussion
----------



Commits
-------

8910e60 Test grids with UI instead of API
e0df523 Update src/Bundle/Tests/Functional/GridUiTest.php
bbce3ff Route of type sylius resource instead of resource_api
b6e8c21 Improve PHPUnit tests
  • Loading branch information
GSadee authored Jun 6, 2024
2 parents e6a0d54 + b6e8c21 commit 3b7010a
Show file tree
Hide file tree
Showing 33 changed files with 309 additions and 143 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ jobs:
name: Run component tests
run: (cd src/Component && vendor/bin/phpspec run)

-
name: Run bundle tests
run: composer test

-
name: Run lint container
run: (cd tests/Application && bin/console lint:container)
Expand All @@ -100,6 +96,12 @@ jobs:
(cd tests/Application && bin/console cache:clear --env=test_grids_with_php_config)
composer test-php-config
-
name: Run tests for grids with yaml config
run: |
(cd tests/Application && bin/console cache:clear --env=test_grids_with_yaml_config)
composer test-yaml-config
-
name: Run tests with grids as services
run: |
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ src/Bundle/test/tmp

src/Bundle/test/vendor/
src/Bundle/test/composer.lock

tests/Application/config/db.sql
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"sylius-labs/coding-standard": "^4.0",
"sylius/resource-bundle": "^1.11@beta",
"symfony/console": "^5.4 || ^6.0",
"symfony/css-selector": "^5.4 || ^6.0",
"symfony/dotenv": "^5.4 || ^6.0",
"symfony/maker-bundle": "^1.36",
"symfony/polyfill-mbstring": "<1.22.0 || >1.22.0",
Expand Down Expand Up @@ -120,7 +121,7 @@
"fix": [
"vendor/bin/ecs check --fix"
],
"test": [
"test-yaml-config": [
"vendor/bin/phpspec run --ansi --no-interaction",
"APP_ENV=test_grids_with_yaml_config vendor/bin/phpunit --colors=always"
],
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ini name="error_reporting" value="-1" />

<!-- ###+ symfony/framework-bundle ### -->
<env name="APP_ENV" value="test"/>
<env name="APP_ENV" value="test_grids_with_php_config"/>
<env name="SHELL_VERBOSITY" value="-1" />
<!-- ###- symfony/framework-bundle ### -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@

namespace Sylius\Bundle\GridBundle\Tests\Functional;

use ApiTestCase\JsonApiTestCase;
use ApiTestCase\ApiTestCase;
use Coduo\PHPMatcher\Backtrace\VoidBacktrace;
use Coduo\PHPMatcher\Matcher;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\Response;

final class GridApiTest extends JsonApiTestCase
final class GridUiTest extends ApiTestCase
{
/** @var array */
private $data;
private array $data;

protected function setUp(): void
{
Expand All @@ -31,19 +34,19 @@ protected function setUp(): void
public function it_shows_authors_grid(): void
{
$this->client->request('GET', '/authors/');
$response = $this->client->getResponse();

$this->assertResponse($this->client->getResponse(), 'authors_grid');
$this->assertResponseCode($response, Response::HTTP_OK);

$this->assertCount(10, $this->getAuthorNamesFromResponse());
}

/** @test */
public function it_sorts_authors_by_name_ascending_by_default(): void
{
$this->client->request('GET', '/authors/?limit=100');

$items = $this->getItemsFromCurrentResponse();
$names = array_map(static function (array $item): string {
return $item['name'];
}, $items);
$names = $this->getAuthorNamesFromResponse();

$sortedNames = $names;
sort($names);
Expand All @@ -56,10 +59,7 @@ public function it_sorts_authors_by_name_descending(): void
{
$this->client->request('GET', '/authors/?sorting[name]=desc&limit=100');

$items = $this->getItemsFromCurrentResponse();
$names = array_map(static function (array $item): string {
return $item['name'];
}, $items);
$names = $this->getAuthorNamesFromResponse();

$sortedNames = $names;
rsort($names);
Expand All @@ -72,19 +72,19 @@ public function it_paginates_authors_by_10_by_default(): void
{
$this->client->request('GET', '/authors/');

$this->assertCount(10, $this->getItemsFromCurrentResponse());
$this->assertCount(10, $this->getAuthorNamesFromResponse());
}

/** @test */
public function it_paginates_authors_by_5_or_15(): void
{
$this->client->request('GET', '/authors/?limit=5');

$this->assertCount(5, $this->getItemsFromCurrentResponse());
$this->assertCount(5, $this->getAuthorNamesFromResponse());

$this->client->request('GET', '/authors/?limit=15');

$this->assertCount(15, $this->getItemsFromCurrentResponse());
$this->assertCount(15, $this->getAuthorNamesFromResponse());
}

/** @test */
Expand All @@ -95,8 +95,10 @@ public function it_filters_books_by_title(): void
urlencode('Book 5'),
));

$this->assertCount(1, $this->getItemsFromCurrentResponse());
$this->assertSame('Book 5', $this->getFirstItemFromCurrentResponse()['title']);
$titles = $this->getBookTitlesFromResponse();

$this->assertCount(1, $titles);
$this->assertSame('Book 5', $titles[0]);
}

/** @test */
Expand All @@ -107,8 +109,10 @@ public function it_filters_books_by_title_with_contains(): void
urlencode('jurassic'),
));

$this->assertCount(1, $this->getItemsFromCurrentResponse());
$this->assertSame('Jurassic Park', $this->getFirstItemFromCurrentResponse()['title']);
$titles = $this->getBookTitlesFromResponse();

$this->assertCount(1, $titles);
$this->assertSame('Jurassic Park', $titles[0]);
}

/** @test */
Expand All @@ -118,8 +122,10 @@ public function it_filters_books_by_author(): void

$this->client->request('GET', sprintf('/books/?criteria[author][]=%d', $authorId));

$this->assertCount(2, $this->getItemsFromCurrentResponse());
$this->assertSame('Jurassic Park', $this->getFirstItemFromCurrentResponse()['title']);
$titles = $this->getBookTitlesFromResponse();

$this->assertCount(2, $titles);
$this->assertSame('Jurassic Park', $titles[0]);
}

/** @test */
Expand All @@ -130,8 +136,10 @@ public function it_filters_books_by_authors(): void

$this->client->request('GET', sprintf('/books/?criteria[author][]=%d&criteria[author][]=%d', $firstAuthorId, $secondAuthorId));

$this->assertCount(3, $this->getItemsFromCurrentResponse());
$this->assertSame('A Study in Scarlet', $this->getFirstItemFromCurrentResponse()['title']);
$titles = $this->getBookTitlesFromResponse();

$this->assertCount(3, $titles);
$this->assertSame('A Study in Scarlet', $titles[0]);
}

/** @test */
Expand All @@ -141,8 +149,10 @@ public function it_filters_books_by_authors_nationality(): void

$this->client->request('GET', sprintf('/books/?criteria[nationality]=%d', $authorNationalityId));

$this->assertCount(2, $this->getItemsFromCurrentResponse());
$this->assertSame('Jurassic Park', $this->getFirstItemFromCurrentResponse()['title']);
$titles = $this->getBookTitlesFromResponse();

$this->assertCount(2, $titles);
$this->assertSame('Jurassic Park', $titles[0]);
}

/** @test */
Expand All @@ -152,19 +162,18 @@ public function it_filters_books_by_author_and_currency(): void

$this->client->request('GET', sprintf('/books/?criteria[author]=%d&criteria[currencyCode]=%s', $authorId, 'EUR'));

$this->assertCount(1, $this->getItemsFromCurrentResponse());
$this->assertSame('Jurassic Park', $this->getFirstItemFromCurrentResponse()['title']);
$titles = $this->getBookTitlesFromResponse();

$this->assertCount(1, $titles);
$this->assertSame('Jurassic Park', $titles[0]);
}

/** @test */
public function it_sorts_books_ascending_by_author(): void
{
$this->client->request('GET', '/books/?sorting[author]=asc&limit=100');

$items = $this->getItemsFromCurrentResponse();
$names = array_map(static function (array $item): string {
return $item['author']['name'];
}, $items);
$names = $this->getBookAuthorsFromResponse();

$sortedNames = $names;
sort($names);
Expand All @@ -177,10 +186,7 @@ public function it_sorts_books_descending_by_authors_nationality(): void
{
$this->client->request('GET', '/books/?sorting[nationality]=desc&limit=100');

$items = $this->getItemsFromCurrentResponse();
$names = array_map(static function (array $item): string {
return $item['author']['nationality']['name'];
}, $items);
$names = $this->getBookAuthorNationalitiesFromResponse();

$sortedNames = $names;
rsort($names);
Expand All @@ -195,16 +201,21 @@ public function it_filters_books_by_author_when_an_author_association_is_used_in

$this->client->request('GET', sprintf('/by-american-authors/books/?criteria[author]=%d', $authorId));

$this->assertCount(2, $this->getItemsFromCurrentResponse());
$this->assertSame('Jurassic Park', $this->getFirstItemFromCurrentResponse()['title']);
$titles = $this->getBookTitlesFromResponse();

$this->assertCount(2, $titles);
$this->assertSame('Jurassic Park', $titles[0]);
}

/** @test */
public function it_sorts_authors_using_table_alias_defined_in_query_builder(): void
{
$this->client->request('GET', '/by-american-authors/books/?sorting[author]=asc');

$this->assertResponse($this->client->getResponse(), 'american_authors_sorted_ascending');
$titles = $this->getBookTitlesFromResponse();

$this->assertCount(2, $titles);
$this->assertSame('Jurassic Park', $titles[0]);
}

/** @test */
Expand All @@ -214,40 +225,72 @@ public function it_filters_books_by_author_when_an_author_is_used_in_join_in_que

$this->client->request('GET', sprintf('/by-english-authors/books/?criteria[author]=%d', $authorId));

$this->assertCount(1, $this->getItemsFromCurrentResponse());
$this->assertSame('A Study in Scarlet', $this->getFirstItemFromCurrentResponse()['title']);
$titles = $this->getBookTitlesFromResponse();

$this->assertCount(1, $titles);
$this->assertSame('A Study in Scarlet', $titles[0]);
}

/** @test */
public function it_includes_all_rows_even_when_sorting_by_a_nullable_path(): void
{
$this->client->request('GET', '/authors/');
$totalItemsCountBeforeSorting = $this->getTotalItemsCountFromCurrentResponse();
$totalItemsCountBeforeSorting = count($this->getAuthorNamesFromResponse());

$this->client->request('GET', '/authors/?sorting[nationality]=desc');
$totalItemsCountAfterSorting = $this->getTotalItemsCountFromCurrentResponse();

$totalItemsCountAfterSorting = count($this->getAuthorNamesFromResponse());

$this->assertSame($totalItemsCountBeforeSorting, $totalItemsCountAfterSorting);
}

private function getTotalItemsCountFromCurrentResponse(): int
/** @return string[] */
private function getBookTitlesFromResponse(): array
{
return $this->getCrawler()
->filter('[data-test-title]')
->each(
fn (Crawler $node): string => $node->text(),
);
}

/** @return string[] */
private function getBookAuthorsFromResponse(): array
{
return json_decode($this->client->getResponse()->getContent(), true)['total'];
return $this->getCrawler()
->filter('[data-test-author]')
->each(
fn (Crawler $node): string => $node->text(),
);
}

private function getItemsFromCurrentResponse(): array
/** @return string[] */
private function getBookAuthorNationalitiesFromResponse(): array
{
return json_decode($this->client->getResponse()->getContent(), true)['_embedded']['items'];
return $this->getCrawler()
->filter('[data-test-nationality]')
->each(
fn (Crawler $node): string => $node->text(),
);
}

private function getFirstItemFromCurrentResponse(): array
/** @return string[] */
private function getAuthorNamesFromResponse(): array
{
return current($this->getItemsFromCurrentResponse());
return $this->getCrawler()
->filter('[data-test-name]')
->each(
fn (Crawler $node): string => $node->text(),
);
}

private function getLastItemFromCurrentResponse(): array
private function getCrawler(): Crawler
{
$result = $this->getItemsFromCurrentResponse();
return $this->client->getCrawler();
}

return end($result);
protected function buildMatcher(): Matcher
{
return $this->matcherFactory->createMatcher(new VoidBacktrace());
}
}
Loading

0 comments on commit 3b7010a

Please sign in to comment.