Skip to content

Commit

Permalink
Use better asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Nov 22, 2019
1 parent b219ae5 commit 9c929ee
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ env:
- SYMFONY_DEPRECATIONS_HELPER="max[self]=0"
- PHPUNIT_FLAGS="-v"
- PHPUNIT_ENABLED="true"
- SYMFONY_PHPUNIT_VERSION="6.5"
- SYMFONY_PHPUNIT_VERSION="7.5"
- COVERALLS_ENABLED="false"
- STABILITY=stable

Expand Down
1 change: 1 addition & 0 deletions Tests/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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\EventDispatcher\EventDispatcherInterface;
Expand Down
18 changes: 9 additions & 9 deletions Tests/Async/ResolveCacheProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,32 @@ public static function setUpBeforeClass()
}
}

public function testShouldImplementProcessorInterface()
public function testShouldImplementProcessorInterface(): void
{
$rc = new \ReflectionClass(ResolveCacheProcessor::class);

$this->assertTrue($rc->implementsInterface(Processor::class));
}

public function testShouldImplementCommandSubscriberInterface()
public function testShouldImplementCommandSubscriberInterface(): void
{
$rc = new \ReflectionClass(ResolveCacheProcessor::class);

$this->assertTrue($rc->implementsInterface(CommandSubscriberInterface::class));
}

public function testShouldImplementQueueSubscriberInterface()
public function testShouldImplementQueueSubscriberInterface(): void
{
$rc = new \ReflectionClass(ResolveCacheProcessor::class);

$this->assertTrue($rc->implementsInterface(QueueSubscriberInterface::class));
}

public function testShouldSubscribeToExpectedCommand()
public function testShouldSubscribeToExpectedCommand(): void
{
$command = ResolveCacheProcessor::getSubscribedCommand();

$this->assertInternalType('array', $command);
$this->assertIsArray($command);
$this->assertSame([
'command' => Commands::RESOLVE_CACHE,
'queue' => Commands::RESOLVE_CACHE,
Expand All @@ -75,15 +75,15 @@ public function testShouldSubscribeToExpectedCommand()
], $command);
}

public function testShouldSubscribeToExpectedQueue()
public function testShouldSubscribeToExpectedQueue(): void
{
$queues = ResolveCacheProcessor::getSubscribedQueues();

$this->assertInternalType('array', $queues);
$this->assertIsArray($queues);
$this->assertSame(['liip_imagine_resolve_cache'], $queues);
}

public function testCouldBeConstructedWithExpectedArguments()
public function testCouldBeConstructedWithExpectedArguments(): void
{
$processor = new ResolveCacheProcessor(
$this->createFilterManagerMock(),
Expand All @@ -94,7 +94,7 @@ public function testCouldBeConstructedWithExpectedArguments()
$this->assertInstanceOf(ResolveCacheProcessor::class, $processor);
}

public function testShouldRejectMessagesWithInvalidJsonBody()
public function testShouldRejectMessagesWithInvalidJsonBody(): void
{
$processor = new ResolveCacheProcessor(
$this->createFilterManagerMock(),
Expand Down
26 changes: 13 additions & 13 deletions Tests/Component/Console/Style/ImagineStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testText(string $format, array $replacements): void
$style = $this->createImagineStyle($output = $this->createBufferedOutput());
$style->text($format, $replacements);

$this->assertContains(vsprintf($format, $replacements), $output->getBuffer());
$this->assertStringContainsString(vsprintf($format, $replacements), $output->getBuffer());
}

public static function provideTextData(): Generator
Expand All @@ -51,7 +51,7 @@ public function testTextWithoutDecoration(string $format, array $replacements):
$style = $this->createImagineStyle($output = $this->createBufferedOutput(), false);
$style->text($format, $replacements);

$this->assertContains(strip_tags(vsprintf($format, $replacements)), $output->getBuffer());
$this->assertStringContainsString(strip_tags(vsprintf($format, $replacements)), $output->getBuffer());
}

public static function provideTextWithoutDecorationData(): Generator
Expand All @@ -67,7 +67,7 @@ public function testLine(string $format, array $replacements): void
$style = $this->createImagineStyle($output = $this->createBufferedOutput());
$style->line($format, $replacements);

$this->assertContains(vsprintf($format, $replacements).PHP_EOL, $output->getBuffer());
$this->assertStringContainsString(vsprintf($format, $replacements).PHP_EOL, $output->getBuffer());
}

public static function provideLineData(): Generator
Expand All @@ -85,7 +85,7 @@ public function testNewline(int $newlineCount, string $separator): void
$style->newline($newlineCount);
$style->text($separator);

