8000 stop using the deprecated at() PHPUnit matcher by xabbuh · Pull Request #37808 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

stop using the deprecated at() PHPUnit matcher #37808

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 1 commit into from
Aug 13, 2020
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
stop using the deprecated at() PHPUnit matcher
  • Loading branch information
xabbuh committed Aug 12, 2020
commit 850389731c3c197886de3a839939d3cf78c8c6a4
12 changes: 5 additions & 7 deletions src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,12 @@ public function testVerbosityChanged()
{
$output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
$output
->expects($this->at(0))
->expects($this->exactly(2))
->method('getVerbosity')
->willReturn(OutputInterface::VERBOSITY_QUIET)
;
$output
->expects($this->at(1))
->method('getVerbosity')
->willReturn(OutputInterface::VERBOSITY_DEBUG)
->willReturnOnConsecutiveCalls(
OutputInterface::VERBOSITY_QUIET,
OutputInterface::VERBOSITY_DEBUG
)
;
$handler = new ConsoleHandler($output);
$this->assertFalse($handler->isHandling(['level' => Logger::NOTICE]),
Expand Down
22 changes: 13 additions & 9 deletions src/Symfony/Bridge/Twig/Tests/Extension/StopwatchExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,22 @@ protected function getStopwatch($events = [])
$events = \is_array($events) ? $events : [$events];
$stopwatch = $this->getMockBuilder('Symfony\Component\Stopwatch\Stopwatch')->getMock();

$i = -1;
$expectedCalls = 0;
$expectedStartCalls = [];
$expectedStopCalls = [];
foreach ($events as $eventName) {
$stopwatch->expects($this->at(++$i))
->method('start')
->with($this->equalTo($eventName), 'template')
;
$stopwatch->expects($this->at(++$i))
->method('stop')
->with($this->equalTo($eventName))
;
++$expectedCalls;
$expectedStartCalls[] = [$this->equalTo($eventName), 'template'];
$expectedStopCalls[] = [$this->equalTo($eventName)];
}

$startInvocationMocker = $stopwatch->expects($this->exactly($expectedCalls))
->method('start');
\call_user_func_array([$startInvocationMocker, 'withConsecutive'], $expectedStartCalls);
$stopInvocationMocker = $stopwatch->expects($this->exactly($expectedCalls))
->method('stop');
\call_user_func_array([$stopInvocationMocker, 'withConsecutive'], $expectedStopCalls);

return $stopwatch;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Bundle\FrameworkBundle\Controller\TemplateController;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
Expand Down Expand Up @@ -48,10 +49,8 @@ public function testLegacyTwig()
$twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
$twig->expects($this->once())->method('render')->willReturn('bar');

$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
$container->expects($this->at(0))->method('has')->willReturn(false);
$container->expects($this->at(1))->method('has')->willReturn(true);
$container->expects($this->at(2))->method('get')->willReturn($twig);
$container = new ContainerBuilder();
Copy link
Member

Choose a reason for hiding this comment

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

You can use Container instead of ContainerBuilder. Btw, good catch! We shouldn't mock the container. 😃

$container->set('twig', $twig);

$controller = new TemplateController();
$controller->setContainer($container);
Expand All @@ -67,9 +66,8 @@ public function testLegacyTemplating()
$templating = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface')->getMock();
$templating->expects($this->once())->method('render')->willReturn('bar');

$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
$container->expects($this->at(0))->method('has')->willReturn(true);
$container->expects($this->at(1))->method('get')->willReturn($templating);
$container = new ContainerBuilder();
$container->set('templating', $templating);

$controller = new TemplateController();
$controller->setContainer($container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Response;

class DelegatingEngineTest extends TestCase
Expand Down Expand Up @@ -108,14 +109,10 @@ private function getFrameworkEngineMock($template, $supports)

private function getContainerMock($services)
{
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
$container = new ContainerBuilder();
Copy link
Member

Choose a reason for hiding this comment

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

Same here.


$i = 0;
foreach ($services as $id => $service) {
$container->expects($this->at($i++))
->method('get')
->with($id)
->willReturn($service);
$container->set($id, $service);
}

return $container;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,18 @@ public function testResourceFilesOptionLoadsBeforeOtherAddedResources($debug, $e

$loader = $this->getMockBuilder('Symfony\Component\Translation\Loader\LoaderInterface')->getMock();

$loader->expects($this->at(0))
$loader->expects($this->exactly(2))
->method('load')
/* The "messages.some_locale.loader" is passed via the resource_file option and shall be loaded first */
->with('messages.some_locale.loader', 'some_locale', 'messages')
->willReturn($someCatalogue);

$loader->expects($this->at(1))
->method('load')
/* This resource is added by an addResource() call and shall be loaded after the resource_files */
->with('second_resource.some_locale.loader', 'some_locale', 'messages')
->willReturn($someCatalogue);
->withConsecutive(
/* The "messages.some_locale.loader" is passed via the resource_file option and shall be loaded first */
['messages.some_locale.loader', 'some_locale', 'messages'],
/* This resource is added by an addResource() call and shall be loaded after the resource_files */
['second_resource.some_locale.loader', 'some_locale', 'messages']
)
->willReturnOnConsecutiveCalls(
$someCatalogue,
$someCatalogue
);

$options = [
'resource_files' => ['some_locale' => ['messages.some_locale.loader']],
Expand Down Expand Up @@ -352,55 +353,33 @@ protected function getLoader()
{
$loader = $this->getMockBuilder('Symfony\Component\Translation\Loader\LoaderInterface')->getMock();
$loader
->expects($this->at(0))
->method('load')
->willReturn($this->getCatalogue('fr', [
'foo' => 'foo (FR)',
]))
;
$loader
->expects($this->at(1))
->method('load')
->willReturn($this->getCatalogue('en', [
'foo' => 'foo (EN)',
'bar' => 'bar (EN)',
'choice' => '{0} choice 0 (EN)|{1} choice 1 (EN)|]1,Inf] choice inf (EN)',
]))
;
$loader
->expects($this->at(2))
->method('load')
->willReturn($this->getCatalogue('es', [
'foobar' => 'foobar (ES)',
]))
;
$loader
->expects($this->at(3))
->method('load')
->willReturn($this->getCatalogue('pt-PT', [
'foobarfoo' => 'foobarfoo (PT-PT)',
]))
;
$loader
->expects($this->at(4))
->method('load')
->willReturn($this->getCatalogue('pt_BR', [
'other choice' => '{0} other choice 0 (PT-BR)|{1} other choice 1 (PT-BR)|]1,Inf] other choice inf (PT-BR)',
]))
;
$loader
->expects($this->at(5))
->method('load')
->willReturn($this->getCatalogue('fr.UTF-8', [
'foobarbaz' => 'foobarbaz (fr.UTF-8)',
]))
;
$loader
->expects($this->at(6))
->expects($this->exactly(7))
->method('load')
->willReturn($this->getCatalogue('sr@latin', [
'foobarbax' => 'foobarbax (sr@latin)',
]))
->willReturnOnConsecutiveCalls(
$this->getCatalogue('fr', [
'foo' => 'foo (FR)',
]),
$this->getCatalogue('en', [
'foo' => 'foo (EN)',
'bar' => 'bar (EN)',
'choice' => '{0} choice 0 (EN)|{1} choice 1 (EN)|]1,Inf] choice inf (EN)',
]),
$this->getCatalogue('es', [
'foobar' => 'foobar (ES)',
]),
$this->getCatalogue('pt-PT', [
'foobarfoo' => 'foobarfoo (PT-PT)',
]),
$this->getCatalogue('pt_BR', [
'other choice' => '{0} other choice 0 (PT-BR)|{1} other choice 1 (PT-BR)|]1,Inf] other choice inf (PT-BR)',
]),
$this->getCatalogue('fr.UTF-8', [
'foobarbaz' => 'foobarbaz (fr.UTF-8)',
]),
$this->getCatalogue('sr@latin', [
'foobarbax' => 'foobarbax (sr@latin)',
])
)
;

return $loader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,12 @@ public function testLogger()
$tdispatcher->addListener('foo', $listener1 = function () {});
$tdispatcher->addListener('foo', $listener2 = function () {});

$logger->expects($this->at(0))->method('debug')->with('Notified event "{event}" to listener "{listener}".', ['event' => 'foo', 'listener' => 'closure']);
$logger->expects($this->at(1))->method('debug')->with('Notified event "{event}" to listener "{listener}".', ['event' => 'foo', 'listener' => 'closure']);
$logger->expects($this->exactly(2))
->method('debug')
->withConsecutive(
['Notified event "{event}" to listener "{listener}".', ['event' => 'foo', 'listener' => 'closure']],
['Notified event "{event}" to listener "{listener}".', ['event' => 'foo', 'listener' => 'closure']]
);
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should just use BufferingLogger instead of mocking the logger.


$tdispatcher->dispatch('foo');
}
Expand All @@ -189,9 +193,13 @@ public function testLoggerWithStoppedEvent()
$tdispatcher->addListener('foo', $listener1 = function (Event $event) { $event->stopPropagation(); });
$tdispatcher->addListener('foo', $listener2 = function () {});

$logger->expects($this->at(0))->method('debug')->with('Notified event "{event}" to listener "{listener}".', ['event' => 'foo', 'listener' => 'closure']);
$logger->expects($this->at(1))->method('debug')->with('Listener "{listener}" stopped propagation of the event "{event}".', ['event' => 'foo', 'listener' => 'closure']);
$logger->expects($this->at(2))->method('debug')->with('Listener "{listener}" was not called for event "{event}".', ['event' => 'foo', 'listener' => 'closure']);
$logger->expects($this->exactly(3))
->method('debug')
->withConsecutive(
['Notified event "{event}" to listener "{listener}".', ['event' => 'foo', 'listener' => 'closure']],
['Listener "{listener}" stopped propagation of the event "{event}".', ['event' => 'foo', 'listener' => 'closure']],
['Listener "{listener}" was not called for event "{event}".', ['event' => 'foo', 'listener' => 'closure']]
);

$tdispatcher->dispatch('foo');
}
Expand Down
Loading
0