Skip to content

Commit

Permalink
minor test improvements (#86)
Browse files Browse the repository at this point in the history
* wrong (empty) directive

* static data provider

* fix test warn

* bumped php & phpunit

* migrated phpunit conf

* ignore phpunit cache dir

* migrate to attributes

* refactor around deprecated getMockForAbstractClass

* cs
  • Loading branch information
basz authored Feb 15, 2025
1 parent daccc3b commit dd1683f
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 169 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
nbproject
composer.lock
.phpunit.result.cache
.phpunit.cache
.phpcs-cache
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^7.4 || ^8.0",
"php": "^8.2",
"ramsey/uuid": "4.1.2 || ^4.3",
"beberlei/assert": "^2.7.1 || ^3.0"
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.2",
"phpunit/phpunit": "^9.5.5",
"phpunit/phpunit": "^11.0",
"prooph/bookdown-template": "^0.2.3",
"prooph/php-cs-fixer-config": "^0.5"
},
Expand Down
19 changes: 9 additions & 10 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php" convertDeprecationsToExceptions="true" colors="true">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>

<testsuite name="Prooph Common Test Suite">
<directory>./tests/</directory>
</testsuite>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" cacheDirectory=".phpunit.cache">
<testsuite name="Prooph Common Test Suite">
<directory>./tests/</directory>
</testsuite>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
45 changes: 12 additions & 33 deletions tests/Event/DefaultActionEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ProophTest\Common\Event;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Prooph\Common\Event\DefaultActionEvent;

Expand All @@ -26,9 +27,7 @@ private function getTestEvent()
return new DefaultActionEvent('test-event', 'target', ['param1' => 'foo']);
}

/**
* @test
*/
#[Test]
public function it_can_be_initialized_with_a_name_a_target_and_params(): void
{
$event = $this->getTestEvent();
Expand All @@ -38,9 +37,7 @@ public function it_can_be_initialized_with_a_name_a_target_and_params(): void
$this->assertEquals(['param1' => 'foo'], $event->getParams());
}

/**
* @test
*/
#[Test]
public function it_can_initialized_without_a_target_and_params(): void
{
$event = new DefaultActionEvent('test-event');
Expand All @@ -49,9 +46,7 @@ public function it_can_initialized_without_a_target_and_params(): void
$this->assertEquals([], $event->getParams());
}

/**
* @test
*/
#[Test]
public function it_returns_param_if_set(): void
{
$event = $this->getTestEvent();
Expand All @@ -60,25 +55,19 @@ public function it_returns_param_if_set(): void
$this->assertEquals('bar', $event->getParam('param1'));
}

/**
* @test
*/
#[Test]
public function it_returns_null_if_param_is_not_set_and_no_other_default_is_given(): void
{
$this->assertNull($this->getTestEvent()->getParam('unknown'));
}

/**
* @test
*/
#[Test]
public function it_returns_default_if_param_is_not_set(): void
{
$this->assertEquals('default', $this->getTestEvent()->getParam('unknown', 'default'));
}

/**
* @test
*/
#[Test]
public function it_changes_name_when_new_one_is_set(): void
{
$event = $this->getTestEvent();
Expand All @@ -88,9 +77,7 @@ public function it_changes_name_when_new_one_is_set(): void
$this->assertEquals('new name', $event->getName());
}

/**
* @test
*/
#[Test]
public function it_overrides_params_array_if_new_one_is_set(): void
{
$event = $this->getTestEvent();
Expand All @@ -100,9 +87,7 @@ public function it_overrides_params_array_if_new_one_is_set(): void
$this->assertEquals(['param_new' => 'bar'], $event->getParams());
}

/**
* @test
*/
#[Test]
public function it_allows_object_implementing_array_access_as_params(): void
{
$arrayLikeObject = new \ArrayObject(['object_param' => 'baz']);
Expand All @@ -114,9 +99,7 @@ public function it_allows_object_implementing_array_access_as_params(): void
$this->assertSame($arrayLikeObject, $event->getParams());
}

/**
* @test
*/
#[Test]
public function it_does_not_allow_params_object_that_is_not_of_type_array_access(): void
{
$this->expectException(\InvalidArgumentException::class);
Expand All @@ -128,9 +111,7 @@ public function it_does_not_allow_params_object_that_is_not_of_type_array_access
$this->getTestEvent()->setParams($stdObj);
}

/**
* @test
*/
#[Test]
public function it_changes_target_if_new_is_set(): void
{
$event = $this->getTestEvent();
Expand All @@ -142,9 +123,7 @@ public function it_changes_target_if_new_is_set(): void
$this->assertSame($target, $event->getTarget());
}

