Skip to content

Commit

Permalink
minor #25420 Refactoring tests (carusogabriel)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.7 branch.

Discussion
----------

Refactoring tests

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

I've refactored some tests, using:
- `assertCount` instead of `count` function;
- `assertArrayHasKey`, `assertArrayNotHasKey`, `assertObjectHasAttribute` and `assertObjectNotHasAttribute` instead of `isset` function;
- `assertContains` instead of `in_array` function;
- `assertSame` and `assertNotSame` instead os strict comparisons `===`;
- `assertNotFalse` instead of strict comparisons `!==` with `false` keyword;
- `assertGreaterThan`, `assertLessThan` and `assertLessThanOrEqual` for mathematical comparisons;

Commits
-------

567e0ab Refactoring tests.
  • Loading branch information
nicolas-grekas committed Dec 12, 2017
2 parents 9ac08e8 + 567e0ab commit e77545a
Show file tree
Hide file tree
Showing 62 changed files with 192 additions and 195 deletions.
Expand Up @@ -143,7 +143,7 @@ public function testAccess()
list($matcherId, $attributes, $channel) = $rule;
$requestMatcher = $container->getDefinition($matcherId);

$this->assertFalse(isset($matcherIds[$matcherId]));
$this->assertArrayNotHasKey($matcherId, $matcherIds);
$matcherIds[$matcherId] = true;

$i = count($matcherIds);
Expand Down
Expand Up @@ -84,9 +84,9 @@ public function testCsrfAliases()
$processor = new Processor();
$configuration = new MainConfiguration(array(), array());
$processedConfig = $processor->processConfiguration($configuration, array($config));
$this->assertTrue(isset($processedConfig['firewalls']['stub']['logout']['csrf_token_generator']));
$this->assertArrayHasKey('csrf_token_generator', $processedConfig['firewalls']['stub']['logout']);
$this->assertEquals('a_token_generator', $processedConfig['firewalls']['stub']['logout']['csrf_token_generator']);
$this->assertTrue(isset($processedConfig['firewalls']['stub']['logout']['csrf_token_id']));
$this->assertArrayHasKey('csrf_token_id', $processedConfig['firewalls']['stub']['logout']);
$this->assertEquals('a_token_id', $processedConfig['firewalls']['stub']['logout']['csrf_token_id']);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Config/Tests/ConfigCacheTest.php
Expand Up @@ -112,7 +112,7 @@ public function testWriteDumpsFile()
$cache->write('FOOBAR');

$this->assertFileExists($this->cacheFile, 'Cache file is created');
$this->assertSame('FOOBAR', file_get_contents($this->cacheFile));
$this->assertStringEqualsFile($this->cacheFile, 'FOOBAR');
$this->assertFileNotExists($this->metaFile, 'Meta file is not created');
}

Expand All @@ -128,7 +128,7 @@ public function testWriteDumpsMetaFileWithDebugEnabled()

$this->assertFileExists($this->cacheFile, 'Cache file is created');
$this->assertFileExists($this->metaFile, 'Meta file is created');
$this->assertSame(serialize($metadata), file_get_contents($this->metaFile));
$this->assertStringEqualsFile($this->metaFile, serialize($metadata));
}

private function makeCacheFresh()
Expand Down
Expand Up @@ -32,7 +32,7 @@ public function testAppendingSomeNode()
->append($child);

$this->assertCount(3, $this->getField($parent, 'children'));
$this->assertTrue(in_array($child, $this->getField($parent, 'children')));
$this->assertContains($child, $this->getField($parent, 'children'));
}

