From 829f59da7f88bc8945761ae95091aa0352c1e7e6 Mon Sep 17 00:00:00 2001 From: Gabriel Caruso Date: Thu, 1 Feb 2018 03:55:42 -0200 Subject: [PATCH] Improve assertions --- .../Bridge/Doctrine/Tests/ManagerRegistryTest.php | 2 +- src/Symfony/Bridge/Monolog/Tests/LoggerTest.php | 4 ++-- .../DependencyInjection/FrameworkExtensionTest.php | 2 +- .../Cache/Tests/Adapter/NullAdapterTest.php | 2 +- .../Cache/Tests/Adapter/ProxyAdapterTest.php | 2 +- .../Component/Cache/Tests/Simple/NullCacheTest.php | 2 +- .../Tests/Resource/ReflectionClassResourceTest.php | 2 +- .../DataCollector/FormDataCollectorTest.php | 4 ++-- .../Storage/Handler/NativeSessionHandlerTest.php | 2 +- .../Component/HttpKernel/Tests/KernelTest.php | 4 ++-- .../Tests/Profiler/FileProfilerStorageTest.php | 12 ++++++------ .../Routing/Tests/RouteCollectionBuilderTest.php | 2 +- 12 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/Tests/ManagerRegistryTest.php b/src/Symfony/Bridge/Doctrine/Tests/ManagerRegistryTest.php index d1596a1968..490a48cfe6 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/ManagerRegistryTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/ManagerRegistryTest.php @@ -40,7 +40,7 @@ class ManagerRegistryTest extends TestCase $registry->resetManager(); $this->assertSame($foo, $container->get('foo')); - $this->assertFalse(isset($foo->bar)); + $this->assertObjectNotHasAttribute('bar', $foo); } } diff --git a/src/Symfony/Bridge/Monolog/Tests/LoggerTest.php b/src/Symfony/Bridge/Monolog/Tests/LoggerTest.php index a5fab0583c..fdd36336cb 100644 --- a/src/Symfony/Bridge/Monolog/Tests/LoggerTest.php +++ b/src/Symfony/Bridge/Monolog/Tests/LoggerTest.php @@ -28,7 +28,7 @@ class LoggerTest extends TestCase $logger = new Logger(__METHOD__, array($handler)); $this->assertTrue($logger->error('error message')); - $this->assertSame(1, count($logger->getLogs())); + $this->assertCount(1, $logger->getLogs()); } public function testGetLogsWithoutDebugProcessor() @@ -93,7 +93,7 @@ class LoggerTest extends TestCase $logger = new Logger(__METHOD__, array($handler), array($processor)); $this->assertTrue($logger->error('error message')); - $this->assertSame(1, count($logger->getLogs())); + $this->assertCount(1, $logger->getLogs()); } public function testCountErrorsWithDebugProcessor() diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index cabccb38c1..b40e735c98 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -1123,7 +1123,7 @@ abstract class FrameworkExtensionTest extends TestCase $this->assertFalse($poolDefinition->isAbstract(), sprintf('Service definition "%s" is not abstract.', $id)); $tag = $poolDefinition->getTag('cache.pool'); - $this->assertTrue(isset($tag[0]['default_lifetime']), 'The default lifetime is stored as an attribute of the "cache.pool" tag.'); + $this->assertArrayHasKey('default_lifetime', $tag[0], 'The default lifetime is stored as an attribute of the "cache.pool" tag.'); $this->assertSame($defaultLifetime, $tag[0]['default_lifetime'], 'The default lifetime is stored as an attribute of the "cache.pool" tag.'); $parentDefinition = $poolDefinition; diff --git a/src/Symfony/Component/Cache/Tests/Adapter/NullAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/NullAdapterTest.php index c28a455026..73e5cad552 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/NullAdapterTest.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/NullAdapterTest.php @@ -53,7 +53,7 @@ class NullAdapterTest extends TestCase $itemKey = $item->getKey(); $this->assertEquals($itemKey, $key, 'Keys must be preserved when fetching multiple items'); - $this->assertTrue(in_array($key, $keys), 'Cache key can not change.'); + $this->assertContains($key, $keys, 'Cache key can not change.'); $this->assertFalse($item->isHit()); // Remove $key for $keys diff --git a/src/Symfony/Component/Cache/Tests/Adapter/ProxyAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/ProxyAdapterTest.php index 5e6abd16c1..ff4b9d34bc 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/ProxyAdapterTest.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/ProxyAdapterTest.php @@ -43,7 +43,7 @@ class ProxyAdapterTest extends AdapterTestCase $proxyItem = $pool->getItem('foo'); - $this->assertFalse($proxyItem === $item); + $this->assertNotSame($item, $proxyItem); $pool->save($proxyItem->set('bar')); } } diff --git a/src/Symfony/Component/Cache/Tests/Simple/NullCacheTest.php b/src/Symfony/Component/Cache/Tests/Simple/NullCacheTest.php index 16dd7764d2..7b760fd3bd 100644 --- a/src/Symfony/Component/Cache/Tests/Simple/NullCacheTest.php +++ b/src/Symfony/Component/Cache/Tests/Simple/NullCacheTest.php @@ -47,7 +47,7 @@ class NullCacheTest extends TestCase $count = 0; foreach ($items as $key => $item) { - $this->assertTrue(in_array($key, $keys), 'Cache key can not change.'); + $this->assertContains($key, $keys, 'Cache key can not change.'); $this->assertSame($default, $item); // Remove $key for $keys diff --git a/src/Symfony/Component/Config/Tests/Resource/ReflectionClassResourceTest.php b/src/Symfony/Component/Config/Tests/Resource/ReflectionClassResourceTest.php index 4eca3d50e4..b14099a9eb 100644 --- a/src/Symfony/Component/Config/Tests/Resource/ReflectionClassResourceTest.php +++ b/src/Symfony/Component/Config/Tests/Resource/ReflectionClassResourceTest.php @@ -100,7 +100,7 @@ EOPHP; $signature = implode("\n", iterator_to_array($generateSignature(new \ReflectionClass($class)))); if ($changeExpected) { - $this->assertTrue($expectedSignature !== $signature); + $this->assertNotSame($expectedSignature, $signature); } else { $this->assertSame($expectedSignature, $signature); } diff --git a/src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataCollectorTest.php b/src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataCollectorTest.php index 7e591d414e..bd3f25bffb 100644 --- a/src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataCollectorTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataCollectorTest.php @@ -690,9 +690,9 @@ class FormDataCollectorTest extends TestCase $this->assertTrue($formData['has_children_error']); $this->assertTrue($child1Data['has_children_error']); - $this->assertFalse(isset($child11Data['has_children_error']), 'The leaf data does not contains "has_children_error" property.'); + $this->assertArrayNotHasKey('has_children_error', $child11Data, 'The leaf data does not contains "has_children_error" property.'); $this->assertFalse($child2Data['has_children_error']); - $this->assertFalse(isset($child21Data['has_children_error']), 'The leaf data does not contains "has_children_error" property.'); + $this->assertArrayNotHasKey('has_children_error', $child21Data, 'The leaf data does not contains "has_children_error" property.'); } public function testReset() diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeSessionHandlerTest.php index 1a68d575e2..4a9fb600d2 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeSessionHandlerTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeSessionHandlerTest.php @@ -32,7 +32,7 @@ class NativeSessionHandlerTest extends TestCase { $handler = new NativeSessionHandler(); - $this->assertTrue($handler instanceof \SessionHandler); + $this->assertInstanceOf('SessionHandler', $handler); $this->assertTrue($handler instanceof NativeSessionHandler); } } diff --git a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php index 755e1db2b7..a16ac37dee 100644 --- a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php @@ -849,14 +849,14 @@ EOF; $kernel = new CustomProjectDirKernel(); $kernel->boot(); - $this->assertSame($containerClass, get_class($kernel->getContainer())); + $this->assertInstanceOf($containerClass, $kernel->getContainer()); $this->assertFileExists($containerFile); unlink(__DIR__.'/Fixtures/cache/custom/FixturesCustomDebugProjectContainer.php.meta'); $kernel = new CustomProjectDirKernel(function ($container) { $container->register('foo', 'stdClass')->setPublic(true); }); $kernel->boot(); - $this->assertTrue(get_class($kernel->getContainer()) !== $containerClass); + $this->assertNotInstanceOf($containerClass, $kernel->getContainer()); $this->assertFileExists($containerFile); $this->assertFileExists(dirname($containerFile).'.legacy'); } diff --git a/src/Symfony/Component/HttpKernel/Tests/Profiler/FileProfilerStorageTest.php b/src/Symfony/Component/HttpKernel/Tests/Profiler/FileProfilerStorageTest.php index 99ff207567..a1db1d9c0e 100644 --- a/src/Symfony/Component/HttpKernel/Tests/Profiler/FileProfilerStorageTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/Profiler/FileProfilerStorageTest.php @@ -83,22 +83,22 @@ class FileProfilerStorageTest extends TestCase $profile = new Profile('simple_quote'); $profile->setUrl('http://foo.bar/\''); $this->storage->write($profile); - $this->assertTrue(false !== $this->storage->read('simple_quote'), '->write() accepts single quotes in URL'); + $this->assertNotFalse($this->storage->read('simple_quote'), '->write() accepts single quotes in URL'); $profile = new Profile('double_quote'); $profile->setUrl('http://foo.bar/"'); $this->storage->write($profile); - $this->assertTrue(false !== $this->storage->read('double_quote'), '->write() accepts double quotes in URL'); + $this->assertNotFalse($this->storage->read('double_quote'), '->write() accepts double quotes in URL'); $profile = new Profile('backslash'); $profile->setUrl('http://foo.bar/\\'); $this->storage->write($profile); - $this->assertTrue(false !== $this->storage->read('backslash'), '->write() accepts backslash in URL'); + $this->assertNotFalse($this->storage->read('backslash'), '->write() accepts backslash in URL'); $profile = new Profile('comma'); $profile->setUrl('http://foo.bar/,'); $this->storage->write($profile); - $this->assertTrue(false !== $this->storage->read('comma'), '->write() accepts comma in URL'); + $this->assertNotFalse($this->storage->read('comma'), '->write() accepts comma in URL'); } public function testStoreDuplicateToken() @@ -247,7 +247,7 @@ class FileProfilerStorageTest extends TestCase $profile->setMethod('GET'); $this->storage->write($profile); - $this->assertTrue(false !== $this->storage->read('token1')); + $this->assertNotFalse($this->storage->read('token1')); $this->assertCount(1, $this->storage->find('127.0.0.1', '', 10, 'GET')); $profile = new Profile('token2'); @@ -256,7 +256,7 @@ class FileProfilerStorageTest extends TestCase $profile->setMethod('GET'); $this->storage->write($profile); - $this->assertTrue(false !== $this->storage->read('token2')); + $this->assertNotFalse($this->storage->read('token2')); $this->assertCount(2, $this->storage->find('127.0.0.1', '', 10, 'GET')); $this->storage->purge(); diff --git a/src/Symfony/Component/Routing/Tests/RouteCollectionBuilderTest.php b/src/Symfony/Component/Routing/Tests/RouteCollectionBuilderTest.php index f6af600bd4..76a042d670 100644 --- a/src/Symfony/Component/Routing/Tests/RouteCollectionBuilderTest.php +++ b/src/Symfony/Component/Routing/Tests/RouteCollectionBuilderTest.php @@ -357,7 +357,7 @@ class RouteCollectionBuilderTest extends TestCase $routeCollectionBuilder->import('/directory/recurse/*', '/other/', 'glob'); $routes = $routeCollectionBuilder->build()->all(); - $this->assertEquals(2, count($routes)); + $this->assertCount(2, $routes); $this->assertEquals('/other/a', $routes['a']->getPath()); $this->assertEquals('/other/b', $routes['b']->getPath()); }