Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Tests] Migrate tests to static data providers #49288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,66 +49,63 @@ public static function requiredType()
yield [Types::DATETIMETZ_MUTABLE, new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', [], Guess::HIGH_CONFIDENCE)];
}

/**
* @dataProvider requiredProvider
*/
public function testRequiredGuesser($classMetadata, $expected)
{
$this->assertEquals($expected, $this->getGuesser($classMetadata)->guessRequired('TestEntity', 'field'));
}

public function requiredProvider()
public function testRequiredGuesserSimpleFieldNotNullable()
{
$return = [];

// Simple field, not nullable
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->fieldMappings['field'] = true;
$classMetadata->expects($this->once())->method('isNullable')->with('field')->willReturn(false);

$return[] = [$classMetadata, new ValueGuess(true, Guess::HIGH_CONFIDENCE)];
$this->assertEquals(new ValueGuess(true, Guess::HIGH_CONFIDENCE), $this->getGuesser($classMetadata)->guessRequired('TestEntity', 'field'));
}

// Simple field, nullable
public function testRequiredGuesserSimpleFieldNullable()
{
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->fieldMappings['field'] = true;
$classMetadata->expects($this->once())->method('isNullable')->with('field')->willReturn(true);

$return[] = [$classMetadata, new ValueGuess(false, Guess::MEDIUM_CONFIDENCE)];
$this->assertEquals(new ValueGuess(false, Guess::MEDIUM_CONFIDENCE), $this->getGuesser($classMetadata)->guessRequired('TestEntity', 'field'));
}

// One-to-one, nullable (by default)
public function testRequiredGuesserOneToOneNullable()
{
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->willReturn(true);

$mapping = ['joinColumns' => [[]]];
$classMetadata->expects($this->once())->method('getAssociationMapping')->with('field')->willReturn($mapping);

$return[] = [$classMetadata, new ValueGuess(false, Guess::HIGH_CONFIDENCE)];
$this->assertEquals(new ValueGuess(false, Guess::HIGH_CONFIDENCE), $this->getGuesser($classMetadata)->guessRequired('TestEntity', 'field'));
}

// One-to-one, nullable (explicit)
public function testRequiredGuesserOneToOneExplicitNullable()
{
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->willReturn(true);

$mapping = ['joinColumns' => [['nullable' => true]]];
$classMetadata->expects($this->once())->method('getAssociationMapping')->with('field')->willReturn($mapping);

$return[] = [$classMetadata, new ValueGuess(false, Guess::HIGH_CONFIDENCE)];
$this->assertEquals(new ValueGuess(false, Guess::HIGH_CONFIDENCE), $this->getGuesser($classMetadata)->guessRequired('TestEntity', 'field'));
}

// One-to-one, not nullable
public function testRequiredGuesserOneToOneNotNullable()
{
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->willReturn(true);

$mapping = ['joinColumns' => [['nullable' => false]]];
$classMetadata->expects($this->once())->method('getAssociationMapping')->with('field')->willReturn($mapping);

$return[] = [$classMetadata, new ValueGuess(true, Guess::HIGH_CONFIDENCE)];
$this->assertEquals(new ValueGuess(true, Guess::HIGH_CONFIDENCE), $this->getGuesser($classMetadata)->guessRequired('TestEntity', 'field'));
}

// One-to-many, no clue
public function testRequiredGuesserOneToMany()
{
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->willReturn(false);

$return[] = [$classMetadata, null];

return $return;
$this->assertNull($this->getGuesser($classMetadata)->guessRequired('TestEntity', 'field'));
}

private function getGuesser(ClassMetadata $classMetadata)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\WebProfilerBundle\Tests\Controller;

use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController;
Expand Down Expand Up @@ -366,6 +367,99 @@ public function provideCspVariants()
* @dataProvider defaultPanelProvider
*/
public function testDefaultPanel(string $expectedPanel, Profile $profile)
{
$this->assertDefaultPanel($expectedPanel, $profile);
}

public static function defaultPanelProvider(): \Generator
{
// Test default behavior
$profile = new Profile('xxxxxx');
$profile->addCollector($requestDataCollector = new RequestDataCollector());
yield [$requestDataCollector->getName(), $profile];

// Test exception
$profile = new Profile('xxxxxx');
$profile->addCollector($exceptionDataCollector = new ExceptionDataCollector());
$exceptionDataCollector->collect(new Request(), new Response(), new \DomainException());
yield [$exceptionDataCollector->getName(), $profile];
}

private function createController($profiler, $twig, $withCSP, array $templates = []): ProfilerController
{
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);

