Skip to content

Commit

Permalink
[SF7] Fix PHPUnit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Sep 4, 2024
1 parent 6de5b05 commit 66338cf
Show file tree
Hide file tree
Showing 18 changed files with 95 additions and 14 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
experimental: false
- php: 8.3
symfony: "^7.0"
app_env: "test_without_fosrest"
app_env: "test_without_hateoas"
experimental: true
exclude:
- php: "8.1"
Expand Down Expand Up @@ -176,6 +176,7 @@ jobs:
-
name: Run lint container without friendsofsymfony/rest-bundle willdurand/hateoas-bundle jms/serializer-bundle packages
if: matrix.app_env == 'test'
run: |
composer remove --dev friendsofsymfony/rest-bundle willdurand/hateoas-bundle jms/serializer-bundle --no-scripts
(cd tests/Application && bin/console cache:clear --env=test_without_fosrest)
Expand All @@ -184,6 +185,7 @@ jobs:
-
name: Run lint container without winzou/state-machine-bundle package
if: matrix.app_env == 'test'
run: |
composer remove winzou/state-machine-bundle --no-scripts
(cd tests/Application && bin/console cache:clear --env=test_without_state_machine)
Expand All @@ -192,6 +194,7 @@ jobs:
-
name: Run lint container without twig/twig package
if: matrix.app_env == 'test'
run: |
composer remove symfony/twig-bundle --no-scripts
composer remove sylius/grid-bundle --no-scripts --dev
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Sylius\Resource\Metadata\HttpOperation;
use Sylius\Resource\Metadata\Operation\HttpOperationInitiatorInterface;
use Sylius\Resource\Symfony\EventListener\AddFormatListener;
use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
Expand Down Expand Up @@ -55,7 +56,7 @@ public function it_sets_format_from_accept_header(): void
$this->operationInitiator->initializeOperation($request)->willReturn($operation);

$request->attributes = new ParameterBag();
$request->headers = new ParameterBag(['Accept' => 'application/json']);
$request->headers = new HeaderBag(['Accept' => 'application/json']);

$request->getFormat('application/json')->willReturn('json')->shouldBeCalled();

Expand All @@ -76,7 +77,7 @@ public function it_sets_format_from_request(): void
$this->operationInitiator->initializeOperation($request)->willReturn($operation);

$request->attributes = new ParameterBag(['_format' => 'json']);
$request->headers = new ParameterBag();
$request->headers = new HeaderBag();

$request->getRequestFormat(null)->willReturn('json')->shouldBeCalled();

Expand All @@ -97,7 +98,7 @@ public function it_throws_an_exception_when_format_is_not_accepted(): void
$this->operationInitiator->initializeOperation($request)->willReturn($operation);

$request->attributes = new ParameterBag(['_format' => 'json-ld']);
$request->headers = new ParameterBag();
$request->headers = new HeaderBag();

$request->getRequestFormat(null)->willReturn('json-ld')->shouldBeCalled();

Expand Down
2 changes: 1 addition & 1 deletion tests/Application/config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
TwigBundle::class => ['all' => true, 'test_without_twig' => false],
FOSRestBundle::class => ['all' => true, 'test_without_fosrest' => false],
JMSSerializerBundle::class => ['all' => true, 'test_without_fosrest' => false],
BazingaHateoasBundle::class => ['all' => true, 'test_without_fosrest' => false],
BazingaHateoasBundle::class => ['all' => true, 'test_without_hateoas' => false, 'test_without_fosrest' => false, 'test_with_attributes' => false],
FidryAliceDataFixturesBundle::class => ['all' => true],
NelmioAliceBundle::class => ['all' => true],
winzouStateMachineBundle::class => ['all' => true, 'test_without_state_machine' => false],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: '../test/framework.yaml' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: '../test/grids.yaml' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: '../test/sylius_grid.yaml' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: '../test/winzou_state_machine.yaml' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: '../test/fos_rest.yaml' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: '../test/framework.yaml' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: '../test/grids.yaml' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: '../test/jms_serializer.yaml' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: '../test/sylius_grid.yaml' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: '../test/twig.yaml' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: '../test/winzou_state_machine.yaml' }
22 changes: 15 additions & 7 deletions tests/Application/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,22 @@ services:
- '%app.model.book_translation.class%'
tags: ['form.type']

