Navigation Menu

Skip to content

Commit

Permalink
minor #31738 Use willReturn() instead of will(returnValue()) (derrabus)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.4 branch.

Discussion
----------

Use willReturn() instead of will(returnValue())

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

In a recent PR, fabbot complained about the usage of will(returnValue()) in test cases that I haven't changed. In this PR, I've applied the PHP CS Fixer's `php_unit_mock_short_will_return` fixer on the whole codebase.

Commits
-------

4fb67df Use willReturn() instead of will(returnValue()).
  • Loading branch information
nicolas-grekas committed May 30, 2019
2 parents 7ae54b4 + 4fb67df commit 3cd3522
Show file tree
Hide file tree
Showing 198 changed files with 1,225 additions and 1,227 deletions.
2 changes: 0 additions & 2 deletions .php_cs.dist
Expand Up @@ -18,8 +18,6 @@ return PhpCsFixer\Config::create()
'native_function_invocation' => ['include' => ['@compiler_optimized'], 'scope' => 'namespaced'],
// Part of future @Symfony ruleset in PHP-CS-Fixer To be removed from the config file once upgrading
'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],
// Part of @Symfony:risky in PHP-CS-Fixer 2.15.0. Incompatible with PHPUnit 4 that is required for Symfony 3.4
'php_unit_mock_short_will_return' => false,
])
->setRiskyAllowed(true)
->setFinder(
Expand Down
Expand Up @@ -166,20 +166,20 @@ private function createCollector($queries)
->getMock();
$connection->expects($this->any())
->method('getDatabasePlatform')
->will($this->returnValue(new MySqlPlatform()));
->willReturn(new MySqlPlatform());

$registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')->getMock();
$registry
->expects($this->any())
->method('getConnectionNames')
->will($this->returnValue(['default' => 'doctrine.dbal.default_connection']));
->willReturn(['default' => 'doctrine.dbal.default_connection']);
$registry
->expects($this->any())
->method('getManagerNames')
->will($this->returnValue(['default' => 'doctrine.orm.default_entity_manager']));
->willReturn(['default' => 'doctrine.orm.default_entity_manager']);
$registry->expects($this->any())
->method('getConnection')
->will($this->returnValue($connection));
->willReturn($connection);

$logger = $this->getMockBuilder('Doctrine\DBAL\Logging\DebugStack')->getMock();
$logger->queries = $queries;
Expand Down
Expand Up @@ -44,9 +44,9 @@ protected function setUp()

$this->extension->expects($this->any())
->method('getObjectManagerElementName')
->will($this->returnCallback(function ($name) {
->willReturnCallback(function ($name) {
return 'doctrine.orm.'.$name;
}));
});
}