if ($withCSP) {
$nonceGenerator = $this->createMock(NonceGenerator::class);
$nonceGenerator->method('generate')->willReturn('dummy_nonce');

return new ProfilerController($urlGenerator, $profiler, $twig, $templates, new ContentSecurityPolicyHandler($nonceGenerator));
}

return new ProfilerController($urlGenerator, $profiler, $twig, $templates);
}

public function testDumpPanelExceptionPriority()
{
$exceptionDataCollector = new ExceptionDataCollector();
$exceptionDataCollector->collect(new Request(), new Response(), new \DomainException());

$dumpDataCollector = $this->createDumpDataCollector();

$profile = new Profile('xxxxxx');
$profile->setCollectors([$exceptionDataCollector, $dumpDataCollector]);

$this->assertDefaultPanel($exceptionDataCollector->getName(), $profile);
}

public function testDumpPanelWhenDefinedAfterwards()
{
$exceptionDataCollector = new ExceptionDataCollector();
$exceptionDataCollector->collect(new Request(), new Response(), new \DomainException());

$dumpDataCollector = $this->createDumpDataCollector();
$dumpDataCollector
->expects($this->atLeastOnce())
->method('getDumpsCount')
->willReturn(1)
;

$profile = new Profile('xxxxxx');
$profile->setCollectors([$dumpDataCollector, $exceptionDataCollector]);

$this->assertDefaultPanel($exceptionDataCollector->getName(), $profile);
}

public function testDumpPanel()
{
$dumpDataCollector = $this->createDumpDataCollector();
$dumpDataCollector
->expects($this->atLeastOnce())
->method('getDumpsCount')
->willReturn(1)
;

$profile = new Profile('xxxxxx');
$profile->addCollector($dumpDataCollector);

$this->assertDefaultPanel($dumpDataCollector->getName(), $profile);
}

/**
* @return MockObject<DumpDataCollector>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The right type here is MockObject&DumpDataCollector actually. PHPUnit does not make the MockObject interface generic.

*/
private function createDumpDataCollector(): MockObject
{
$dumpDataCollector = $this->createMock(DumpDataCollector::class);
$dumpDataCollector
->expects($this->atLeastOnce())
->method('getName')
->willReturn('dump');

return $dumpDataCollector;
}

private function assertDefaultPanel(string $expectedPanel, Profile $profile)
{
$profiler = $this->createMock(Profiler::class);
$profiler
Expand Down Expand Up @@ -415,56 +509,4 @@ public function testDefaultPanel(string $expectedPanel, Profile $profile)
}, $collectorsNames))
->panelAction(new Request(), $profile->getToken());
}

public function defaultPanelProvider(): \Generator
{
// Test default behavior
$profile = new Profile('xxxxxx');
$profile->addCollector($requestDataCollector = new RequestDataCollector());
yield [$requestDataCollector->getName(), $profile];

// Test exception
$profile = new Profile('xxxxxx');
$profile->addCollector($exceptionDataCollector = new ExceptionDataCollector());
$exceptionDataCollector->collect(new Request(), new Response(), new \DomainException());
yield [$exceptionDataCollector->getName(), $profile];

// Test exception priority
$dumpDataCollector = $this->createMock(DumpDataCollector::class);
$dumpDataCollector
->expects($this->atLeastOnce())
->method('getName')
->willReturn('dump');
$dumpDataCollector
->expects($this->atLeastOnce())
->method('getDumpsCount')
->willReturn(1);
$profile = new Profile('xxxxxx');
$profile->setCollectors([$exceptionDataCollector, $dumpDataCollector]);
yield [$exceptionDataCollector->getName(), $profile];

// Test exception priority when defined afterwards
$profile = new Profile('xxxxxx');
$profile->setCollectors([$dumpDataCollector, $exceptionDataCollector]);
yield [$exceptionDataCollector->getName(), $profile];

// Test dump
$profile = new Profile('xxxxxx');
$profile->addCollector($dumpDataCollector);
yield [$dumpDataCollector->getName(), $profile];
}

private function createController($profiler, $twig, $withCSP, array $templates = []): ProfilerController
{
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);

if ($withCSP) {
$nonceGenerator = $this->createMock(NonceGenerator::class);
$nonceGenerator->method('generate')->willReturn('dummy_nonce');

return new ProfilerController($urlGenerator, $profiler, $twig, $templates, new ContentSecurityPolicyHandler($nonceGenerator));
}

