Skip to content

Commit a987a9f

Browse files
authored
Merge pull request #6 from bitwise-operators/codestyle-and-version-fix
Fix codestyle issue and changelog
2 parents 8d6e8ea + a738501 commit a987a9f

5 files changed

Lines changed: 98 additions & 10 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name: CI
33

44
on:
55
push:
6+
pull_request:
67

78
jobs:
89
php-tests:

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## Unreleased
8+
9+
## [1.4.0]
810
- Add option to use custom attribute classes
911

1012
## [1.3.0]

src/Electrician.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use JeroenG\Autowire\Attribute\Configure as ConfigureAttribute;
1010
use JeroenG\Autowire\Attribute\ConfigureInterface as ConfigureAttributeInterface;
1111
use JeroenG\Autowire\Exception\FaultyWiringException;
12-
use Webmozart\Assert\Assert;
12+
use JeroenG\Autowire\Exception\InvalidAttributeException;
1313

1414
final class Electrician
1515
{
@@ -22,8 +22,8 @@ public function __construct(
2222
private string $autowireAttribute = AutowireAttribute::class,
2323
private string $configureAttribute = ConfigureAttribute::class
2424
) {
25-
self::assertValidAttributeImplementation($this->autowireAttribute, AutowireAttributeInterface::class);
26-
self::assertValidAttributeImplementation($this->configureAttribute, ConfigureAttributeInterface::class);
25+
self::checkValidAttributeImplementation($this->autowireAttribute, AutowireAttributeInterface::class);
26+
self::checkValidAttributeImplementation($this->configureAttribute, ConfigureAttributeInterface::class);
2727
}
2828

2929
public function connect(string $interface): Wire
@@ -62,13 +62,6 @@ public function configure(string $implementation): Configuration
6262

6363
return new Configuration($implementation, $configurations);
6464
}
65-
66-
private static function assertValidAttributeImplementation(string $className, string $attributeInterface): void
67-
{
68-
Assert::classExists($className);
69-
Assert::notEmpty((new \ReflectionClass($className))->getAttributes(\Attribute::class));
70-
Assert::isAOf($className, $attributeInterface, "{$className} : {$attributeInterface}");
71-
}
7265

7366
public function canAutowire(string $name): bool
7467
{
@@ -79,6 +72,24 @@ public function canConfigure(string $name): bool
7972
{
8073
return $this->classHasAttribute($name, $this->configureAttribute);
8174
}
75+
76+
/**
77+
* @throws InvalidAttributeException
78+
*/
79+
private static function checkValidAttributeImplementation(string $className, string $attributeInterface): void
80+
{
81+
if (!class_exists($className)) {
82+
throw InvalidAttributeException::doesNotExist($className);
83+
}
84+
85+
if (empty((new \ReflectionClass($className))->getAttributes(\Attribute::class))) {
86+
throw InvalidAttributeException::isNotAnAttribute($className);
87+
}
88+
89+
if (!is_a($className, $attributeInterface, true)) {
90+
throw InvalidAttributeException::doesNotImplementInterface($className, $attributeInterface);
91+
}
92+
}
8293

8394
private function classHasAttribute(string $className, string $attributeName): bool
8495
{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace JeroenG\Autowire\Exception;
6+
7+
final class InvalidAttributeException extends \RuntimeException
8+
{
9+
public static function doesNotExist(string $class): self
10+
{
11+
return new self("Class $class does not exist");
12+
}
13+
14+
public static function doesNotImplementInterface(string $class, string $interface): self
15+
{
16+
return new self("Class $class does not implement $interface");
17+
}
18+
19+
public static function isNotAnAttribute(string $class): self
20+
{
21+
return new self("Class $class is not an attribute");
22+
}
23+
}

tests/Unit/ElectricianTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use JeroenG\Autowire\Crawler;
1010
use JeroenG\Autowire\Electrician;
1111
use JeroenG\Autowire\Exception\FaultyWiringException;
12+
use JeroenG\Autowire\Exception\InvalidAttributeException;
1213
use JeroenG\Autowire\Tests\Support\Attributes\CustomAutowire;
1314
use JeroenG\Autowire\Tests\Support\Attributes\CustomConfigure;
1415
use JeroenG\Autowire\Tests\Support\Subject\Contracts\GoodbyeInterface;
@@ -114,6 +115,31 @@ public function test_it_throws_exception_when_implementation_can_not_be_configur
114115
$electrician->configure(GoodbyeInterface::class);
115116
}
116117

118+
/** @dataProvider invalidAutowireAttributeProvider */
119+
public function test_it_throws_an_exception_on_an_invalid_custom_autowire_attribute(string $invalidAttribute): void
120+
{
121+
$crawler = Crawler::in([SubjectDirectory::ALL]);
122+
123+
$this->expectException(InvalidAttributeException::class);
124+
125+
$electrician = new Electrician($crawler, $invalidAttribute);
126+
}
127+
128+
public function invalidAutowireAttributeProvider(): \Generator
129+
{
130+
yield 'text' => [
131+
'Hello, World!',
132+
];
133+
134+
yield 'non-attribute class' => [
135+
HowDoYouDoInterface::class,
136+
];
137+
138+
yield 'wrong attribute class' => [
139+
CustomConfigure::class,
140+
];
141+
}
142+
117143
public function test_it_can_tell_if_class_has_custom_autowire_attribute(): void
118144
{
119145
$crawler = Crawler::in([SubjectDirectory::ALL]);
@@ -133,6 +159,31 @@ public function test_it_can_connect_implementation_with_custom_autowire_attribut
133159
self::assertEquals(HowDoYouDoInterface::class, $wire->interface);
134160
self::assertEquals(MoonClass::class, $wire->implementation);
135161
}
162+
163+
/** @dataProvider invalidConfigureAttributeProvider */
164+
public function test_it_throws_an_exception_on_an_invalid_custom_configure_attribute(string $invalidAttribute): void
165+
{
166+
$crawler = Crawler::in([SubjectDirectory::ALL]);
167+
168+
$this->expectException(InvalidAttributeException::class);
169+
170+
$electrician = new Electrician(crawler: $crawler, configureAttribute: $invalidAttribute);
171+
}
172+
173+
public function invalidConfigureAttributeProvider(): \Generator
174+
{
175+
yield 'text' => [
176+
'Hello, World!',
177+
];
178+
179+
yield 'non-attribute class' => [
180+
HowDoYouDoInterface::class,
181+
];
182+
183+
yield 'wrong attribute class' => [
184+
CustomAutowire::class,
185+
];
186+
}
136187

137188
public function test_it_can_tell_if_class_has_custom_configure_attribute(): void
138189
{

0 commit comments

Comments
 (0)