/**
Expand Down
Expand Up @@ -26,7 +26,7 @@ public function testFetch()

$doctrineCacheMock->expects($this->once())
->method('fetch')
->will($this->returnValue('bar'));
->willReturn('bar');

$result = $parserCache->fetch('foo');

Expand All @@ -41,7 +41,7 @@ public function testFetchUnexisting()
$doctrineCacheMock
->expects($this->once())
->method('fetch')
->will($this->returnValue(false));
->willReturn(false);

$this->assertNull($parserCache->fetch(''));
}
Expand Down
Expand Up @@ -34,47 +34,47 @@ public function requiredProvider()
// Simple field, not nullable
$classMetadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$classMetadata->fieldMappings['field'] = true;
$classMetadata->expects($this->once())->method('isNullable')->with('field')->will($this->returnValue(false));
$classMetadata->expects($this->once())->method('isNullable')->with('field')->willReturn(false);

$return[] = [$classMetadata, new ValueGuess(true, Guess::HIGH_CONFIDENCE)];

// Simple field, nullable
$classMetadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$classMetadata->fieldMappings['field'] = true;
$classMetadata->expects($this->once())->method('isNullable')->with('field')->will($this->returnValue(true));
$classMetadata->expects($this->once())->method('isNullable')->with('field')->willReturn(true);

$return[] = [$classMetadata, new ValueGuess(false, Guess::MEDIUM_CONFIDENCE)];

// One-to-one, nullable (by default)
$classMetadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->will($this->returnValue(true));
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->willReturn(true);

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

$return[] = [$classMetadata, new ValueGuess(false, Guess::HIGH_CONFIDENCE)];

// One-to-one, nullable (explicit)
$classMetadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->will($this->returnValue(true));
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->willReturn(true);

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

$return[] = [$classMetadata, new ValueGuess(false, Guess::HIGH_CONFIDENCE)];

// One-to-one, not nullable
$classMetadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->will($this->returnValue(true));
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->willReturn(true);

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

$return[] = [$classMetadata, new ValueGuess(true, Guess::HIGH_CONFIDENCE)];

// One-to-many, no clue
$classMetadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->will($this->returnValue(false));
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->willReturn(false);

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

Expand All @@ -84,10 +84,10 @@ public function requiredProvider()
private function getGuesser(ClassMetadata $classMetadata)
{
$em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')->getMock();
$em->expects($this->once())->method('getClassMetaData')->with('TestEntity')->will($this->returnValue($classMetadata));
$em->expects($this->once())->method('getClassMetaData')->with('TestEntity')->willReturn($classMetadata);

$registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')->getMock();
$registry->expects($this->once())->method('getManagers')->will($this->returnValue([$em]));
$registry->expects($this->once())->method('getManagers')->willReturn([$em]);

return new DoctrineOrmTypeGuesser($registry);
}
Expand Down
Expand Up @@ -38,11 +38,11 @@ protected function getExtensions()

$manager->expects($this->any())
->method('getManager')
->will($this->returnValue($this->em));
->willReturn($this->em);

$manager->expects($this->any())
->method('getManagerForClass')
->will($this->returnValue($this->em));
->willReturn($this->em);

return [
new CoreExtension(),
Expand Down
Expand Up @@ -1087,7 +1087,7 @@ public function testGetManagerForClassIfNoEm()
$this->emRegistry->expects($this->once())
->method('getManagerForClass')
->with(self::SINGLE_IDENT_CLASS)
->will($this->returnValue($this->em));
->willReturn($this->em);

$this->factory->createNamed('name', static::TESTED_TYPE, null, [
'class' => self::SINGLE_IDENT_CLASS,
Expand Down Expand Up @@ -1237,7 +1237,7 @@ protected function createRegistryMock($name, $em)
$registry->expects($this->any())
->method('getManager')
->with($this->equalTo($name))
->will($this->returnValue($em));
->willReturn($em);

return $registry;
}
Expand Down
Expand Up @@ -188,7 +188,7 @@ private function getManager($em, $name = null)
$manager->expects($this->any())
->method('getManager')
->with($this->equalTo($name))
->will($this->returnValue($em));
->willReturn($em);

return $manager;
}
Expand Down
Expand Up @@ -82,7 +82,7 @@ protected function createRegistryMock(ObjectManager $em = null)
$registry->expects($this->any())
->method('getManager')
->with($this->equalTo(self::EM_NAME))
->will($this->returnValue($em));
->willReturn($em);

return $registry;
}
Expand All @@ -104,14 +104,14 @@ protected function createEntityManagerMock($repositoryMock)
;
$em->expects($this->any())
->method('getRepository')
->will($this->returnValue($repositoryMock))
->willReturn($repositoryMock)
;

$classMetadata = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\ClassMetadata')->getMock();
$classMetadata
->expects($this->any())
->method('hasField')
->will($this->returnValue(true))
->willReturn(true)
;
$reflParser = $this->getMockBuilder('Doctrine\Common\Reflection\StaticReflectionParser')
->disableOriginalConstructor()
Expand All @@ -125,12 +125,12 @@ protected function createEntityManagerMock($repositoryMock)
$refl
->expects($this->any())
->method('getValue')
->will($this->returnValue(true))
->willReturn(true)
;
$classMetadata->reflFields = ['name' => $refl];
$em->expects($this->any())
->method('getClassMetadata')
->will($this->returnValue($classMetadata))
->willReturn($classMetadata)
;

return $em;
Expand Down Expand Up @@ -366,7 +366,7 @@ public function testValidateUniquenessUsingCustomRepositoryMethod()
$repository = $this->createRepositoryMock();
$repository->expects($this->once())
->method('findByCustom')
->will($this->returnValue([]))
->willReturn([])
;
$this->em = $this->createEntityManagerMock($repository);
$this->registry = $this->createRegistryMock($this->em);
Expand Down Expand Up @@ -394,15 +394,15 @@ public function testValidateUniquenessWithUnrewoundArray()
$repository = $this->createRepositoryMock();
$repository->expects($this->once())
->method('findByCustom')
->will(
$this->returnCallback(function () use ($entity) {
->willReturnCallback(
function () use ($entity) {
$returnValue = [
$entity,
];
next($returnValue);

return $returnValue;
})
}
)
;
$this->em = $this->createEntityManagerMock($repository);
Expand Down Expand Up @@ -430,7 +430,7 @@ public function testValidateResultTypes($entity1, $result)
$repository = $this->createRepositoryMock();
$repository->expects($this->once())
->method('findByCustom')
->will($this->returnValue($result))
->willReturn($result)
;
$this->em = $this->createEntityManagerMock($repository);
$this->registry = $this->createRegistryMock($this->em);
Expand Down Expand Up @@ -564,7 +564,7 @@ public function testValidateUniquenessWithArrayValue()

$repository->expects($this->once())
->method('findByCustom')
->will($this->returnValue([$entity1]))
->willReturn([$entity1])
;

$this->em->persist($entity1);
Expand Down Expand Up @@ -635,7 +635,7 @@ public function testValidateUniquenessOnNullResult()
$repository = $this->createRepositoryMock();
$repository
->method('find')
->will($this->returnValue(null))
->willReturn(null)
;

$this->em = $this->createEntityManagerMock($repository);
Expand Down
Expand Up @@ -50,7 +50,7 @@ public function testVerbosityMapping($verbosity, $level, $isHandling, array $map
$output
->expects($this->atLeastOnce())
->method('getVerbosity')
->will($this->returnValue($verbosity))
->willReturn($verbosity)
;
$handler = new ConsoleHandler($output, true, $map);
$this->assertSame($isHandling, $handler->isHandling(['level' => $level]),
Expand Down Expand Up @@ -114,12 +114,12 @@ public function testVerbosityChanged()
$output
->expects($this->at(0))
->method('getVerbosity')
->will($this->returnValue(OutputInterface::VERBOSITY_QUIET))
->willReturn(OutputInterface::VERBOSITY_QUIET)
;
$output
->expects($this->at(1))
->method('getVerbosity')
->will($this->returnValue(OutputInterface::VERBOSITY_DEBUG))
->willReturn(OutputInterface::VERBOSITY_DEBUG)
;
$handler = new ConsoleHandler($output);
$this->assertFalse($handler->isHandling(['level' => Logger::NOTICE]),
Expand All @@ -144,7 +144,7 @@ public function testWritingAndFormatting()
$output
->expects($this->any())
->method('getVerbosity')
->will($this->returnValue(OutputInterface::VERBOSITY_DEBUG))
->willReturn(OutputInterface::VERBOSITY_DEBUG)
;
$output
->expects($this->once())
Expand Down
Expand Up @@ -95,10 +95,10 @@ private function createRequestEvent($additionalServerParameters = [])
->getMock();
$event->expects($this->any())
->method('isMasterRequest')
->will($this->returnValue(true));
->willReturn(true);
$event->expects($this->any())
->method('getRequest')
->will($this->returnValue($request));
->willReturn($request);

return [$event, $server];
}
Expand Down
Expand Up @@ -62,15 +62,15 @@ public function testUnknownFragmentRenderer()
protected function getFragmentHandler($return)
{
$strategy = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface')->getMock();
$strategy->expects($this->once())->method('getName')->will($this->returnValue('inline'));
$strategy->expects($this->once())->method('getName')->willReturn('inline');
$strategy->expects($this->once())->method('render')->will($return);

$context = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\RequestStack')
->disableOriginalConstructor()
->getMock()
;

$context->expects($this->any())->method('getCurrentRequest')->will($this->returnValue(Request::create('/')));
$context->expects($this->any())->method('getCurrentRequest')->willReturn(Request::create('/'));

return new FragmentHandler($context, [$strategy], false);
}
Expand All @@ -82,9 +82,9 @@ protected function renderTemplate(FragmentHandler $renderer, $template = '{{ ren
$twig->addExtension(new HttpKernelExtension());

$loader = $this->getMockBuilder('Twig\RuntimeLoader\RuntimeLoaderInterface')->getMock();
$loader->expects($this->any())->method('load')->will($this->returnValueMap([
$loader->expects($this->any())->method('load')->willReturnMap([
['Symfony\Bridge\Twig\Extension\HttpKernelRuntime', new HttpKernelRuntime($renderer)],
]));
]);
$twig->addRuntimeLoader($loader);

return $twig->render('index');
Expand Down
Expand Up @@ -34,7 +34,7 @@ public function testFindAllTemplates()
$kernel
->expects($this->once())
->method('getBundles')
->will($this->returnValue(['BaseBundle' => new BaseBundle()]))
->willReturn(['BaseBundle' => new BaseBundle()])
;

$parser = new TemplateFilenameParser();
Expand Down
Expand Up @@ -68,13 +68,13 @@ public function testWarmUp()
$this->templateFinder
->expects($this->once())
->method('findAllTemplates')
->will($this->returnValue([$template]));
->willReturn([$template]);

$this->fileLocator
->expects($this->once())
->method('locate')
->with($template->getPath())
->will($this->returnValue(\dirname($this->tmpDir).'/path/to/template.html.twig'));
->willReturn(\dirname($this->tmpDir).'/path/to/template.html.twig');

$warmer = new TemplatePathsCacheWarmer($this->templateFinder, $this->templateLocator);
$warmer->warmUp($this->tmpDir);
Expand All @@ -87,7 +87,7 @@ public function testWarmUpEmpty()
$this->templateFinder
->expects($this->once())
->method('findAllTemplates')
->will($this->returnValue([]));
->willReturn([]);

$this->fileLocator
->expects($this->never())
Expand Down

0 comments on commit 3cd3522

Please sign in to comment.