Skip to content

Commit

Permalink
Merge pull request #6 from bitwise-operators/codestyle-and-version-fix
Browse files Browse the repository at this point in the history
Fix codestyle issue and changelog
  • Loading branch information
Jeroen-G authored Jun 13, 2022
2 parents 8d6e8ea + a738501 commit a987a9f
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 10 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: CI

on:
push:
pull_request:

jobs:
php-tests:
Expand Down
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

## [1.4.0]
- Add option to use custom attribute classes

## [1.3.0]
Expand Down
31 changes: 21 additions & 10 deletions src/Electrician.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use JeroenG\Autowire\Attribute\Configure as ConfigureAttribute;
use JeroenG\Autowire\Attribute\ConfigureInterface as ConfigureAttributeInterface;
use JeroenG\Autowire\Exception\FaultyWiringException;
use Webmozart\Assert\Assert;
use JeroenG\Autowire\Exception\InvalidAttributeException;

final class Electrician
{
Expand All @@ -22,8 +22,8 @@ public function __construct(
private string $autowireAttribute = AutowireAttribute::class,
private string $configureAttribute = ConfigureAttribute::class
) {
self::assertValidAttributeImplementation($this->autowireAttribute, AutowireAttributeInterface::class);
self::assertValidAttributeImplementation($this->configureAttribute, ConfigureAttributeInterface::class);
self::checkValidAttributeImplementation($this->autowireAttribute, AutowireAttributeInterface::class);
self::checkValidAttributeImplementation($this->configureAttribute, ConfigureAttributeInterface::class);
}

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

return new Configuration($implementation, $configurations);
}

private static function assertValidAttributeImplementation(string $className, string $attributeInterface): void
{
Assert::classExists($className);
Assert::notEmpty((new \ReflectionClass($className))->getAttributes(\Attribute::class));
Assert::isAOf($className, $attributeInterface, "{$className} : {$attributeInterface}");
}

public function canAutowire(string $name): bool
{
Expand All @@ -79,6 +72,24 @@ public function canConfigure(string $name): bool
{
return $this->classHasAttribute($name, $this->configureAttribute);
}

/**
* @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))) {
throw InvalidAttributeException::isNotAnAttribute($className);
}

if (!is_a($className, $attributeInterface, true)) {
throw InvalidAttributeException::doesNotImplementInterface($className, $attributeInterface);
}
}

private function classHasAttribute(string $className, string $attributeName): bool
{
Expand Down
23 changes: 23 additions & 0 deletions src/Exception/InvalidAttributeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace JeroenG\Autowire\Exception;

final class InvalidAttributeException extends \RuntimeException
{
public static function doesNotExist(string $class): self
{
return new self("Class $class does not exist");
}

public static function doesNotImplementInterface(string $class, string $interface): self
{
return new self("Class $class does not implement $interface");
}

public static function isNotAnAttribute(string $class): self
{
return new self("Class $class is not an attribute");
}
}
51 changes: 51 additions & 0 deletions tests/Unit/ElectricianTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use JeroenG\Autowire\Crawler;
use JeroenG\Autowire\Electrician;
use JeroenG\Autowire\Exception\FaultyWiringException;
use JeroenG\Autowire\Exception\InvalidAttributeException;
use JeroenG\Autowire\Tests\Support\Attributes\CustomAutowire;
use JeroenG\Autowire\Tests\Support\Attributes\CustomConfigure;
use JeroenG\Autowire\Tests\Support\Subject\Contracts\GoodbyeInterface;
Expand Down Expand Up @@ -114,6 +115,31 @@ public function test_it_throws_exception_when_implementation_can_not_be_configur
$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);

$electrician = 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 @@ -133,6 +159,31 @@ 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
{
$crawler = Crawler::in([SubjectDirectory::ALL]);

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

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

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

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

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

public function test_it_can_tell_if_class_has_custom_configure_attribute(): void
{
Expand Down

0 comments on commit a987a9f

Please sign in to comment.