Skip to content

Commit 7425cfe

Browse files
committed
refactor tests to kill last mutants
1 parent 16b59f7 commit 7425cfe

File tree

5 files changed

+86
-51
lines changed

5 files changed

+86
-51
lines changed

src/Electrician.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,15 @@ public function canConfigure(string $name): bool
110110
return $this->classHasAttribute($name, $this->configureAttribute);
111111
}
112112

113-
/**
114-
* @throws InvalidAttributeException
115-
*/
113+
/** @throws InvalidAttributeException */
116114
private static function checkValidAttributeImplementation(string $className, string $attributeInterface): void
117115
{
118116
if (! class_exists($className)) {
119117
throw InvalidAttributeException::doesNotExist($className);
120118
}
121119

122-
if (empty((new ReflectionClass($className))->getAttributes(Attribute::class))) {
120+
$classDoesNotHaveGenericAttribute = empty((new ReflectionClass($className))->getAttributes(Attribute::class));
121+
if ($classDoesNotHaveGenericAttribute) {
123122
throw InvalidAttributeException::isNotAnAttribute($className);
124123
}
125124

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace JeroenG\Autowire\Tests\Support\Attributes;
6+
7+
class EmptyClass
8+
{
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace JeroenG\Autowire\Tests\Support\Attributes;
6+
7+
use Attribute;
8+
use JeroenG\Autowire\Attribute\AutowireInterface;
9+
use JeroenG\Autowire\Attribute\ListenInterface;
10+
11+
class NotAnAttribute implements AutowireInterface
12+
{
13+
public function __construct(public string $nope)
14+
{
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace JeroenG\Autowire\Tests\Support\Attributes;
6+
7+
use Attribute;
8+
use JeroenG\Autowire\Attribute\AutowireInterface;
9+
10+
#[Attribute(Attribute::TARGET_CLASS)]
11+
class WrongAttribute
12+
{
13+
}

tests/Unit/ElectricianTest.php

+45-47
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
namespace JeroenG\Autowire\Tests\Unit;
66

77
use Generator;
8+
use JeroenG\Autowire\Attribute\Autowire;
9+
use JeroenG\Autowire\Attribute\Configure;
10+
use JeroenG\Autowire\Attribute\Listen;
811
use JeroenG\Autowire\ConfigurationType;
912
use JeroenG\Autowire\ConfigurationValue;
1013
use JeroenG\Autowire\Crawler;
@@ -14,6 +17,9 @@
1417
use JeroenG\Autowire\Tests\Support\Attributes\CustomAutowire;
1518
use JeroenG\Autowire\Tests\Support\Attributes\CustomConfigure;
1619
use JeroenG\Autowire\Tests\Support\Attributes\CustomListen;
20+
use JeroenG\Autowire\Tests\Support\Attributes\EmptyClass;
21+
use JeroenG\Autowire\Tests\Support\Attributes\NotAnAttribute;
22+
use JeroenG\Autowire\Tests\Support\Attributes\WrongAttribute;
1723
use JeroenG\Autowire\Tests\Support\Subject\Contracts\GoodbyeInterface;
1824
use JeroenG\Autowire\Tests\Support\Subject\Contracts\HelloInterface;
1925
use JeroenG\Autowire\Tests\Support\Subject\Contracts\HowDoYouDoInterface;
@@ -118,32 +124,7 @@ public function test_it_throws_exception_when_implementation_can_not_be_configur
118124
$this->expectExceptionMessage('No JeroenG\Autowire\Attribute\Configure found in '.GoodbyeInterface::class);
119125
$electrician->configure(GoodbyeInterface::class);
120126
}
121-
122-
/** @dataProvider invalidAutowireAttributeProvider */
123-
public function test_it_throws_an_exception_on_an_invalid_custom_autowire_attribute(string $invalidAttribute): void
124-
{
125-
$crawler = Crawler::in([SubjectDirectory::ALL]);
126127

127-
$this->expectException(InvalidAttributeException::class);
128-
129-
new Electrician($crawler, $invalidAttribute);
130-
}
131-
132-
public function invalidAutowireAttributeProvider(): Generator
133-
{
134-
yield 'text' => [
135-
'Hello, World!',
136-
];
137-
138-
yield 'non-attribute class' => [
139-
HowDoYouDoInterface::class,
140-
];
141-
142-
yield 'wrong attribute class' => [
143-
CustomConfigure::class,
144-
];
145-
}
146-
147128
public function test_it_can_tell_if_class_has_custom_autowire_attribute(): void
148129
{
149130
$crawler = Crawler::in([SubjectDirectory::ALL]);
@@ -163,29 +144,55 @@ public function test_it_can_connect_implementation_with_custom_autowire_attribut
163144
self::assertEquals(HowDoYouDoInterface::class, $wire->interface);
164145
self::assertEquals(MoonClass::class, $wire->implementation);
165146
}
166-
167-
/** @dataProvider invalidConfigureAttributeProvider */
168-
public function test_it_throws_an_exception_on_an_invalid_custom_configure_attribute(string $invalidAttribute): void
147+
148+
/** @dataProvider invalidAttributeProvider */
149+
public function test_it_throws_an_exception_on_an_invalid_custom_autowire_attribute(string $invalidAttribute, string $message): void
169150
{
170151
$crawler = Crawler::in([SubjectDirectory::ALL]);
171152

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

174-
$electrician = new Electrician(crawler: $crawler, configureAttribute: $invalidAttribute);
156+
new Electrician(crawler: $crawler, autowireAttribute: $invalidAttribute);
175157
}
176-
177-
public function invalidConfigureAttributeProvider(): Generator
158+
159+
/** @dataProvider invalidAttributeProvider */
160+
public function test_it_throws_an_exception_on_an_invalid_custom_configure_attribute(string $invalidAttribute, string $message): void
178161
{
179-
yield 'text' => [
162+
$crawler = Crawler::in([SubjectDirectory::ALL]);
163+
164+
$this->expectException(InvalidAttributeException::class);
165+
$this->expectExceptionMessage(sprintf($message, Configure::class));
166+
167+
new Electrician(crawler: $crawler, configureAttribute: $invalidAttribute);
168+
}
169+
170+
/** @dataProvider invalidAttributeProvider */
171+
public function test_it_throws_an_exception_on_an_invalid_custom_listen_attribute(string $invalidAttribute, string $message): void
172+
{
173+
$crawler = Crawler::in([SubjectDirectory::ALL]);
174+
175+
$this->expectException(InvalidAttributeException::class);
176+
$this->expectExceptionMessage(sprintf($message, Listen::class));
177+
178+
new Electrician(crawler: $crawler, listenAttribute: $invalidAttribute);
179+
}
180+
181+
public function invalidAttributeProvider(): Generator
182+
{
183+
yield 'This is text, what are you doing?' => [
180184
'Hello, World!',
185+
'Class Hello, World! does not exist'
181186
];
182-
183-
yield 'non-attribute class' => [
184-
HowDoYouDoInterface::class,
187+
188+
yield 'This is a class, but not an attribute.' => [
189+
EmptyClass::class,
190+
'Class JeroenG\Autowire\Tests\Support\Attributes\EmptyClass is not an attribute'
185191
];
186-
187-
yield 'wrong attribute class' => [
188-
CustomAutowire::class,
192+
193+
yield 'Wrong attribute class.' => [
194+
WrongAttribute::class,
195+
'Class JeroenG\Autowire\Tests\Support\Attributes\WrongAttribute does not implement %s'
189196
];
190197
}
191198

@@ -233,15 +240,6 @@ public function test_it_can_retrieve_events_from_listener(): void
233240
], $events);
234241
}
235242

236-
public function test_it_throws_an_exception_on_an_invalid_custom_listen_attribute(): void
237-
{
238-
$crawler = Crawler::in([SubjectDirectory::ALL]);
239-
240-
$this->expectException(InvalidAttributeException::class);
241-
242-
new Electrician(crawler: $crawler, listenAttribute: HelloInterface::class);
243-
}
244-
245243
public function test_it_throws_exception_on_a_class_missing_listeners(): void
246244
{
247245
$this->expectException(FaultyWiringException::class);

0 commit comments

Comments
 (0)