return new ProfilerController($urlGenerator, $profiler, $twig, $templates);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,27 @@ public function testDescribeApplication(Application $application, $expectedDescr

public static function getDescribeInputArgumentTestData()
{
return static::getDescriptionTestData(ObjectsProvider::getInputArguments());
return self::getDescriptionTestData(ObjectsProvider::getInputArguments());
}

public static function getDescribeInputOptionTestData()
{
return static::getDescriptionTestData(ObjectsProvider::getInputOptions());
return self::getDescriptionTestData(ObjectsProvider::getInputOptions());
}

public static function getDescribeInputDefinitionTestData()
{
return static::getDescriptionTestData(ObjectsProvider::getInputDefinitions());
return self::getDescriptionTestData(ObjectsProvider::getInputDefinitions());
}

public static function getDescribeCommandTestData()
{
return static::getDescriptionTestData(ObjectsProvider::getCommands());
return self::getDescriptionTestData(ObjectsProvider::getCommands());
}

public static function getDescribeApplicationTestData()
{
return static::getDescriptionTestData(ObjectsProvider::getApplications());
return self::getDescriptionTestData(ObjectsProvider::getApplications());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getDescriptionTestData is protected, but switching to self:: makes it ignore any override of the method. Is it expected ?

Copy link
Member Author

@alexandre-daubois alexandre-daubois Feb 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't. It works here because the method is currently not overridden. But it would definitely cause some problems if it's overridden one day. I addressed both your comments in #49328, thank you very much for letting me know! I'll be cautious next time with this 👍

}

abstract protected function getDescriptor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testHtml5ParserWithInvalidHeadedContent(string $content)

public function validHtml5Provider(): iterable
{
$html = static::getDoctype().'<html><body><h1><p>Foo</p></h1></body></html>';
$html = self::getDoctype().'<html><body><h1><p>Foo</p></h1></body></html>';
$BOM = \chr(0xEF).\chr(0xBB).\chr(0xBF);

yield 'BOM first' => [$BOM.$html];
Expand All @@ -65,7 +65,7 @@ public function validHtml5Provider(): iterable

public function invalidHtml5Provider(): iterable
{
$html = static::getDoctype().'<html><body><h1><p>Foo</p></h1></body></html>';
$html = self::getDoctype().'<html><body><h1><p>Foo</p></h1></body></html>';

yield 'Text' => ['hello world'.$html];
yield 'Text between comments' => ['<!--c--> test <!--cc-->'.$html];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,39 @@ class GetAttrNodeTest extends AbstractNodeTestCase
public static function getEvaluateData(): array
{
return [
['b', new GetAttrNode(new NameNode('foo'), new ConstantNode(0), static::getArrayNode(), GetAttrNode::ARRAY_CALL), ['foo' => ['b' => 'a', 'b']]],
['a', new GetAttrNode(new NameNode('foo'), new ConstantNode('b'), static::getArrayNode(), GetAttrNode::ARRAY_CALL), ['foo' => ['b' => 'a', 'b']]],
['b', new GetAttrNode(new NameNode('foo'), new ConstantNode(0), self::getArrayNode(), GetAttrNode::ARRAY_CALL), ['foo' => ['b' => 'a', 'b']]],
['a', new GetAttrNode(new NameNode('foo'), new ConstantNode('b'), self::getArrayNode(), GetAttrNode::ARRAY_CALL), ['foo' => ['b' => 'a', 'b']]],

['bar', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), static::getArrayNode(), GetAttrNode::PROPERTY_CALL), ['foo' => new Obj()]],
['bar', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), self::getArrayNode(), GetAttrNode::PROPERTY_CALL), ['foo' => new Obj()]],

['baz', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), static::getArrayNode(), GetAttrNode::METHOD_CALL), ['foo' => new Obj()]],
['a', new GetAttrNode(new NameNode('foo'), new NameNode('index'), static::getArrayNode(), GetAttrNode::ARRAY_CALL), ['foo' => ['b' => 'a', 'b'], 'index' => 'b']],
['baz', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), self::getArrayNode(), GetAttrNode::METHOD_CALL), ['foo' => new Obj()]],
['a', new GetAttrNode(new NameNode('foo'), new NameNode('index'), self::getArrayNode(), GetAttrNode::ARRAY_CALL), ['foo' => ['b' => 'a', 'b'], 'index' => 'b']],
];
}