Gedmo\Sortable\SortableListener:
calls:
-
setAnnotationReader: ['@?annotation_reader']
gedmo_doctrine_extensions.mapping.driver.attribute:
class: Gedmo\Mapping\Driver\AttributeReader
public: false

gedmo.listener.sortable:
class: Gedmo\Sortable\SortableListener
tags:
-
name: doctrine.event_subscriber
connection: default
- { name: doctrine.event_listener, event: 'onFlush' }
- { name: doctrine.event_listener, event: 'loadClassMetadata' }
- { name: doctrine.event_listener, event: 'prePersist' }
- { name: doctrine.event_listener, event: 'postPersist' }
- { name: doctrine.event_listener, event: 'preUpdate' }
- { name: doctrine.event_listener, event: 'postRemove' }
- { name: doctrine.event_listener, event: 'postFlush' }
calls:
- [ setAnnotationReader, [ "@gedmo_doctrine_extensions.mapping.driver.attribute" ] ]

App\Repository\ComicBookRepository: null
App\Repository\LegacyBookRepository: null
Expand Down
5 changes: 3 additions & 2 deletions tests/Application/src/Subscription/Entity/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@
#[Update]
#[Delete]
#[BulkDelete]
#[ApplyStateMachineTransition(stateMachineTransition: 'accept')]
#[ApplyStateMachineTransition(stateMachineTransition: 'reject')]
#[ApplyStateMachineTransition(stateMachineTransition: 'accept', stateMachineGraph: 'subscription')]
#[ApplyStateMachineTransition(stateMachineTransition: 'reject', stateMachineGraph: 'subscription')]
#[BulkUpdate(
shortName: 'bulk_accept',
stateMachineTransition: 'accept',
stateMachineGraph: 'subscription',
)]
#[Show(
template: 'subscription/show.html.twig',
Expand Down
31 changes: 31 additions & 0 deletions tests/Application/src/Tests/Controller/BookApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class BookApiTest extends JsonApiTestCase
*/
public function it_allows_creating_a_book()
{
$this->markAsSkippedIfNecessary();

$data =
<<<EOT
{
Expand Down Expand Up @@ -103,6 +105,8 @@ public function it_allows_removing_a_book()
*/
public function it_allows_showing_a_book()
{
$this->markAsSkippedIfNecessary();

$objects = $this->loadFixturesFromFile('books.yml');

$this->client->request('GET', '/books/' . $objects['book1']->getId());
Expand All @@ -115,6 +119,8 @@ public function it_allows_showing_a_book()
*/
public function it_allows_indexing_books()
{
$this->markAsSkippedIfNecessary();

$this->loadFixturesFromFile('books.yml');

$this->client->request('GET', '/books/');
Expand All @@ -127,6 +133,8 @@ public function it_allows_indexing_books()
*/
public function it_allows_paginating_the_index_of_books()
{
$this->markAsSkippedIfNecessary();

$this->loadFixturesFromFile('more_books.yml');

$this->client->request('GET', '/books/', ['page' => 2]);
Expand All @@ -139,6 +147,8 @@ public function it_allows_paginating_the_index_of_books()
*/
public function it_does_not_allow_showing_resource_if_it_not_exists()
{
$this->markAsSkippedIfNecessary();

$this->loadFixturesFromFile('books.yml');

$this->client->request('GET', '/books/3');
Expand All @@ -151,6 +161,8 @@ public function it_does_not_allow_showing_resource_if_it_not_exists()
*/
public function it_does_not_apply_sorting_for_un_existing_field()
{
$this->markAsSkippedIfNecessary();

$this->loadFixturesFromFile('more_books.yml');

$this->client->request('GET', '/sortable-books/', ['sorting' => ['name' => 'DESC']]);
Expand All @@ -164,6 +176,8 @@ public function it_does_not_apply_sorting_for_un_existing_field()
*/
public function it_does_not_apply_filtering_for_un_existing_field()
{
$this->markAsSkippedIfNecessary();

$this->loadFixturesFromFile('more_books.yml');

$this->client->request('GET', '/filterable-books/', ['criteria' => ['name' => 'John']]);
Expand All @@ -177,6 +191,8 @@ public function it_does_not_apply_filtering_for_un_existing_field()
*/
public function it_applies_sorting_for_existing_field()
{
$this->markAsSkippedIfNecessary();

$this->loadFixturesFromFile('more_books.yml');

$this->client->request('GET', '/sortable-books/', ['sorting' => ['id' => 'DESC']]);
Expand All @@ -190,6 +206,8 @@ public function it_applies_sorting_for_existing_field()
*/
public function it_applies_filtering_for_existing_field()
{
$this->markAsSkippedIfNecessary();

$this->loadFixturesFromFile('more_books.yml');

$this->client->request('GET', '/filterable-books/', ['criteria' => ['author' => 'J.R.R. Tolkien']]);
Expand All @@ -203,6 +221,8 @@ public function it_applies_filtering_for_existing_field()
*/
public function it_allows_creating_a_book_via_custom_factory()
{
$this->markAsSkippedIfNecessary();

$data =
<<<EOT
{
Expand All @@ -225,6 +245,8 @@ public function it_allows_creating_a_book_via_custom_factory()
*/
public function it_allows_indexing_books_via_custom_repository(): void
{
$this->markAsSkippedIfNecessary();

$this->loadFixturesFromFile('books.yml');

$this->client->request('GET', '/find-custom-books');
Expand All @@ -237,10 +259,19 @@ public function it_allows_indexing_books_via_custom_repository(): void
*/
public function it_allows_showing_a_book_via_custom_repository()
{
$this->markAsSkippedIfNecessary();

$this->loadFixturesFromFile('books.yml');

$this->client->request('GET', '/find-custom-book');
$response = $this->client->getResponse();
$this->assertResponse($response, 'books/show_response');
}

private function markAsSkippedIfNecessary(): void
{
if ('test_without_hateoas' === self::$sharedKernel->getEnvironment()) {
$this->markTestSkipped();
}
}
}
15 changes: 15 additions & 0 deletions tests/Application/src/Tests/Controller/ComicBookApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function it_allows_creating_a_comic_book()
*/
public function it_allows_versioned_creating_a_comic_book()
{
$this->markAsSkippedIfNecessary();

$data =
<<<EOT
{
Expand Down Expand Up @@ -134,6 +136,8 @@ public function it_allows_showing_a_comic_book()
*/
public function it_allows_versioning_of_a_showing_comic_book_serialization()
{
$this->markAsSkippedIfNecessary();

$objects = $this->loadFixturesFromFile('comic_books.yml');

$this->client->request('GET', '/v1.2/comic-books/' . $objects['comic-book1']->getId());
Expand All @@ -146,6 +150,8 @@ public function it_allows_versioning_of_a_showing_comic_book_serialization()
*/
public function it_allows_indexing_of_comic_books()
{
$this->markAsSkippedIfNecessary();

$this->loadFixturesFromFile('comic_books.yml');

$this->client->request('GET', '/v1/comic-books/');
Expand All @@ -158,6 +164,8 @@ public function it_allows_indexing_of_comic_books()
*/
public function it_allows_versioned_indexing_of_comic_books()
{
$this->markAsSkippedIfNecessary();

$this->loadFixturesFromFile('comic_books.yml');

$this->client->request('GET', '/v1.2/comic-books/');
Expand All @@ -176,4 +184,11 @@ public function it_does_not_allow_showing_resource_if_it_does_not_exist()
$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_NOT_FOUND);
}

private function markAsSkippedIfNecessary(): void
{
if ('test_without_hateoas' === self::$sharedKernel->getEnvironment()) {
$this->markTestSkipped();
}
}
}

0 comments on commit 66338cf

Please sign in to comment.