/**
Expand Down
Expand Up @@ -164,6 +164,6 @@ public function testResourcesWithDifferentPatternsAreDifferent()
$resourceA = new DirectoryResource($this->directory, '/.xml$/');
$resourceB = new DirectoryResource($this->directory, '/.yaml$/');

$this->assertEquals(2, count(array_unique(array($resourceA, $resourceB))));
$this->assertCount(2, array_unique(array($resourceA, $resourceB)));
}
}
Expand Up @@ -37,7 +37,7 @@ public function testXmlLang($css, array $elementsId)
$translator = new Translator();
$document = new \SimpleXMLElement(file_get_contents(__DIR__.'/Fixtures/lang.xml'));
$elements = $document->xpath($translator->cssToXPath($css));
$this->assertEquals(count($elementsId), count($elements));
$this->assertCount(count($elementsId), $elements);
foreach ($elements as $element) {
$this->assertTrue(in_array($element->attributes()->id, $elementsId));
}
Expand Down
Expand Up @@ -49,7 +49,7 @@ public function testDefinitions()

$builder->setDefinition('foobar', $foo = new Definition('FooBarClass'));
$this->assertEquals($foo, $builder->getDefinition('foobar'), '->getDefinition() returns a service definition if defined');
$this->assertTrue($builder->setDefinition('foobar', $foo = new Definition('FooBarClass')) === $foo, '->setDefinition() implements a fluid interface by returning the service reference');
$this->assertSame($builder->setDefinition('foobar', $foo = new Definition('FooBarClass')), $foo, '->setDefinition() implements a fluid interface by returning the service reference');

$builder->addDefinitions($defs = array('foobar' => new Definition('FooBarClass')));
$this->assertEquals(array_merge($definitions, $defs), $builder->getDefinitions(), '->addDefinitions() adds the service definitions');
Expand Down Expand Up @@ -163,7 +163,7 @@ public function testAliases()
$this->assertFalse($builder->hasAlias('foobar'), '->hasAlias() returns false if the alias does not exist');
$this->assertEquals('foo', (string) $builder->getAlias('bar'), '->getAlias() returns the aliased service');
$this->assertTrue($builder->has('bar'), '->setAlias() defines a new service');
$this->assertTrue($builder->get('bar') === $builder->get('foo'), '->setAlias() creates a service that is an alias to another one');
$this->assertSame($builder->get('bar'), $builder->get('foo'), '->setAlias() creates a service that is an alias to another one');

try {
$builder->setAlias('foobar', 'foobar');
Expand Down Expand Up @@ -208,8 +208,8 @@ public function testSetAliases()
$builder->setAliases(array('bar' => 'foo', 'foobar' => 'foo'));

$aliases = $builder->getAliases();
$this->assertTrue(isset($aliases['bar']));
$this->assertTrue(isset($aliases['foobar']));
$this->assertArrayHasKey('bar', $aliases);
$this->assertArrayHasKey('foobar', $aliases);
}

public function testAddAliases()
Expand All @@ -219,8 +219,8 @@ public function testAddAliases()
$builder->addAliases(array('foobar' => 'foo'));

$aliases = $builder->getAliases();
$this->assertTrue(isset($aliases['bar']));
$this->assertTrue(isset($aliases['foobar']));
$this->assertArrayHasKey('bar', $aliases);
$this->assertArrayHasKey('foobar', $aliases);
}

public function testSetReplacesAlias()
Expand Down Expand Up @@ -480,7 +480,7 @@ public function testMerge()
$this->assertEquals(array('foo', 'bar', 'baz'), array_keys($container->getDefinitions()), '->merge() merges definitions already defined ones');

$aliases = $container->getAliases();
$this->assertTrue(isset($aliases['alias_for_foo']));
$this->assertArrayHasKey('alias_for_foo', $aliases);
$this->assertEquals('foo', (string) $aliases['alias_for_foo']);

$container = new ContainerBuilder();
Expand Down Expand Up @@ -619,7 +619,7 @@ public function testExtension()
$container->setResourceTracking(false);

$container->registerExtension($extension = new \ProjectExtension());
$this->assertTrue($container->getExtension('project') === $extension, '->registerExtension() registers an extension');
$this->assertSame($container->getExtension('project'), $extension, '->registerExtension() registers an extension');

$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('LogicException');
$container->getExtension('no_registered');
Expand Down
Expand Up @@ -172,7 +172,7 @@ public function testSetAlsoSetsScopedService()
$c->set('foo', $foo = new \stdClass(), 'foo');

$scoped = $this->getField($c, 'scopedServices');
$this->assertTrue(isset($scoped['foo']['foo']), '->set() sets a scoped service');
$this->assertArrayHasKey('foo', $scoped['foo'], '->set() sets a scoped service');
$this->assertSame($foo, $scoped['foo']['foo'], '->set() sets a scoped service');
}

Expand Down Expand Up @@ -340,14 +340,14 @@ public function testEnterLeaveScopeWithChildScopes()
$container->set('a', $a, 'bar');

$scoped = $this->getField($container, 'scopedServices');
$this->assertTrue(isset($scoped['bar']['a']));
$this->assertArrayHasKey('a', $scoped['bar']);
$this->assertSame($a, $scoped['bar']['a']);
$this->assertTrue($container->has('a'));

$container->leaveScope('foo');

$scoped = $this->getField($container, 'scopedServices');
$this->assertFalse(isset($scoped['bar']));
$this->assertArrayNotHasKey('bar', $scoped);
$this->assertFalse($container->isScopeActive('foo'));
$this->assertFalse($container->has('a'));
}
Expand All @@ -370,14 +370,14 @@ public function testEnterScopeRecursivelyWithInactiveChildScopes()
$container->set('a', $a, 'foo');

$scoped = $this->getField($container, 'scopedServices');
$this->assertTrue(isset($scoped['foo']['a']));
$this->assertArrayHasKey('a', $scoped['foo']);
$this->assertSame($a, $scoped['foo']['a']);
$this->assertTrue($container->has('a'));

$container->enterScope('foo');

$scoped = $this->getField($container, 'scopedServices');
$this->assertFalse(isset($scoped['a']));
$this->assertArrayNotHasKey('a', $scoped);
$this->assertTrue($container->isScopeActive('foo'));
$this->assertFalse($container->isScopeActive('bar'));
$this->assertFalse($container->has('a'));
Expand Down Expand Up @@ -409,14 +409,14 @@ public function testEnterChildScopeRecursively()
$container->set('a', $a, 'bar');

$scoped = $this->getField($container, 'scopedServices');
$this->assertTrue(isset($scoped['bar']['a']));
$this->assertArrayHasKey('a', $scoped['bar']);
$this->assertSame($a, $scoped['bar']['a']);
$this->assertTrue($container->has('a'));

$container->enterScope('bar');

$scoped = $this->getField($container, 'scopedServices');
$this->assertFalse(isset($scoped['a']));
$this->assertArrayNotHasKey('a', $scoped);
$this->assertTrue($container->isScopeActive('foo'));
$this->assertTrue($container->isScopeActive('bar'));
$this->assertFalse($container->has('a'));
Expand Down
Expand Up @@ -94,7 +94,7 @@ public function testLoadWithExternalEntitiesDisabled()

libxml_disable_entity_loader($disableEntities);

$this->assertTrue(count($containerBuilder->getParameterBag()->all()) > 0, 'Parameters can be read from the config file.');
$this->assertGreaterThan(0, $containerBuilder->getParameterBag()->all(), 'Parameters can be read from the config file.');
}

public function testLoadParameters()
Expand Down Expand Up @@ -182,7 +182,7 @@ public function testLoadAnonymousServices()
$args = $services['foo']->getArguments();
$this->assertCount(1, $args, '->load() references anonymous services as "normal" ones');
$this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Reference', $args[0], '->load() converts anonymous services to references to "normal" services');
$this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones');
$this->assertArrayHasKey((string) $args[0], $services, '->load() makes a reference to the created ones');
$inner = $services[(string) $args[0]];
$this->assertEquals('BarClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
$this->assertFalse($inner->isPublic());
Expand All @@ -191,7 +191,7 @@ public function testLoadAnonymousServices()
$args = $inner->getArguments();
$this->assertCount(1, $args, '->load() references anonymous services as "normal" ones');
$this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Reference', $args[0], '->load() converts anonymous services to references to "normal" services');
$this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones');
$this->assertArrayHasKey((string) $args[0], $services, '->load() makes a reference to the created ones');
$inner = $services[(string) $args[0]];
$this->assertEquals('BazClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
$this->assertFalse($inner->isPublic());
Expand All @@ -200,7 +200,7 @@ public function testLoadAnonymousServices()
$properties = $services['foo']->getProperties();
$property = $properties['p'];
$this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Reference', $property, '->load() converts anonymous services to references to "normal" services');
$this->assertTrue(isset($services[(string) $property]), '->load() makes a reference to the created ones');
$this->assertArrayHasKey((string) $property, $services, '->load() makes a reference to the created ones');
$inner = $services[(string) $property];
$this->assertEquals('BuzClass', $inner->getClass(), '->load() uses the same configuration as for the anonymous ones');
$this->assertFalse($inner->isPublic());
Expand Down Expand Up @@ -249,7 +249,7 @@ public function testLoadServices()
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('services6.xml');
$services = $container->getDefinitions();
$this->assertTrue(isset($services['foo']), '->load() parses <service> elements');
$this->assertArrayHasKey('foo', $services, '->load() parses <service> elements');
$this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Definition', $services['foo'], '->load() converts <service> element to Definition instances');
$this->assertEquals('FooClass', $services['foo']->getClass(), '->load() parses the class attribute');
$this->assertEquals('container', $services['scope.container']->getScope());
Expand All @@ -267,10 +267,10 @@ public function testLoadServices()
$this->assertEquals(array('BazClass', 'getInstance'), $services['new_factory3']->getFactory(), '->load() parses the factory tag');

$aliases = $container->getAliases();
$this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses <service> elements');
$this->assertArrayHasKey('alias_for_foo', $aliases, '->load() parses <service> elements');
$this->assertEquals('foo', (string) $aliases['alias_for_foo'], '->load() parses aliases');
$this->assertTrue($aliases['alias_for_foo']->isPublic());
$this->assertTrue(isset($aliases['another_alias_for_foo']));
$this->assertArrayHasKey('another_alias_for_foo', $aliases);
$this->assertEquals('foo', (string) $aliases['another_alias_for_foo']);
$this->assertFalse($aliases['another_alias_for_foo']->isPublic());

Expand Down Expand Up @@ -366,8 +366,8 @@ public function testExtensions()
$services = $container->getDefinitions();
$parameters = $container->getParameterBag()->all();

$this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
$this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
$this->assertArrayHasKey('project.service.bar', $services, '->load() parses extension elements');
$this->assertArrayHasKey('project.parameter.bar', $parameters, '->load() parses extension elements');

$this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
$this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
Expand All @@ -382,8 +382,8 @@ public function testExtensions()
$services = $container->getDefinitions();
$parameters = $container->getParameterBag()->all();

$this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
$this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
$this->assertArrayHasKey('project.service.bar', $services, '->load() parses extension elements');
$this->assertArrayHasKey('project.parameter.bar', $parameters, '->load() parses extension elements');

$this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
$this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
Expand Down Expand Up @@ -504,8 +504,8 @@ public function testXmlNamespaces()
$loader->load('namespaces.xml');
$services = $container->getDefinitions();

$this->assertTrue(isset($services['foo']), '->load() parses <srv:service> elements');
$this->assertEquals(1, count($services['foo']->getTag('foo.tag')), '->load parses <srv:tag> elements');
$this->assertArrayHasKey('foo', $services, '->load() parses <srv:service> elements');
$this->assertCount(1, $services['foo']->getTag('foo.tag'), '->load parses <srv:tag> elements');
$this->assertEquals(array(array('setBar', array('foo'))), $services['foo']->getMethodCalls(), '->load() parses the <srv:call> tag');
}

Expand Down
Expand Up @@ -142,7 +142,7 @@ public function testLoadServices()
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('services6.yml');
$services = $container->getDefinitions();
$this->assertTrue(isset($services['foo']), '->load() parses service elements');
$this->assertArrayHasKey('foo', $services, '->load() parses service elements');
$this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Definition', $services['foo'], '->load() converts service element to Definition instances');
$this->assertEquals('FooClass', $services['foo']->getClass(), '->load() parses the class attribute');
$this->assertEquals('container', $services['scope.container']->getScope());
Expand All @@ -160,10 +160,10 @@ public function testLoadServices()
$this->assertEquals(array('BazClass', 'getInstance'), $services['new_factory3']->getFactory(), '->load() parses the factory tag');

$aliases = $container->getAliases();
$this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses aliases');
$this->assertArrayHasKey('alias_for_foo', $aliases, '->load() parses aliases');
$this->assertEquals('foo', (string) $aliases['alias_for_foo'], '->load() parses aliases');
$this->assertTrue($aliases['alias_for_foo']->isPublic());
$this->assertTrue(isset($aliases['another_alias_for_foo']));
$this->assertArrayHasKey('another_alias_for_foo', $aliases);
$this->assertEquals('foo', (string) $aliases['another_alias_for_foo']);
$this->assertFalse($aliases['another_alias_for_foo']->isPublic());

Expand Down Expand Up @@ -192,8 +192,8 @@ public function testExtensions()
$services = $container->getDefinitions();
$parameters = $container->getParameterBag()->all();

$this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements');
$this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements');
$this->assertArrayHasKey('project.service.bar', $services, '->load() parses extension elements');
$this->assertArrayHasKey('project.parameter.bar', $parameters, '->load() parses extension elements');

$this->assertEquals('BAR', $services['project.service.foo']->getClass(), '->load() parses extension elements');
$this->assertEquals('BAR', $parameters['project.parameter.foo'], '->load() parses extension elements');
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
Expand Up @@ -172,7 +172,7 @@ public function testAddXmlContentWithErrors()
EOF
, 'UTF-8');

$this->assertTrue(count(libxml_get_errors()) > 1);
$this->assertGreaterThan(1, libxml_get_errors());

libxml_clear_errors();
libxml_use_internal_errors($internalErrors);
Expand Down

0 comments on commit e77545a

Please sign in to comment.