From 5465d77143eeb9eac6b312360dddcf8036d7db1a Mon Sep 17 00:00:00 2001 From: BentiGorlich Date: Tue, 27 Jan 2026 22:24:37 +0100 Subject: [PATCH] Improve DI for testing If we're in the test environment we should never load `ApHttpClient` or `ImageManager` because these classes actually request things from other servers, which we do not want while testing. Instead we want to automatically get our testing services. This PR autowires the testing services when we're in the test environment --- config/services.yaml | 5 +++++ src/Service/ActivityPub/ApHttpClient.php | 2 ++ src/Service/ImageManager.php | 2 ++ tests/Service/TestingApHttpClient.php | 2 ++ tests/WebTestCase.php | 24 +++++++----------------- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index 6fc3186c7b..459c4c8a3c 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -251,3 +251,8 @@ services: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler: arguments: - '%env(DATABASE_URL)%' +when@test: + services: + # Testing + App\Tests\Service\: + resource: '../tests/Service/' diff --git a/src/Service/ActivityPub/ApHttpClient.php b/src/Service/ActivityPub/ApHttpClient.php index 8886198375..40fe65e012 100644 --- a/src/Service/ActivityPub/ApHttpClient.php +++ b/src/Service/ActivityPub/ApHttpClient.php @@ -19,6 +19,7 @@ use Psr\Cache\InvalidArgumentException; use Psr\Log\LoggerInterface; use Symfony\Component\Cache\CacheItem; +use Symfony\Component\DependencyInjection\Attribute\WhenNot; use Symfony\Component\HttpClient\CurlHttpClient; use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Cache\ItemInterface; @@ -41,6 +42,7 @@ enum ApRequestType case NodeInfo; } +#[WhenNot(env: 'test')] class ApHttpClient implements ApHttpClientInterface { public const TIMEOUT = 8; diff --git a/src/Service/ImageManager.php b/src/Service/ImageManager.php index 39116f265b..c14813015b 100644 --- a/src/Service/ImageManager.php +++ b/src/Service/ImageManager.php @@ -8,12 +8,14 @@ use App\Exception\ImageDownloadTooLargeException; use League\Flysystem\FilesystemOperator; use Psr\Log\LoggerInterface; +use Symfony\Component\DependencyInjection\Attribute\WhenNot; use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; use Symfony\Component\Mime\MimeTypesInterface; use Symfony\Component\Validator\Constraints\Image; use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; +#[WhenNot(env: 'test')] class ImageManager implements ImageManagerInterface { public const IMAGE_MIMETYPES = [ diff --git a/tests/Service/TestingApHttpClient.php b/tests/Service/TestingApHttpClient.php index 9180f50ebc..b98071e76f 100644 --- a/tests/Service/TestingApHttpClient.php +++ b/tests/Service/TestingApHttpClient.php @@ -7,7 +7,9 @@ use App\Entity\Magazine; use App\Entity\User; use App\Service\ActivityPub\ApHttpClientInterface; +use Symfony\Component\DependencyInjection\Attribute\When; +#[When(env: 'test')] class TestingApHttpClient implements ApHttpClientInterface { /** diff --git a/tests/WebTestCase.php b/tests/WebTestCase.php index 9e248cef3b..7197ce6278 100644 --- a/tests/WebTestCase.php +++ b/tests/WebTestCase.php @@ -60,7 +60,6 @@ use App\Tests\Service\TestingImageManager; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\EntityManagerInterface; -use League\Flysystem\Filesystem; use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Console\Application; @@ -69,11 +68,8 @@ use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Messenger\MessageBusInterface; -use Symfony\Component\Mime\MimeTypesInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\RouterInterface; -use Symfony\Component\Validator\Validator\ValidatorInterface; -use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\Translation\TranslatorInterface; abstract class WebTestCase extends BaseWebTestCase @@ -182,20 +178,14 @@ public function setUp(): void $this->kibbyPath = \dirname(__FILE__).'/assets/kibby_emoji.png'; $this->client = static::createClient(); - $this->testingApHttpClient = new TestingApHttpClient(); - self::getContainer()->set(ApHttpClientInterface::class, $this->testingApHttpClient); - - $this->imageManager = new TestingImageManager( - $this->getContainer()->getParameter('kbin_storage_url'), - $this->getService(Filesystem::class), - $this->getService(HttpClientInterface::class), - $this->getService(MimeTypesInterface::class), - $this->getService(ValidatorInterface::class), - $this->getService(LoggerInterface::class), - $this->getService(SettingsManager::class), - ); + $client = $this->getService(ApHttpClientInterface::class); + self::assertTrue($client instanceof TestingApHttpClient); + $this->testingApHttpClient = $client; + + $imageManager = $this->getService(ImageManagerInterface::class); + self::assertTrue($imageManager instanceof TestingImageManager); + $this->imageManager = $imageManager; $this->imageManager->setKibbyPath($this->kibbyPath); - self::getContainer()->set(ImageManagerInterface::class, $this->imageManager); $this->entityManager = $this->getService(EntityManagerInterface::class); $this->magazineManager = $this->getService(MagazineManager::class);