Skip to content

Commit

Permalink
inject CustomTagCollector into behat tests
Browse files Browse the repository at this point in the history
  • Loading branch information
usu committed Oct 14, 2023
1 parent c935d8d commit 889a614
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 8 deletions.
1 change: 1 addition & 0 deletions features/http_cache/tag_collector_service.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@sqlite
@customTagCollector
Feature: Cache invalidation through HTTP Cache tags (custom TagCollector service)
In order to have a fast API
As an API software developer
Expand Down
6 changes: 3 additions & 3 deletions features/http_cache/tags.feature
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Feature: Cache invalidation through HTTP Cache tags
Scenario: Tags must be set for items
When I send a "GET" request to "/relation_embedders/1"
Then the response status code should be 200
And the header "Cache-Tags" should be equal to "/relation_embedders/1,/related_dummies/1,/third_levels/1"
And the header "Cache-Tags" should be equal to "/third_levels/1,/related_dummies/1,/relation_embedders/1"

Scenario: Create some more resources
When I add "Content-Type" header equal to "application/ld+json"
Expand All @@ -42,7 +42,7 @@ Feature: Cache invalidation through HTTP Cache tags
Scenario: Tags must be set for collections
When I send a "GET" request to "/relation_embedders"
Then the response status code should be 200
And the header "Cache-Tags" should be equal to "/relation_embedders/1,/related_dummies/1,/third_levels/1,/relation_embedders/2,/related_dummies/2,/third_levels/2,/relation_embedders"
And the header "Cache-Tags" should be equal to "/third_levels/1,/related_dummies/1,/relation_embedders/1,/third_levels/2,/related_dummies/2,/relation_embedders/2,/relation_embedders"

Scenario: Purge item on update
When I add "Content-Type" header equal to "application/ld+json"
Expand Down Expand Up @@ -119,7 +119,7 @@ Feature: Cache invalidation through HTTP Cache tags
When I add "Content-Type" header equal to "application/ld+json"
And I send a "GET" request to "/relation3s"
Then the response status code should be 200
And the header "Cache-Tags" should be equal to "/relation3s/1,/relation2s/1,/relation2s/2,/relation3s"
And the header "Cache-Tags" should be equal to "/relation2s/1,/relation2s/2,/relation3s/1,/relation3s"

Scenario: Update a collection member only (legacy non-standard PUT)
When I add "Content-Type" header equal to "application/ld+json"
Expand Down
34 changes: 31 additions & 3 deletions tests/Behat/HttpCacheContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,31 @@

namespace ApiPlatform\Tests\Behat;

use Behat\Behat\Context\Context;
use PHPUnit\Framework\ExpectationFailedException;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use PHPUnit\Framework\ExpectationFailedException;
use Behat\MinkExtension\Context\MinkContext;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Behat\Context\Context;
use ApiPlatform\Tests\Fixtures\TestBundle\HttpCache\TagCollectorDefault;
use ApiPlatform\Tests\Fixtures\TestBundle\HttpCache\TagCollectorCustom;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
final class HttpCacheContext implements Context
{
public function __construct(private readonly KernelInterface $kernel)
public function __construct(private readonly KernelInterface $kernel, private ContainerInterface $driverContainer)
{
}

/**
* @BeforeScenario @customTagCollector
*/
public function registerCustomTagCollector(BeforeScenarioScope $scope): void
{
$this->disableReboot($scope);
$this->driverContainer->set('api_platform.http_cache.tag_collector', new TagCollectorCustom());
}

/**
Expand All @@ -40,4 +54,18 @@ public function irisShouldBePurged(string $iris): void
throw new ExpectationFailedException(sprintf('IRIs "%s" does not match expected "%s".', $purgedIris, $iris));
}
}

/**
* this is necessary to allow overriding services
* see https://github.com/FriendsOfBehat/SymfonyExtension/issues/149 for details
*/
private function disableReboot(BeforeScenarioScope $scope){

/** @var MinkContext $minkContext */
$minkContext = $scope->getEnvironment()->getContext(MinkContext::class);

Check failure on line 65 in tests/Behat/HttpCacheContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.2)

Call to an undefined method Behat\Testwork\Environment\Environment::getContext().
$client = $minkContext->getSession()->getDriver()->getClient();

Check failure on line 66 in tests/Behat/HttpCacheContext.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.2)

Call to an undefined method Behat\Mink\Driver\DriverInterface::getClient().
$client->disableReboot();


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
* @author Urban Suppiger <urban@suppiger.net>
*/
class TagCollector implements TagCollectorInterface
class TagCollectorCustom implements TagCollectorInterface
{
public const IRI_RELATION_DELIMITER = '#';

Expand Down
41 changes: 41 additions & 0 deletions tests/Fixtures/TestBundle/HttpCache/TagCollectorDefault.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\HttpCache;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Serializer\TagCollectorInterface;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelationEmbedder;
use Symfony\Component\PropertyInfo\Type;

/**
* Collects cache tags during normalization.
*
* @author Urban Suppiger <urban@suppiger.net>
*/
class TagCollectorDefault implements TagCollectorInterface
{
public function collect(mixed $object = null, string $format = null, array $context = [], string $iri = null, mixed $data = null, string $attribute = null, ApiProperty $propertyMetadata = null, Type $type = null): void
{
if (!$attribute) {
$this->addResourceToContext($context, $iri);
}
}

private function addResourceToContext(array $context, ?string $iri): void
{
if (isset($context['resources']) && isset($iri)) {
$context['resources'][$iri] = $iri;
}
}
}
4 changes: 3 additions & 1 deletion tests/Fixtures/app/config/config_common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -425,5 +425,7 @@ services:
- { name: 'serializer.normalizer' }

api_platform.http_cache.tag_collector:
class: ApiPlatform\Tests\Fixtures\TestBundle\HttpCache\TagCollector
class: ApiPlatform\Tests\Fixtures\TestBundle\HttpCache\TagCollectorDefault
public: true


0 comments on commit 889a614

Please sign in to comment.