Skip to content

Commit

Permalink
Add missing test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-patterson-webdev committed May 8, 2023
1 parent b3842e5 commit e5b33fb
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 62 deletions.
15 changes: 0 additions & 15 deletions src/Resolver/EventNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,14 @@

namespace Arp\EventDispatcher\Resolver;

use Arp\EventDispatcher\Resolver\Exception\EventNameResolverException;

final class EventNameResolver implements EventNameResolverInterface
{
/**
* @throws EventNameResolverException If the provided $event is not a string or object.
*/
public function resolveEventName(string|object $event): string
{
if (is_string($event)) {
return $event;
}

if (!is_object($event)) {
throw new EventNameResolverException(
sprintf(
'The \'event\' argument must be of type \'string\' or \'object\'; \'%s\' provided in \'%s\'.',
gettype($event),
__METHOD__
)
);
}

if ($event instanceof EventNameAwareInterface) {
return $event->getEventName();
}
Expand Down
122 changes: 75 additions & 47 deletions test/unit/Event/ImmutableParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,27 @@
*/
final class ImmutableParametersTest extends TestCase
{
/**
* @var ParametersInterface&MockObject
*/
private $parameters;

/**
* Prepare the test case dependencies
*/
private ParametersInterface&MockObject $parameters;

public function setUp(): void
{
$this->parameters = $this->createMock(ParametersInterface::class);
}

/**
* Assert that the class implements the ParametersInterface
*/
public function testImplementsParametersInterface(): void
{
$params = new ImmutableParameters($this->parameters);

$this->assertInstanceOf(ParametersInterface::class, $params);
}

/**
* Assert that the class implements \ArrayAccess
*/
public function testImplementsArrayAccess(): void
{
$params = new ImmutableParameters($this->parameters);

$this->assertInstanceOf(\ArrayAccess::class, $params);
}

/**
* Assert that parameters can be fetched via getParams() but attempts to setParams() will be ignored
*/
public function testGetParameters(): void
{
$data = [
Expand All @@ -77,9 +62,6 @@ public function testGetParameters(): void
$this->assertEquals($data, $params->getParams());
}

/**
* Assert that calls to count() will proxy to the internal parameters collection
*/
public function testCountWillReturnParameterCount(): void
{
$parameters = new ImmutableParameters($this->parameters);
Expand All @@ -91,9 +73,6 @@ public function testCountWillReturnParameterCount(): void
$this->assertSame(5, $parameters->count());
}

/**
* Assert that calls to isEmpty() will proxy to the internal parameters collection
*/
public function testIsEmptyWillProxyToInternalParametersCollection(): void
{
$immutableParameters = new ImmutableParameters(new Parameters([]));
Expand All @@ -111,9 +90,6 @@ public function testIsEmptyWillProxyToInternalParametersCollection(): void
$this->assertFalse($immutableParameters->isEmpty());
}

/**
* Assert that calls to hasParam() will proxy to the internal parameters collection
*/
public function testHasParamWillProxyToInternalParametersCollection(): void
{
$immutableParameters = new ImmutableParameters(
Expand All @@ -130,9 +106,6 @@ public function testHasParamWillProxyToInternalParametersCollection(): void
$this->assertFalse($immutableParameters->hasParam('baz'));
}

/**
* Assert that calls to getParam() will proxy to the internal parameters collection
*/
public function testGetParamWillProxyToInternalParametersCollection(): void
{
$immutableParameters = new ImmutableParameters(
Expand All @@ -152,9 +125,6 @@ public function testGetParamWillProxyToInternalParametersCollection(): void
$this->assertSame(123, $immutableParameters->getParam('test', 123));
}

/**
* Assert that calls to removeParam() will NOT modify the internal parameters
*/
public function testRemoveParamWillReturnFalse(): void
{
$params = ['test' => 123];
Expand All @@ -165,9 +135,6 @@ public function testRemoveParamWillReturnFalse(): void
$this->assertSame($params, $immutableParameters->getParams());
}

/**
* Assert that calls to getKeys() will return the keys of the internal parameters collection
*/
public function testGetKeysWillProxyToInternalParametersCollection(): void
{
$params = [
Expand All @@ -181,9 +148,6 @@ public function testGetKeysWillProxyToInternalParametersCollection(): void
$this->assertSame(array_keys($params), $immutableParameters->getKeys());
}

/**
* Assert that calls to getValues() will return the values of the internal parameters collection
*/
public function testGetValuesWillProxyToInternalParametersCollection(): void
{
$params = [
Expand All @@ -197,9 +161,6 @@ public function testGetValuesWillProxyToInternalParametersCollection(): void
$this->assertSame(array_values($params), $immutableParameters->getValues());
}

/**
* Assert that calls to getValues() will return the values of the internal parameters collection
*/
public function testGetIteratorWillProxyToInternalParametersCollection(): void
{
$iterator = new \ArrayIterator(
Expand All @@ -219,9 +180,6 @@ public function testGetIteratorWillProxyToInternalParametersCollection(): void
$this->assertSame($iterator, $immutableParameters->getIterator());
}

/**
* Assert that calls to offsetExists() will test the keys of the internal parameters collection
*/
public function testOffsetExistsWillProxyToInternalParametersCollection(): void
{
$params = [
Expand All @@ -237,9 +195,6 @@ public function testOffsetExistsWillProxyToInternalParametersCollection(): void
$this->assertFalse(isset($immutableParameters['test']));
}

/**
* Assert that calls to offsetExists() will test the keys of the internal parameters collection
*/
public function testOffsetGetWillProxyToInternalParametersCollection(): void
{
$params = [
Expand All @@ -255,4 +210,77 @@ public function testOffsetGetWillProxyToInternalParametersCollection(): void
$this->assertSame($params['baz'], $immutableParameters['baz']);
$this->assertNull($immutableParameters['test']);
}

public function testSetParamPerformsNoModifications(): void
{
$data = [
'foo' => 123,
'bar' => 'test',
];

$params = new ImmutableParameters(new Parameters($data));

$this->assertSame($data, $params->getParams());

$params->setParam('foo', true);
$params->setParam('baz', 'Testing setter');

$this->assertSame(123, $params->getParam('foo'));
$this->assertFalse($params->hasParam('baz'));
$this->assertSame($data, $params->getParams());
}

public function testOffsetSetParamPerformsNoModifications(): void
{
$data = [
'foo' => 456,
'bar' => 'test123',
];

$params = new ImmutableParameters(new Parameters($data));

$this->assertSame($data, $params->getParams());

$params->offsetSet('foo', true);
$params->offsetSet('baz', 'Testing setter');

$this->assertSame(456, $params->getParam('foo'));
$this->assertFalse($params->hasParam('baz'));
$this->assertSame($data, $params->getParams());
}

public function testRemoveParamsWillPerformsNoModifications(): void
{
$data = [
'foo' => 123,
'bar' => 'test',
'baz' => true,
];

$params = new ImmutableParameters(new Parameters($data));

$params->removeParams(['foo', 'baz']);
$params->removeParams(['baz']);

$this->assertTrue($params->hasParam('foo'));
$this->assertSame($data, $params->getParams());
}

public function testOffsetUnsetWillPerformsNoModifications(): void
{
$data = [
'foo' => 123,
'bar' => 'test',
'baz' => true,
];

$params = new ImmutableParameters(new Parameters($data));

$params->offsetUnset('foo');
$params->offsetUnset('baz');

$this->assertTrue($params->hasParam('foo'));
$this->assertTrue($params->hasParam('baz'));
$this->assertSame($data, $params->getParams());
}
}
1 change: 1 addition & 0 deletions test/unit/Event/NamedEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/**
* @covers \Arp\EventDispatcher\Event\NamedEvent
* @covers \Arp\EventDispatcher\Event\AbstractEvent
*/
final class NamedEventTest extends TestCase
{
Expand Down
1 change: 1 addition & 0 deletions test/unit/EventDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

/**
* @covers \Arp\EventDispatcher\EventDispatcher
* @covers \Arp\EventDispatcher\AbstractEventDispatcher
*/
final class EventDispatcherTest extends TestCase
{
Expand Down
1 change: 1 addition & 0 deletions test/unit/ImmutableEventDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/**
* @covers \Arp\EventDispatcher\ImmutableEventDispatcher
* @covers \Arp\EventDispatcher\AbstractEventDispatcher
*/
final class ImmutableEventDispatcherTest extends TestCase
{
Expand Down

0 comments on commit e5b33fb

Please sign in to comment.