Skip to content

Commit

Permalink
cleanup phpunit tests now that we don't need ancient legacy support a…
Browse files Browse the repository at this point in the history
…nymore
  • Loading branch information
dbu committed Jan 6, 2024
1 parent fcc9ef0 commit 25c7586
Show file tree
Hide file tree
Showing 57 changed files with 503 additions and 1,296 deletions.
10 changes: 1 addition & 9 deletions src/DependencyInjection/Compiler/AssetsVersionCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,12 @@ public function process(ContainerBuilder $container): void
$version = $versionStrategyDefinition->getArgument(0);
$format = $versionStrategyDefinition->getArgument(1);
$format = $container->resolveEnvPlaceholders($format);
if ($format && !$this->str_ends_with($format, '?%%s')) {
if ($format && !str_ends_with($format, '?%%s')) {
$this->log($container, 'Can not handle assets versioning with custom format "'.$format.'". asset twig function can likely not be used with the imagine_filter');

return;
}

$runtimeDefinition->setArgument(1, $version);
}

/**
* Can be replaced with the built-in method when dropping support for PHP < 8.0
*/
private function str_ends_with(string $haystack, string $needle): bool
{
return mb_substr($haystack, -mb_strlen($needle)) === $needle;
}
}
230 changes: 1 addition & 229 deletions tests/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,8 @@

namespace Liip\ImagineBundle\Tests;

use Imagine\Image\ImageInterface;
use Imagine\Image\ImagineInterface;
use Imagine\Image\Metadata\MetadataBag;
use Liip\ImagineBundle\Binary\Loader\LoaderInterface;
use Liip\ImagineBundle\Binary\MimeTypeGuesserInterface;
use Liip\ImagineBundle\Config\Controller\ControllerConfig;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface;
use Liip\ImagineBundle\Imagine\Cache\SignerInterface;
use Liip\ImagineBundle\Imagine\Data\DataManager;
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
use Liip\ImagineBundle\Imagine\Filter\PostProcessor\PostProcessorInterface;
use Liip\ImagineBundle\Service\FilterService;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Mime\MimeTypesInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

abstract class AbstractTest extends TestCase
{
Expand Down Expand Up @@ -65,216 +46,7 @@ protected function tearDown(): void
}
}

/**
* @return string[]
*/
public function invalidPathProvider(): array
{
return [
[$this->fixturesPath.'/assets/../../foobar.png'],
[$this->fixturesPath.'/assets/some_folder/../foobar.png'],
['../../outside/foobar.jpg'],
];
}

public function expectExceptionMessageMatchesBC(string $regularExpression): void
{
if (method_exists($this, 'expectExceptionMessageMatches')) {
$this->expectExceptionMessageMatches($regularExpression);
} else {
$this->expectExceptionMessageRegExp($regularExpression);
}
}

protected function createFilterConfiguration(): FilterConfiguration
{
$config = new FilterConfiguration();
$config->set('thumbnail', [
'size' => [180, 180],
'mode' => 'outbound',
]);

return $config;
}

/**
* @return MockObject&CacheManager
*/
protected function createCacheManagerMock()
{
return $this
->getMockBuilder(CacheManager::class)
->setConstructorArgs([
$this->createFilterConfiguration(),
$this->createRouterInterfaceMock(),
$this->createSignerInterfaceMock(),
$this->createEventDispatcherInterfaceMock(),
])
->getMock();
}

/**
* @return MockObject&FilterConfiguration
*/
protected function createFilterConfigurationMock()
{
return $this->createObjectMock(FilterConfiguration::class);
}

/**
* @return MockObject&SignerInterface
*/
protected function createSignerInterfaceMock()
{
return $this->createObjectMock(SignerInterface::class);
}

/**
* @return MockObject&RouterInterface
*/
protected function createRouterInterfaceMock()
{
return $this->createObjectMock(RouterInterface::class);
}

/**
* @return MockObject&ResolverInterface
*/
protected function createCacheResolverInterfaceMock()
{
return $this->createObjectMock(ResolverInterface::class);
}

/**
* @return MockObject&EventDispatcherInterface
*/
protected function createEventDispatcherInterfaceMock()
{
return $this->createObjectMock(EventDispatcherInterface::class);
}

/**
* @return MockObject&ImageInterface
*/
protected function getImageInterfaceMock()
{
return $this->createObjectMock(ImageInterface::class);
}

/**
* @return MockObject&MetadataBag
*/
protected function getMetadataBagMock()
{
return $this->createObjectMock(MetadataBag::class);
}

/**
* @return MockObject&ImagineInterface
*/
protected function createImagineInterfaceMock()
{
return $this->createObjectMock(ImagineInterface::class);
}

/**
* @return MockObject&LoggerInterface
*/
protected function createLoggerInterfaceMock()
{
return $this->createObjectMock(LoggerInterface::class);
}

/**
* @return MockObject&LoaderInterface
*/
protected function createBinaryLoaderInterfaceMock()
{
return $this->createObjectMock(LoaderInterface::class);
}

/**
* @return MockObject&MimeTypeGuesserInterface
*/
protected function createMimeTypeGuesserInterfaceMock()
{
return $this->createObjectMock(MimeTypeGuesserInterface::class);
}

