Skip to content

Commit

Permalink
refactor tests to kill last mutants
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen-G committed Jun 28, 2022
1 parent 16b59f7 commit 7425cfe
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 51 deletions.
7 changes: 3 additions & 4 deletions src/Electrician.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,15 @@ public function canConfigure(string $name): bool
return $this->classHasAttribute($name, $this->configureAttribute);
}

/**
* @throws InvalidAttributeException
*/
/** @throws InvalidAttributeException */
private static function checkValidAttributeImplementation(string $className, string $attributeInterface): void
{
if (! class_exists($className)) {
throw InvalidAttributeException::doesNotExist($className);
}

if (empty((new ReflectionClass($className))->getAttributes(Attribute::class))) {
$classDoesNotHaveGenericAttribute = empty((new ReflectionClass($className))->getAttributes(Attribute::class));
if ($classDoesNotHaveGenericAttribute) {
throw InvalidAttributeException::isNotAnAttribute($className);
}

Expand Down
9 changes: 9 additions & 0 deletions tests/Support/Attributes/EmptyClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace JeroenG\Autowire\Tests\Support\Attributes;

class EmptyClass
{
}
16 changes: 16 additions & 0 deletions tests/Support/Attributes/NotAnAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace JeroenG\Autowire\Tests\Support\Attributes;

use Attribute;
use JeroenG\Autowire\Attribute\AutowireInterface;
use JeroenG\Autowire\Attribute\ListenInterface;

class NotAnAttribute implements AutowireInterface
{
public function __construct(public string $nope)
{
}
}
13 changes: 13 additions & 0 deletions tests/Support/Attributes/WrongAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace JeroenG\Autowire\Tests\Support\Attributes;

use Attribute;
use JeroenG\Autowire\Attribute\AutowireInterface;

#[Attribute(Attribute::TARGET_CLASS)]
class WrongAttribute
{
}
92 changes: 45 additions & 47 deletions tests/Unit/ElectricianTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace JeroenG\Autowire\Tests\Unit;

use Generator;
use JeroenG\Autowire\Attribute\Autowire;
use JeroenG\Autowire\Attribute\Configure;
use JeroenG\Autowire\Attribute\Listen;
use JeroenG\Autowire\ConfigurationType;
use JeroenG\Autowire\ConfigurationValue;
use JeroenG\Autowire\Crawler;
Expand All @@ -14,6 +17,9 @@
use JeroenG\Autowire\Tests\Support\Attributes\CustomAutowire;
use JeroenG\Autowire\Tests\Support\Attributes\CustomConfigure;
use JeroenG\Autowire\Tests\Support\Attributes\CustomListen;
use JeroenG\Autowire\Tests\Support\Attributes\EmptyClass;
use JeroenG\Autowire\Tests\Support\Attributes\NotAnAttribute;
use JeroenG\Autowire\Tests\Support\Attributes\WrongAttribute;
use JeroenG\Autowire\Tests\Support\Subject\Contracts\GoodbyeInterface;
use JeroenG\Autowire\Tests\Support\Subject\Contracts\HelloInterface;
use JeroenG\Autowire\Tests\Support\Subject\Contracts\HowDoYouDoInterface;
Expand Down Expand Up @@ -118,32 +124,7 @@ public function test_it_throws_exception_when_implementation_can_not_be_configur
$this->expectExceptionMessage('No JeroenG\Autowire\Attribute\Configure found in '.GoodbyeInterface::class);
$electrician->configure(GoodbyeInterface::class);
}

/** @dataProvider invalidAutowireAttributeProvider */
public function test_it_throws_an_exception_on_an_invalid_custom_autowire_attribute(string $invalidAttribute): void
{
$crawler = Crawler::in([SubjectDirectory::ALL]);

$this->expectException(InvalidAttributeException::class);

new Electrician($crawler, $invalidAttribute);
}

public function invalidAutowireAttributeProvider(): Generator
{
yield 'text' => [
'Hello, World!',
];

yield 'non-attribute class' => [
HowDoYouDoInterface::class,
];

yield 'wrong attribute class' => [
CustomConfigure::class,
];
}

public function test_it_can_tell_if_class_has_custom_autowire_attribute(): void
{
$crawler = Crawler::in([SubjectDirectory::ALL]);
Expand All @@ -163,29 +144,55 @@ public function test_it_can_connect_implementation_with_custom_autowire_attribut
self::assertEquals(HowDoYouDoInterface::class, $wire->interface);
self::assertEquals(MoonClass::class, $wire->implementation);
}
/** @dataProvider invalidConfigureAttributeProvider */
public function test_it_throws_an_exception_on_an_invalid_custom_configure_attribute(string $invalidAttribute): void

/** @dataProvider invalidAttributeProvider */
public function test_it_throws_an_exception_on_an_invalid_custom_autowire_attribute(string $invalidAttribute, string $message): void
{
$crawler = Crawler::in([SubjectDirectory::ALL]);

$this->expectException(InvalidAttributeException::class);
$this->expectExceptionMessage(sprintf($message, Autowire::class));

$electrician = new Electrician(crawler: $crawler, configureAttribute: $invalidAttribute);
new Electrician(crawler: $crawler, autowireAttribute: $invalidAttribute);
}

public function invalidConfigureAttributeProvider(): Generator

/** @dataProvider invalidAttributeProvider */
public function test_it_throws_an_exception_on_an_invalid_custom_configure_attribute(string $invalidAttribute, string $message): void
{
yield 'text' => [
$crawler = Crawler::in([SubjectDirectory::ALL]);

$this->expectException(InvalidAttributeException::class);
$this->expectExceptionMessage(sprintf($message, Configure::class));

new Electrician(crawler: $crawler, configureAttribute: $invalidAttribute);
}

/** @dataProvider invalidAttributeProvider */
public function test_it_throws_an_exception_on_an_invalid_custom_listen_attribute(string $invalidAttribute, string $message): void
{
$crawler = Crawler::in([SubjectDirectory::ALL]);

$this->expectException(InvalidAttributeException::class);
$this->expectExceptionMessage(sprintf($message, Listen::class));

new Electrician(crawler: $crawler, listenAttribute: $invalidAttribute);
}

public function invalidAttributeProvider(): Generator
{
yield 'This is text, what are you doing?' => [
'Hello, World!',
'Class Hello, World! does not exist'
];

yield 'non-attribute class' => [
HowDoYouDoInterface::class,

yield 'This is a class, but not an attribute.' => [
EmptyClass::class,
'Class JeroenG\Autowire\Tests\Support\Attributes\EmptyClass is not an attribute'
];

yield 'wrong attribute class' => [
CustomAutowire::class,

yield 'Wrong attribute class.' => [
WrongAttribute::class,
'Class JeroenG\Autowire\Tests\Support\Attributes\WrongAttribute does not implement %s'
];
}

Expand Down Expand Up @@ -233,15 +240,6 @@ public function test_it_can_retrieve_events_from_listener(): void
], $events);
}

public function test_it_throws_an_exception_on_an_invalid_custom_listen_attribute(): void
{
$crawler = Crawler::in([SubjectDirectory::ALL]);

$this->expectException(InvalidAttributeException::class);

new Electrician(crawler: $crawler, listenAttribute: HelloInterface::class);
}

public function test_it_throws_exception_on_a_class_missing_listeners(): void
{
$this->expectException(FaultyWiringException::class);
Expand Down

0 comments on commit 7425cfe

Please sign in to comment.