$this->assertContains(sprintf('%1$s%2$s%1$s', $separator, str_repeat(PHP_EOL, $newlineCount)), $output->getBuffer());
$this->assertStringContainsString(sprintf('%1$s%2$s%1$s', $separator, str_repeat(PHP_EOL, $newlineCount)), $output->getBuffer());
}

public static function provideNewlineData(): Generator
Expand Down Expand Up @@ -117,7 +117,7 @@ public function testTitle(string $title, string $type = null, bool $decoration):
}
}

$this->assertContains($expected, $output->getBuffer());
$this->assertStringContainsString($expected, $output->getBuffer());
}

public static function provideTitleData(): Generator
Expand Down Expand Up @@ -148,7 +148,7 @@ public function testBlockTypes(string $type, string $expectedFormat, string $for
$style->{$blockMethod}($format, $replacements);
$compiled = vsprintf(strip_tags($format), $replacements);

$this->assertContains(sprintf($expectedFormat, $compiled), $output->getBuffer());
$this->assertStringContainsString(sprintf($expectedFormat, $compiled), $output->getBuffer());
}

public static function provideBlockTypesData(): Generator
Expand Down Expand Up @@ -178,11 +178,11 @@ public static function provideBlockTypesData(): Generator
public function testStatus(string $status, string $fg = null, string $bg = null): void
{
$style = $this->createImagineStyle($output = $this->createBufferedOutput());
$style->status($status, $fg, $bg);
$style->status($status, $fg);

$this->assertContains($fg ?: 'default', $output->getBuffer());
$this->assertContains($bg ?: 'default', $output->getBuffer());
$this->assertContains(sprintf('(%s)', $status), strip_tags($output->getBuffer()));
$this->assertStringContainsString($fg ?: 'default', $output->getBuffer());
$this->assertStringContainsString($bg ?: 'default', $output->getBuffer());
$this->assertStringContainsString(sprintf('(%s)', $status), strip_tags($output->getBuffer()));
}

public static function provideStatusData(): Generator
Expand All @@ -204,9 +204,9 @@ public function testGroup(string $item, string $group, string $fg = null, string
$style = $this->createImagineStyle($output = $this->createBufferedOutput());
$style->group($item, $group, $fg, $bg);

$this->assertContains($fg ?: 'default', $output->getBuffer());
$this->assertContains($bg ?: 'default', $output->getBuffer());
$this->assertContains(sprintf('%s[%s]', $item, $group), strip_tags($output->getBuffer()));
$this->assertStringContainsString($fg ?: 'default', $output->getBuffer());
$this->assertStringContainsString($bg ?: 'default', $output->getBuffer());
$this->assertStringContainsString(sprintf('%s[%s]', $item, $group), strip_tags($output->getBuffer()));
}

public static function provideGroupData(): Generator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Liip\ImagineBundle\DependencyInjection\Compiler\AbstractCompilerPass;
use Liip\ImagineBundle\Tests\AbstractTest;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