public static function getCompileData(): array
{
return [
['$foo[0]', new GetAttrNode(new NameNode('foo'), new ConstantNode(0), static::getArrayNode(), GetAttrNode::ARRAY_CALL)],
['$foo["b"]', new GetAttrNode(new NameNode('foo'), new ConstantNode('b'), static::getArrayNode(), GetAttrNode::ARRAY_CALL)],
['$foo[0]', new GetAttrNode(new NameNode('foo'), new ConstantNode(0), self::getArrayNode(), GetAttrNode::ARRAY_CALL)],
['$foo["b"]', new GetAttrNode(new NameNode('foo'), new ConstantNode('b'), self::getArrayNode(), GetAttrNode::ARRAY_CALL)],

['$foo->foo', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), static::getArrayNode(), GetAttrNode::PROPERTY_CALL), ['foo' => new Obj()]],
['$foo->foo', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), self::getArrayNode(), GetAttrNode::PROPERTY_CALL), ['foo' => new Obj()]],

['$foo->foo(["b" => "a", 0 => "b"])', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), static::getArrayNode(), GetAttrNode::METHOD_CALL), ['foo' => new Obj()]],
['$foo[$index]', new GetAttrNode(new NameNode('foo'), new NameNode('index'), static::getArrayNode(), GetAttrNode::ARRAY_CALL)],
['$foo->foo(["b" => "a", 0 => "b"])', new GetAttrNode(new NameNode('foo'), new ConstantNode('foo'), self::getArrayNode(), GetAttrNode::METHOD_CALL), ['foo' => new Obj()]],
['$foo[$index]', new GetAttrNode(new NameNode('foo'), new NameNode('index'), self::getArrayNode(), GetAttrNode::ARRAY_CALL)],
];
}

public static function getDumpData(): array
{
return [
['foo[0]', new GetAttrNode(new NameNode('foo'), new ConstantNode(0), static::getArrayNode(), GetAttrNode::ARRAY_CALL)],
['foo["b"]', new GetAttrNode(new NameNode('foo'), new ConstantNode('b'), static::getArrayNode(), GetAttrNode::ARRAY_CALL)],
['foo[0]', new GetAttrNode(new NameNode('foo'), new ConstantNode(0), self::getArrayNode(), GetAttrNode::ARRAY_CALL)],
['foo["b"]', new GetAttrNode(new NameNode('foo'), new ConstantNode('b'), self::getArrayNode(), GetAttrNode::ARRAY_CALL)],

['foo.foo', new GetAttrNode(new NameNode('foo'), new NameNode('foo'), static::getArrayNode(), GetAttrNode::PROPERTY_CALL), ['foo' => new Obj()]],
['foo.foo', new GetAttrNode(new NameNode('foo'), new NameNode('foo'), self::getArrayNode(), GetAttrNode::PROPERTY_CALL), ['foo' => new Obj()]],

['foo.foo({"b": "a", 0: "b"})', new GetAttrNode(new NameNode('foo'), new NameNode('foo'), static::getArrayNode(), GetAttrNode::METHOD_CALL), ['foo' => new Obj()]],
['foo[index]', new GetAttrNode(new NameNode('foo'), new NameNode('index'), static::getArrayNode(), GetAttrNode::ARRAY_CALL)],
['foo.foo({"b": "a", 0: "b"})', new GetAttrNode(new NameNode('foo'), new NameNode('foo'), self::getArrayNode(), GetAttrNode::METHOD_CALL), ['foo' => new Obj()]],
['foo[index]', new GetAttrNode(new NameNode('foo'), new NameNode('index'), self::getArrayNode(), GetAttrNode::ARRAY_CALL)],
];
}

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Filesystem/Tests/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ private static function getPathTests(): \Generator

public function provideMakeAbsoluteTests(): \Generator
{
yield from static::getPathTests();
yield from self::getPathTests();

// collapse dots
yield ['css/./style.css', '/webmozart/symfony', '/webmozart/symfony/css/style.css'];
Expand Down Expand Up @@ -589,7 +589,7 @@ public function testMakeAbsoluteDoesNotFailIfDifferentRoot(string $basePath, str

public function provideMakeRelativeTests(): \Generator
{
foreach (static::getPathTests() as $set) {
foreach (self::getPathTests() as $set) {
yield [$set[2], $set[1], $set[0]];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ public function getAcceptData()
];

return [
[0, 0, static::toAbsolute($lessThan1)],
[0, 1, static::toAbsolute($lessThanOrEqualTo1)],
[0, 0, self::toAbsolute($lessThan1)],
[0, 1, self::toAbsolute($lessThanOrEqualTo1)],
[2, \PHP_INT_MAX, []],
[1, \PHP_INT_MAX, static::toAbsolute($graterThanOrEqualTo1)],
[1, 1, static::toAbsolute($equalTo1)],
[1, \PHP_INT_MAX, self::toAbsolute($graterThanOrEqualTo1)],
[1, 1, self::toAbsolute($equalTo1)],
];
}
}
Loading