/**
* @return MockObject&MimeTypesInterface
*/
protected function createExtensionGuesserInterfaceMock()
{
return $this->createObjectMock(MimeTypesInterface::class);
}

/**
* @return MockObject&PostProcessorInterface
*/
protected function createPostProcessorInterfaceMock()
{
return $this->createObjectMock(PostProcessorInterface::class);
}

/**
* @return MockObject&FilterManager
*/
protected function createFilterManagerMock()
{
return $this->createObjectMock(FilterManager::class, [], false);
}

/**
* @return MockObject&FilterService
*/
protected function createFilterServiceMock()
{
return $this->createObjectMock(FilterService::class);
}

/**
* @return MockObject&DataManager
*/
protected function createDataManagerMock()
{
return $this->createObjectMock(DataManager::class, [], false);
}

protected function createControllerConfigInstance(int $redirectResponseCode = null): ControllerConfig
{
return new ControllerConfig($redirectResponseCode ?? 301);
}

/**
* @param string[] $methods
* @param mixed[] $constructorParams
*/
protected function createObjectMock(string $object, array $methods = [], bool $constructorInvoke = false, array $constructorParams = []): MockObject
{
$builder = $this->getMockBuilder($object);

if (\count($methods) > 0) {
$builder->setMethods($methods);
}

if ($constructorInvoke) {
$builder->enableOriginalConstructor();
} else {
$builder->disableOriginalConstructor();
}

if (\count($constructorParams) > 0) {
$builder->setConstructorArgs($constructorParams);
}

return $builder->getMock();
}

/**
* @param object $object
*/
protected function getVisibilityRestrictedMethod($object, string $name): \ReflectionMethod
protected function getVisibilityRestrictedMethod(object $object, string $name): \ReflectionMethod
{
$r = new \ReflectionObject($object);

Expand Down
4 changes: 1 addition & 3 deletions tests/Binary/Loader/AbstractDoctrineLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ class AbstractDoctrineLoaderTest extends TestCase

protected function setUp(): void
{
$this->om = $this
->getMockBuilder(ObjectManager::class)
->getMock();
$this->om = $this->createMock(ObjectManager::class);

$this->loader = $this
->getMockBuilder(AbstractDoctrineLoader::class)
Expand Down
4 changes: 2 additions & 2 deletions tests/Binary/Loader/ChainLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function provideInvalidPathsData(): array
public function testThrowsIfFileDoesNotExist(string $path): void
{
$this->expectException(NotLoadableException::class);
$this->expectExceptionMessageMatchesBC('{Source image not resolvable "[^"]+" using "FileSystemLoader=\[foo\]" 1 loaders}');
$this->expectExceptionMessageMatches('{Source image not resolvable "[^"]+" using "FileSystemLoader=\[foo\]" 1 loaders}');

$this->getChainLoader()->find($path);
}
Expand All @@ -102,7 +102,7 @@ public function testThrowsIfFileDoesNotExist(string $path): void
public function testThrowsIfFileDoesNotExistWithMultipleLoaders(string $path): void
{
$this->expectException(NotLoadableException::class);
$this->expectExceptionMessageMatchesBC('{Source image not resolvable "[^"]+" using "FileSystemLoader=\[foo\], FileSystemLoader=\[bar\]" 2 loaders \(internal exceptions: FileSystemLoader=\[.+\], FileSystemLoader=\[.+\]\)\.}');
$this->expectExceptionMessageMatches('{Source image not resolvable "[^"]+" using "FileSystemLoader=\[foo\], FileSystemLoader=\[bar\]" 2 loaders \(internal exceptions: FileSystemLoader=\[.+\], FileSystemLoader=\[.+\]\)\.}');

$this->getChainLoader([], [
'foo' => $this->createFileSystemLoader(
Expand Down
70 changes: 0 additions & 70 deletions tests/Binary/Loader/FlysystemLoaderTest.php

This file was deleted.

6 changes: 1 addition & 5 deletions tests/Binary/Loader/FlysystemV2LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ protected function setUp(): void
{
parent::setUp();

if (!interface_exists(FilesystemOperator::class)) {
$this->markTestSkipped('Requires the league/flysystem:^2.0 package.');
}

$this->flyFilesystem = new Filesystem(new LocalFilesystemAdapter($this->fixturesPath));
}

Expand All @@ -57,7 +53,7 @@ public function testReturnImageContentOnFind(): void
public function testThrowsIfInvalidPathGivenOnFind(): void
{
$this->expectException(NotLoadableException::class);
$this->expectExceptionMessageMatchesBC('{Source image .+ not found}');
$this->expectExceptionMessageMatches('{Source image .+ not found}');

$loader = $this->getFlysystemLoader();

Expand Down
2 changes: 1 addition & 1 deletion tests/Binary/Loader/StreamLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class StreamLoaderTest extends AbstractTest
public function testThrowsIfInvalidPathGivenOnFind(): void
{
$this->expectException(\Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException::class);
$this->expectExceptionMessageMatchesBC('{Source image file://.+ not found.}');
$this->expectExceptionMessageMatches('{Source image file://.+ not found.}');

$loader = new StreamLoader('file://');
$loader->find($this->temporaryPath.'/invalid.jpeg');
Expand Down
Loading

0 comments on commit 25c7586

Please sign in to comment.