Expand Down Expand Up @@ -77,7 +78,7 @@ protected function createContainerBuilderMock(array $methods = [], array $defini
* @param \PHPUnit_Framework_MockObject_MockObject $container
* @param mixed[] ...$expectedArguments
*/
protected function expectContainerLogMethodCalledOnce(\PHPUnit_Framework_MockObject_MockObject $container, ...$expectedArguments): void
protected function expectContainerLogMethodCalledOnce(MockObject $container, ...$expectedArguments): void
{
$expectation = $container
->expects($this->once())
Expand Down
12 changes: 6 additions & 6 deletions Tests/Events/CacheResolveEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public function testShouldAllowSetPathInConstruct()
{
$event = new CacheResolveEvent('default_path', 'default_filter');

$this->assertAttributeSame('default_path', 'path', $event);
$this->assertSame('default_path', $event->getPath());
}

public function testShouldAllowSetPathByMethod()
{
$event = new CacheResolveEvent('default_path', 'default_filter');
$event->setPath('new_path');

$this->assertAttributeSame('new_path', 'path', $event);
$this->assertSame('new_path', $event->getPath());
}

public function testShouldAllowGetPathWhichWasSetInConstruct()
Expand All @@ -57,15 +57,15 @@ public function testShouldAllowSetFilterInConstruct()
{
$event = new CacheResolveEvent('default_path', 'default_filter');

$this->assertAttributeSame('default_filter', 'filter', $event);
$this->assertSame('default_filter', $event->getFilter());
}

public function testShouldAllowSetFilterByMethod()
{
$event = new CacheResolveEvent('default_path', 'default_filter');
$event->setFilter('new_filter');

$this->assertAttributeSame('new_filter', 'filter', $event);
$this->assertSame('new_filter', $event->getFilter());
}

public function testShouldAllowGetFilterWhichWasSetInConstruct()
Expand All @@ -87,15 +87,15 @@ public function testShouldAllowSetUrlInConstruct()
{
$event = new CacheResolveEvent('default_path', 'default_filter', 'default_url');

$this->assertAttributeSame('default_url', 'url', $event);
$this->assertSame('default_url', $event->getUrl());
}

public function testShouldAllowSetUrlByMethod()
{
$event = new CacheResolveEvent('default_path', 'default_filter');
$event->setUrl('new_url');

$this->assertAttributeSame('new_url', 'url', $event);
$this->assertSame('new_url', $event->getUrl());
}

public function testShouldAllowGetUrlWhichWasSetInConstruct()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Liip\ImagineBundle\Tests\Exception\Imagine\Filter\PostProcessor;

use Liip\ImagineBundle\Exception\Imagine\Filter\PostProcessor\InvalidOptionException;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use PHPUnit\Framework\TestCase;

/**
* @covers \Liip\ImagineBundle\Exception\Imagine\Filter\PostProcessor\InvalidOptionException
Expand All @@ -38,7 +38,7 @@ public function testExceptionMessage(string $message, array $options, string $op
{
$exception = new InvalidOptionException($message, $options);

$this->assertContains(sprintf('(%s)', $message), $exception->getMessage());
$this->assertContains(sprintf('[%s]', $optionsText), $exception->getMessage());
$this->assertStringContainsString(sprintf('(%s)', $message), $exception->getMessage());
$this->assertStringContainsString(sprintf('[%s]', $optionsText), $exception->getMessage());
}
}
7 changes: 4 additions & 3 deletions Tests/Functional/Command/AbstractCommandTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class AbstractCommandTestCase extends AbstractSetupWebTestCase
{
protected function executeConsole(string $commandName, array $arguments = [], &$return = null): string
{
$application = new Application($this->createClient()->getKernel());
$kernel = static::createKernel();
$application = new Application($kernel);
$command = $application->find($commandName);

$arguments = array_replace(['command' => $command->getName()], $arguments);
Expand Down Expand Up @@ -81,7 +82,7 @@ protected function assertOutputContainsFailedImages($output, array $images, arra
{
foreach ($images as $i) {
foreach ($filters as $f) {
$this->assertContains(sprintf('%s[%s] (failed)', $i, $f), $output);
$this->assertStringContainsString(sprintf('%s[%s] (failed)', $i, $f), $output);
}
}
}
Expand All @@ -95,7 +96,7 @@ protected function assertOutputContainsImage($output, $image, $filter, $type): v
$filter,
$image,
]);
$this->assertContains($expected, $output);
$this->assertStringContainsString($expected, $output);
}

/**
Expand Down
20 changes: 10 additions & 10 deletions Tests/Functional/Command/RemoveCacheCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ protected function assertOutputContainsSkippedImages($output, array $images, arr
{
foreach ($images as $i) {
foreach ($filters as $f) {
$this->assertContains(sprintf('%s[%s] (skipped)', $i, $f), $output);
$this->assertStringContainsString(sprintf('%s[%s] (skipped)', $i, $f), $output);
}
}
}
Expand All @@ -155,7 +155,7 @@ protected function assertOutputContainsRemovedImages($output, array $images, arr
{
foreach ($images as $i) {
foreach ($filters as $f) {
$this->assertContains(sprintf('%s[%s] (removed)', $i, $f), $output);
$this->assertStringContainsString(sprintf('%s[%s] (removed)', $i, $f), $output);
}
}
}
Expand All @@ -166,11 +166,11 @@ protected function assertOutputContainsRemovedImages($output, array $images, arr
*/
protected function assertOutputContainsSummary(string $output, array $images, array $filters, int $failures = 0): void
{
$this->assertContains(sprintf('Completed %d removal', (count($images) * count($filters)) - $failures), $output);
$this->assertContains(sprintf('%d image', count($images)), $output);
$this->assertContains(sprintf('%d filter', count($filters)), $output);
$this->assertStringContainsString(sprintf('Completed %d removal', (count($images) * count($filters)) - $failures), $output);
$this->assertStringContainsString(sprintf('%d image', count($images)), $output);
$this->assertStringContainsString(sprintf('%d filter', count($filters)), $output);
if (0 !== $failures) {
$this->assertContains(sprintf('%d failure', $failures), $output);
$this->assertStringContainsString(sprintf('%d failure', $failures), $output);
}
}

Expand All @@ -180,11 +180,11 @@ protected function assertOutputContainsSummary(string $output, array $images, ar
*/
protected function assertOutputNotContainsSummary(string $output, array $images, array $filters, int $failures = 0): void
{
$this->assertNotContains(sprintf('Completed %d removal', (count($images) * count($filters)) - $failures), $output);
$this->assertNotContains(sprintf('%d image', count($images)), $output);
$this->assertNotContains(sprintf('%d filter', count($filters)), $output);
$this->assertStringNotContainsString(sprintf('Completed %d removal', (count($images) * count($filters)) - $failures), $output);
$this->assertStringNotContainsString(sprintf('%d image', count($images)), $output);
$this->assertStringNotContainsString(sprintf('%d filter', count($filters)), $output);
if (0 !== $failures) {
$this->assertNotContains(sprintf('%d failure', $failures), $output);
$this->assertStringNotContainsString(sprintf('%d failure', $failures), $output);
}
}

Expand Down
20 changes: 10 additions & 10 deletions Tests/Functional/Command/ResolveCacheCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ public function testScriptReadableOption(): void

$output = $this->executeResolveCacheCommand($images, $filters, ['--as-script' => false]);

$this->assertContains('liip/imagine-bundle', $output);
$this->assertStringContainsString('liip/imagine-bundle', $output);
$this->assertOutputContainsFailedImages($output, $images, $filters);
$this->assertOutputContainsSummary($output, $images, $filters, 2);

$output = $this->executeResolveCacheCommand($images, $filters, ['--as-script' => true]);

$this->assertImagesNotExist($images, $filters);
$this->assertNotContains('liip/imagine-bundle', $output);
$this->assertStringNotContainsString('liip/imagine-bundle', $output);
$this->assertOutputContainsFailedImages($output, $images, $filters);
$this->assertOutputNotContainsSummary($output, $images, $filters, 2);

Expand All @@ -192,11 +192,11 @@ public function testScriptReadableOption(): void
*/
protected function assertOutputContainsSummary(string $output, array $images, array $filters, int $failures = 0): void
{
$this->assertContains(sprintf('Completed %d resolution', (count($images) * count($filters)) - $failures), $output);
$this->assertContains(sprintf('%d image', count($images)), $output);
$this->assertContains(sprintf('%d filter', count($filters)), $output);
$this->assertStringContainsString(sprintf('Completed %d resolution', (count($images) * count($filters)) - $failures), $output);
$this->assertStringContainsString(sprintf('%d image', count($images)), $output);
$this->assertStringContainsString(sprintf('%d filter', count($filters)), $output);
if (0 !== $failures) {
$this->assertContains(sprintf('%d failure', $failures), $output);
$this->assertStringContainsString(sprintf('%d failure', $failures), $output);
}
}

Expand All @@ -206,11 +206,11 @@ protected function assertOutputContainsSummary(string $output, array $images, ar
*/
protected function assertOutputNotContainsSummary(string $output, array $images, array $filters, int $failures = 0): void
{
$this->assertNotContains(sprintf('Completed %d resolution', (count($images) * count($filters)) - $failures), $output);
$this->assertNotContains(sprintf('%d image', count($images)), $output);
$this->assertNotContains(sprintf('%d filter', count($filters)), $output);
$this->assertStringNotContainsString(sprintf('Completed %d resolution', (count($images) * count($filters)) - $failures), $output);
$this->assertStringNotContainsString(sprintf('%d image', count($images)), $output);
$this->assertStringNotContainsString(sprintf('%d filter', count($filters)), $output);
if (0 !== $failures) {
$this->assertNotContains(sprintf('%d failure', $failures), $output);
$this->assertStringNotContainsString(sprintf('%d failure', $failures), $output);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/Imagine/Cache/CacheManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ public function testShouldAllowToPassChangedDataFromPreResolveEventToPostResolve
$this->isInstanceOf(CacheResolveEvent::class),
ImagineEvents::PRE_RESOLVE
]))
->willReturnCallback($this->getDispatcherCallbackWithBC($dispatcher, function ($event, $name) {
->willReturnCallback($this->getDispatcherCallbackWithBC($dispatcher, function (CacheResolveEvent $event) {
$event->setPath('changed_path');
$event->setFilter('changed_filter');
}));
Expand Down
Loading

0 comments on commit 9c929ee

Please sign in to comment.