Skip to content

Commit

Permalink
Refactor mixin components from extensions (#26)
Browse files Browse the repository at this point in the history
We changing strategy to mixing components, because, if you have more components in you extension, which's must are mixing with one component, you don't doing this.
  • Loading branch information
pvsaintpe authored Sep 10, 2019
1 parent 40a48a2 commit 74a6a0d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ class SomeExtension extends AbstractExtension
public static function getMixins(): array
{
return [
Blueprint::class => SomeBlueprint::class,
PostgresConnection::class => SomeConnection::class,
PostgresGrammar::class => SomeSchemaGrammar::class,
SomeBlueprint::class => Blueprint::class,
SomeConnection::class => PostgresConnection::class,
SomeSchemaGrammar::class => PostgresGrammar::class,
...
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Extensions/AbstractExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static function getTypes(): array

final public static function register(): void
{
collect(static::getMixins())->each(static function ($mixin, $extension) {
collect(static::getMixins())->each(static function ($extension, $mixin) {
if (!is_subclass_of($mixin, AbstractComponent::class)) {
throw new MixinInvalidException(sprintf(
'Mixed class %s is not descendant of %s.',
Expand Down
68 changes: 40 additions & 28 deletions tests/Unit/Extensions/AbstractExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,7 @@ class AbstractExtensionTest extends TestCase
/** @test */
public function registerInvalidExtension(): void
{
$abstractExtension = new class() extends AbstractExtension {
public static function getName(): string
{
return 'extension';
}

public static function getMixins(): array
{
return [
Blueprint::class => new class() extends Model {
},
];
}
};
$abstractExtension = new ExtensionStub();

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

Expand All @@ -42,24 +29,49 @@ public static function getMixins(): array
/** @test */
public function registerWithInvalidMixin(): void
{
$abstractExtension = new class() extends AbstractExtension {
public static function getName(): string
{
return 'extension';
}

public static function getMixins(): array
{
return [
ServiceProvider::class => new class() extends AbstractComponent {
},
];
}
};
$abstractExtension = new InvalidExtensionStub();

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

/** @var AbstractExtension $abstractExtension */
$abstractExtension::register();
}
}

class InvalidExtensionStub extends AbstractExtension
{
public static function getName(): string
{
return 'extension';
}

public static function getMixins(): array
{
return [
ComponentStub::class => ServiceProvider::class,
];
}
}

class ComponentStub extends AbstractComponent
{
}

class ExtensionStub extends AbstractExtension
{
public static function getName(): string
{
return 'extension';
}

public static function getMixins(): array
{
return [
InvalidComponentStub::class => Blueprint::class,
];
}
}

class InvalidComponentStub extends Model
{
}

0 comments on commit 74a6a0d

Please sign in to comment.