From a95094297f15921c9327696c6cac9bd950948835 Mon Sep 17 00:00:00 2001 From: Massimiliano Arione Date: Sun, 17 Nov 2024 12:00:02 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20improve=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build.yaml | 17 ++++---- src/Form/SimpleBus/AbstractBusType.php | 6 +-- tests/Form/Messenger/AbstractBusTypeTest.php | 44 ++++++++++---------- tests/Form/SimpleBus/AbstractBusTypeTest.php | 44 ++++++++++---------- 4 files changed, 54 insertions(+), 57 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 4874f5d..e6ddd44 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -6,11 +6,11 @@ on: jobs: phpstan: - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 name: PHPStan steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: PHPStan uses: docker://oskarstark/phpstan-ga env: @@ -18,20 +18,21 @@ jobs: with: args: analyse cs-fixer: - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 name: PHP-CS-Fixer steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Fix CS uses: docker://oskarstark/php-cs-fixer-ga tests: - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 strategy: matrix: php: - '8.2' - '8.3' + - '8.4' include: - description: 'Lowest' php: '8.2' @@ -45,10 +46,10 @@ jobs: name: PHP ${{ matrix.php }} tests (${{ matrix.description }}) steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Cache - uses: actions/cache@v3 - with: + uses: actions/cache@v4 + with: path: ~/.composer/cache/files key: ${{ matrix.php }}-${{ matrix.symfony }}-${{ matrix.composer_option }} - name: Setup PHP diff --git a/src/Form/SimpleBus/AbstractBusType.php b/src/Form/SimpleBus/AbstractBusType.php index a5f9639..15c37de 100644 --- a/src/Form/SimpleBus/AbstractBusType.php +++ b/src/Form/SimpleBus/AbstractBusType.php @@ -11,14 +11,10 @@ abstract class AbstractBusType extends AbstractType { - public function __construct(private Bus $bus) + public function __construct(private readonly Bus $bus) { } - /** - * @param FormBuilderInterface $builder - * @param array $options - */ public function buildForm(FormBuilderInterface $builder, array $options): void { if (isset($options['data']) && \is_object($options['data'])) { diff --git a/tests/Form/Messenger/AbstractBusTypeTest.php b/tests/Form/Messenger/AbstractBusTypeTest.php index d7d3d52..431a3d6 100644 --- a/tests/Form/Messenger/AbstractBusTypeTest.php +++ b/tests/Form/Messenger/AbstractBusTypeTest.php @@ -14,10 +14,10 @@ final class AbstractBusTypeTest extends TestCase { public function testBuildForm(): void { - $bus = $this->getMockBuilder(MessageBusInterface::class)->disableOriginalConstructor()->getMock(); - $builder = $this->getMockBuilder(FormBuilder::class)->disableOriginalConstructor()->getMock(); + $bus = $this->createMock(MessageBusInterface::class); + $builder = $this->createMock(FormBuilder::class); - $builder->expects(self::once())->method('addEventListener'); + $builder->expects($this->once())->method('addEventListener'); $type = new BusFormTypeMessengerStub($bus); $type->buildForm($builder, ['data' => new \stdClass()]); @@ -25,14 +25,14 @@ public function testBuildForm(): void public function testHandleValidForm(): void { - $bus = $this->getMockBuilder(MessageBusInterface::class)->disableOriginalConstructor()->getMock(); - $event = $this->getMockBuilder(FormEvent::class)->disableOriginalConstructor()->getMock(); - $form = $this->getMockBuilder(FormInterface::class)->getMock(); + $bus = $this->createMock(MessageBusInterface::class); + $event = $this->createMock(FormEvent::class); + $form = $this->createMock(FormInterface::class); $fooCommand = new \stdClass(); - $event->expects(self::once())->method('getForm')->willReturn($form); - $form->expects(self::once())->method('isValid')->willReturn(true); - $bus->expects(self::once())->method('dispatch')->willReturn(new Envelope($fooCommand)); + $event->expects($this->once())->method('getForm')->willReturn($form); + $form->expects($this->once())->method('isValid')->willReturn(true); + $bus->expects($this->once())->method('dispatch')->willReturn(new Envelope($fooCommand)); $type = new BusFormTypeMessengerStub($bus); $type->handle($event, $fooCommand); @@ -40,14 +40,14 @@ public function testHandleValidForm(): void public function testHandleInvalidForm(): void { - $bus = $this->getMockBuilder(MessageBusInterface::class)->disableOriginalConstructor()->getMock(); - $event = $this->getMockBuilder(FormEvent::class)->disableOriginalConstructor()->getMock(); - $form = $this->getMockBuilder(FormInterface::class)->getMock(); + $bus = $this->createMock(MessageBusInterface::class); + $event = $this->createMock(FormEvent::class); + $form = $this->createMock(FormInterface::class); $fooCommand = new \stdClass(); - $event->expects(self::once())->method('getForm')->willReturn($form); - $form->expects(self::once())->method('isValid')->willReturn(false); - $bus->expects(self::never())->method('dispatch')->willReturn(new Envelope($fooCommand)); + $event->expects($this->once())->method('getForm')->willReturn($form); + $form->expects($this->once())->method('isValid')->willReturn(false); + $bus->expects($this->never())->method('dispatch')->willReturn(new Envelope($fooCommand)); $type = new BusFormTypeMessengerStub($bus); $type->handle($event, $fooCommand); @@ -55,15 +55,15 @@ public function testHandleInvalidForm(): void public function testHandleException(): void { - $bus = $this->getMockBuilder(MessageBusInterface::class)->disableOriginalConstructor()->getMock(); - $event = $this->getMockBuilder(FormEvent::class)->disableOriginalConstructor()->getMock(); - $form = $this->getMockBuilder(FormInterface::class)->getMock(); + $bus = $this->createMock(MessageBusInterface::class); + $event = $this->createMock(FormEvent::class); + $form = $this->createMock(FormInterface::class); $fooCommand = new \stdClass(); - $event->expects(self::exactly(2))->method('getForm')->willReturn($form); - $form->expects(self::once())->method('isValid')->willReturn(true); - $form->expects(self::once())->method('addError'); - $bus->expects(self::once())->method('dispatch')->will(self::throwException(new \DomainException())); + $event->expects($this->exactly(2))->method('getForm')->willReturn($form); + $form->expects($this->once())->method('isValid')->willReturn(true); + $form->expects($this->once())->method('addError'); + $bus->expects($this->once())->method('dispatch')->will($this->throwException(new \DomainException())); $type = new BusFormTypeMessengerStub($bus); $type->handle($event, $fooCommand); diff --git a/tests/Form/SimpleBus/AbstractBusTypeTest.php b/tests/Form/SimpleBus/AbstractBusTypeTest.php index ea6f386..eaa31b4 100644 --- a/tests/Form/SimpleBus/AbstractBusTypeTest.php +++ b/tests/Form/SimpleBus/AbstractBusTypeTest.php @@ -13,10 +13,10 @@ final class AbstractBusTypeTest extends TestCase { public function testBuildForm(): void { - $bus = $this->getMockBuilder(MessageBusSupportingMiddleware::class)->disableOriginalConstructor()->getMock(); - $builder = $this->getMockBuilder(FormBuilder::class)->disableOriginalConstructor()->getMock(); + $bus = $this->createMock(MessageBusSupportingMiddleware::class); + $builder = $this->createMock(FormBuilder::class); - $builder->expects(self::once())->method('addEventListener'); + $builder->expects($this->once())->method('addEventListener'); $type = new BusFormTypeStub($bus); $type->buildForm($builder, ['data' => new \stdClass()]); @@ -24,14 +24,14 @@ public function testBuildForm(): void public function testHandleValidForm(): void { - $bus = $this->getMockBuilder(MessageBusSupportingMiddleware::class)->disableOriginalConstructor()->getMock(); - $event = $this->getMockBuilder(FormEvent::class)->disableOriginalConstructor()->getMock(); - $form = $this->getMockBuilder(FormInterface::class)->getMock(); + $bus = $this->createMock(MessageBusSupportingMiddleware::class); + $event = $this->createMock(FormEvent::class); + $form = $this->createMock(FormInterface::class); $fooCommand = new \stdClass(); - $event->expects(self::once())->method('getForm')->willReturn($form); - $form->expects(self::once())->method('isValid')->willReturn(true); - $bus->expects(self::once())->method('handle'); + $event->expects($this->once())->method('getForm')->willReturn($form); + $form->expects($this->once())->method('isValid')->willReturn(true); + $bus->expects($this->once())->method('handle'); $type = new BusFormTypeStub($bus); $type->handle($event, $fooCommand); @@ -39,14 +39,14 @@ public function testHandleValidForm(): void public function testHandleInvalidForm(): void { - $bus = $this->getMockBuilder(MessageBusSupportingMiddleware::class)->disableOriginalConstructor()->getMock(); - $event = $this->getMockBuilder(FormEvent::class)->disableOriginalConstructor()->getMock(); - $form = $this->getMockBuilder(FormInterface::class)->getMock(); + $bus = $this->createMock(MessageBusSupportingMiddleware::class); + $event = $this->createMock(FormEvent::class); + $form = $this->createMock(FormInterface::class); $fooCommand = new \stdClass(); - $event->expects(self::once())->method('getForm')->willReturn($form); - $form->expects(self::once())->method('isValid')->willReturn(false); - $bus->expects(self::never())->method('handle'); + $event->expects($this->once())->method('getForm')->willReturn($form); + $form->expects($this->once())->method('isValid')->willReturn(false); + $bus->expects($this->never())->method('handle'); $type = new BusFormTypeStub($bus); $type->handle($event, $fooCommand); @@ -54,15 +54,15 @@ public function testHandleInvalidForm(): void public function testHandleException(): void { - $bus = $this->getMockBuilder(MessageBusSupportingMiddleware::class)->disableOriginalConstructor()->getMock(); - $event = $this->getMockBuilder(FormEvent::class)->disableOriginalConstructor()->getMock(); - $form = $this->getMockBuilder(FormInterface::class)->getMock(); + $bus = $this->createMock(MessageBusSupportingMiddleware::class); + $event = $this->createMock(FormEvent::class); + $form = $this->createMock(FormInterface::class); $fooCommand = new \stdClass(); - $event->expects(self::exactly(2))->method('getForm')->willReturn($form); - $form->expects(self::once())->method('isValid')->willReturn(true); - $form->expects(self::once())->method('addError'); - $bus->expects(self::once())->method('handle')->will(self::throwException(new \DomainException())); + $event->expects($this->exactly(2))->method('getForm')->willReturn($form); + $form->expects($this->once())->method('isValid')->willReturn(true); + $form->expects($this->once())->method('addError'); + $bus->expects($this->once())->method('handle')->will($this->throwException(new \DomainException())); $type = new BusFormTypeStub($bus); $type->handle($event, $fooCommand);