/**
* @test
*/
#[Test]
public function it_indicates_that_propagation_should_be_stopped(): void
{
$event = $this->getTestEvent();
Expand Down
63 changes: 22 additions & 41 deletions tests/Event/ProophActionEventEmitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ProophTest\Common\Event;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Prooph\Common\Event\ActionEvent;
use Prooph\Common\Event\ListenerHandler;
Expand All @@ -29,9 +30,7 @@ protected function setUp(): void
$this->proophActionEventEmitter = new ProophActionEventEmitter();
}

/**
* @test
*/
#[Test]
public function it_attaches_action_event_listeners_and_dispatch_event_to_them(): void
{
$lastEvent = null;
Expand All @@ -52,9 +51,7 @@ public function it_attaches_action_event_listeners_and_dispatch_event_to_them():
$this->assertSame($lastEvent, $listener1->lastEvent);
}

/**
* @test
*/
#[Test]
public function it_detaches_a_listener(): void
{
$lastEvent = null;
Expand All @@ -78,9 +75,7 @@ public function it_detaches_a_listener(): void
$this->assertSame($actionEvent, $lastEvent);
}

/**
* @test
*/
#[Test]
public function it_triggers_listeners_until_callback_returns_true(): void
{
$lastEvent = null;
Expand All @@ -103,9 +98,7 @@ public function it_triggers_listeners_until_callback_returns_true(): void
$this->assertSame($actionEvent, $listener1->lastEvent);
}

/**
* @test
*/
#[Test]
public function it_stops_dispatching_when_event_propagation_is_stopped(): void
{
$lastEvent = null;
Expand All @@ -131,9 +124,7 @@ public function it_stops_dispatching_when_event_propagation_is_stopped(): void
$this->assertSame($actionEvent, $listener1->lastEvent);
}

/**
* @test
*/
#[Test]
public function it_stops_dispatching_when_event_propagation_is_stopped_2(): void
{
$lastEvent = null;
Expand All @@ -160,9 +151,7 @@ public function it_stops_dispatching_when_event_propagation_is_stopped_2(): void
$this->assertSame($actionEvent, $listener1->lastEvent);
}

/**
* @test
*/
#[Test]
public function it_triggers_listeners_with_high_priority_first(): void
{
$lastEvent = null;
Expand All @@ -188,9 +177,7 @@ public function it_triggers_listeners_with_high_priority_first(): void
$this->assertNull($listener1->lastEvent);
}

/**
* @test
*/
#[Test]
public function it_attaches_a_listener_aggregate(): void
{
$listener1 = new ActionEventListenerMock();
Expand All @@ -207,9 +194,7 @@ public function it_attaches_a_listener_aggregate(): void
$this->assertNull($listener1->lastEvent);
}

/**
* @test
*/
#[Test]
public function it_detaches_listener_aggregate(): void
{
$listener1 = new ActionEventListenerMock();
Expand All @@ -227,48 +212,44 @@ public function it_detaches_listener_aggregate(): void
$this->assertSame($actionEvent, $listener1->lastEvent);
}

/**
* @test
*/
#[Test]
public function it_uses_default_event_name_if_none_given(): void
{
$event = $this->proophActionEventEmitter->getNewActionEvent();
$this->assertEquals('action_event', $event->getName());
}

/**
* @test
*/
#[Test]
public function it_returns_false_when_unattached_listener_handler_gets_detached(): void
{
$listener = $this->getMockForAbstractClass(ListenerHandler::class);
$listener = $this->getMockBuilder(ListenerHandler::class)
->onlyMethods(['getActionEventListener'])
->getMock();

$this->assertFalse($this->proophActionEventEmitter->detachListener($listener));
}

/**
* @test
*/
public function it_dispatches_until_whith_no_listeners_attached(): void
#[Test]
public function it_dispatches_until_with_no_listeners_attached(): void
{
$this->expectNotToPerformAssertions();

$actionEventMock = $this->createMock(ActionEvent::class);

$this->proophActionEventEmitter->dispatchUntil($actionEventMock, fn () => true);
}

/**
* @test
*/
#[Test]
public function it_attaches_to_known_event_names(): void
{
$this->expectNotToPerformAssertions();

$proophActionEventEmitter = new ProophActionEventEmitter(['foo']);
$proophActionEventEmitter->attachListener('foo', function (): void {
});
}

/**
* @test
*/
#[Test]
public function it_does_not_attach_to_unknown_event_names(): void
{
$this->expectException(\InvalidArgumentException::class);
Expand Down
Loading

0 comments on commit dd1683f

Please sign in to comment.