Skip to content

Commit

Permalink
✅ improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
garak committed Nov 17, 2024
1 parent 591259d commit a950942
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 57 deletions.
17 changes: 9 additions & 8 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,33 @@ 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:
REQUIRE_DEV: true
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'
Expand All @@ -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
Expand Down
6 changes: 1 addition & 5 deletions src/Form/SimpleBus/AbstractBusType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@

abstract class AbstractBusType extends AbstractType
{
public function __construct(private Bus $bus)
public function __construct(private readonly Bus $bus)
{
}

/**
* @param FormBuilderInterface<AbstractType> $builder
* @param array<string, mixed> $options
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
if (isset($options['data']) && \is_object($options['data'])) {
Expand Down
44 changes: 22 additions & 22 deletions tests/Form/Messenger/AbstractBusTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,56 @@ 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()]);
}

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);
}

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);
}

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);
Expand Down
44 changes: 22 additions & 22 deletions tests/Form/SimpleBus/AbstractBusTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,56 +13,56 @@ 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()]);
}

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);
}

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);
}

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);
Expand Down

0 comments on commit a950942

Please sign in to comment.