Skip to content

Commit

Permalink
Test grids with UI instead of API
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Jun 4, 2024
1 parent d01dbfb commit 8910e60
Show file tree
Hide file tree
Showing 33 changed files with 303 additions and 135 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,22 @@ 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)

-
name: Run tests for grids with php config
if: matrix.symfony == '^5.3'
run: |
(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": "dev-poc-new-resource-metadata",
"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,9 +13,13 @@

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;
Expand All @@ -31,19 +35,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->getAuthorNames());
}

/** @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->getAuthorNames();

$sortedNames = $names;
sort($names);
Expand All @@ -56,10 +60,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->getAuthorNames();

$sortedNames = $names;
rsort($names);
Expand All @@ -72,19 +73,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->getAuthorNames());
}

/** @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->getAuthorNames());

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

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

/** @test */
Expand All @@ -95,8 +96,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->getBookTitles();

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

/** @test */
Expand All @@ -107,8 +110,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->getBookTitles();

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

/** @test */
Expand All @@ -118,8 +123,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->getBookTitles();

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

/** @test */
Expand All @@ -130,8 +137,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->getBookTitles();

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

/** @test */
Expand All @@ -141,8 +150,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->getBookTitles();

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

/** @test */
Expand All @@ -152,19 +163,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->getBookTitles();

$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->getBookAuthors();

$sortedNames = $names;
sort($names);
Expand All @@ -177,10 +187,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->getBookAuthorNationalities();

$sortedNames = $names;
rsort($names);
Expand All @@ -195,16 +202,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->getBookTitles();

$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->getBookTitles();

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

/** @test */
Expand All @@ -214,40 +226,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->getBookTitles();

$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->getAuthorNames());

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

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

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

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

/** @return string[] */
private function getBookAuthors(): 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 getBookAuthorNationalities(): 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 getAuthorNames(): 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 8910e60

Please sign in to comment.