Repository: symfony/symfony
File: src/Symfony/Component/Messenger/Tests/EventListener/ResetServicesListenerTest.php (PHP)

Code before:
 */

namespace Symfony\Component\Messenger\Tests\EventListener;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetterInterface;
use Symfony\Component\Messenger\Event\WorkerRunningEvent;
use Symfony\Component\Messenger\Event\WorkerStoppedEvent;
use Symfony\Component\Messenger\EventListener\ResetServicesListener;
use Symfony\Component\Messenger\Worker;

class ResetServicesListenerTest extends TestCase
{
    public static function provideResetServices(): iterable
    {
        yield [true];
        yield [false];
    }

    /**
     * @dataProvider provideResetServices
     */
    public function testResetServices(bool $shouldReset)
    {
        /** @var ServicesResetterInterface&MockObject $servicesResetter */
        $servicesResetter = $this->createMock(ServicesResetterInterface::class);
        $servicesResetter->expects($shouldReset ? $this->once() : $this->never())->method('reset');

        $event = new WorkerRunningEvent($this->createMock(Worker::class), !$shouldReset);

        $resetListener = new ResetServicesListener($servicesResetter);
        $resetListener->resetServices($event);
    }

    public function testResetServicesAtStop()
    {
        /** @var ServicesResetterInterface&MockObject $servicesResetter */
        $servicesResetter = $this->createMock(ServicesResetterInterface::class);
        $servicesResetter->expects($this->once())->method('reset');

        $event = new WorkerStoppedEvent($this->createMock(Worker::class));

        $resetListener = new ResetServicesListener($servicesResetter);
        $resetListener->resetServicesAtStop($event);
    }
}


Review comment (suggestion):
this `@var` is not necessary. `$this->createMock` already has this as return type.

Code after:
 */

namespace Symfony\Component\Messenger\Tests\EventListener;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
use Symfony\Component\Messenger\Event\WorkerRunningEvent;
use Symfony\Component\Messenger\Event\WorkerStoppedEvent;
use Symfony\Component\Messenger\EventListener\ResetServicesListener;
use Symfony\Component\Messenger\Worker;
use Symfony\Contracts\Service\ResetInterface;

class ResetServicesListenerTest extends TestCase
{
    public static function provideResetServices(): iterable
    {
        yield [true];
        yield [false];
    }

    /**
     * @dataProvider provideResetServices
     */
    public function testResetServices(bool $shouldReset)
    {
        $resettableService = $this->createMock(ResetInterface::class);
        $resettableService->expects($shouldReset ? $this->once() : $this->never())->method('reset');
        $servicesResetter = new ServicesResetter(new \ArrayIterator(['foo' => $resettableService]), ['foo' => 'reset']);

        $event = new WorkerRunningEvent($this->createMock(Worker::class), !$shouldReset);

        $resetListener = new ResetServicesListener($servicesResetter);
        $resetListener->resetServices($event);
    }

    public function testResetServicesAtStop()
    {
        $resettableService = $this->createMock(ResetInterface::class);
        $resettableService->expects($this->once())->method('reset');
        $servicesResetter = new ServicesResetter(new \ArrayIterator(['foo' => $resettableService]), ['foo' => 'reset']);

        $event = new WorkerStoppedEvent($this->createMock(Worker::class));

        $resetListener = new ResetServicesListener($servicesResetter);
        $resetListener->resetServicesAtStop($event);
    }
}