Skip to content

Commit

Permalink
Merge pull request #140 from gacela-project/feat/return-setupGacela-f…
Browse files Browse the repository at this point in the history
…rom-gacela-php

Allow to return an instance of SetupGacela on gacela.php
  • Loading branch information
Chemaclass authored Apr 14, 2022
2 parents 78e6c10 + 57cfcb9 commit 2a952b9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ public function __construct(

public function createGacelaFileConfig(): GacelaConfigFileInterface
{
$setupGacelaFn = $this->fileIo->include($this->gacelaPhpPath);
if (!is_callable($setupGacelaFn)) {
throw new RuntimeException('Create a function that returns an anonymous class that implements SetupGacelaInterface');
/** @var SetupGacelaInterface|callable $setupGacelaFile */
$setupGacelaFile = $this->fileIo->include($this->gacelaPhpPath);
if (is_callable($setupGacelaFile)) {
trigger_deprecation('Gacela', '0.15', 'Return a SetupGacelaInterface instance directly. The callable option will be removed in the next version.');
}

/** @var object $setupGacela */
$setupGacela = $setupGacelaFn();
$setupGacela = is_callable($setupGacelaFile) ? $setupGacelaFile() : $setupGacelaFile;

if (!is_subclass_of($setupGacela, SetupGacelaInterface::class)) {
throw new RuntimeException('Your anonymous class must implements SetupGacelaInterface');
throw new RuntimeException('The gacela.php file should return an instance of SetupGacela');
}

$configBuilder = $this->createConfigBuilder($setupGacela);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@

final class GacelaConfigUsingGacelaPhpFileFactoryTest extends TestCase
{
public function test_exception_when_include_gacela_file_is_not_callable(): void
public function test_exception_when_the_class_does_not_implements_setup_gacela_interface(): void
{
$fileIo = $this->createStub(FileIoInterface::class);
$fileIo->method('include')->willReturn('anything-but-not-callable');
$fileIo->method('include')->willReturn(new class() {});

$factory = new GacelaConfigUsingGacelaPhpFileFactory(
'gacelaPhpPath',
$this->createStub(SetupGacelaInterface::class),
$fileIo
);

$this->expectErrorMessage('Create a function that returns an anonymous class that implements SetupGacelaInterface');
$this->expectErrorMessage('The gacela.php file should return an instance of SetupGacela');
$factory->createGacelaFileConfig();
}

Expand All @@ -47,7 +47,7 @@ public function test_exception_when_is_callable_but_does_not_extends_abstract_co
$fileIo
);

$this->expectErrorMessage('Your anonymous class must implements SetupGacelaInterface');
$this->expectErrorMessage('The gacela.php file should return an instance of SetupGacela');
$factory->createGacelaFileConfig();
}

Expand Down Expand Up @@ -93,7 +93,7 @@ public function test_gacela_file_set_mapping_interfaces(): void
{
$fileIo = $this->createStub(FileIoInterface::class);
$fileIo->method('include')->willReturn(
static fn () => (new SetupGacela())
(new SetupGacela())
->setExternalServices(['externalServiceKey' => 'externalServiceValue'])
->setMappingInterfaces(
static function (MappingInterfacesBuilder $mappingInterfacesBuilder, array $externalServices): void {
Expand All @@ -119,12 +119,14 @@ static function (MappingInterfacesBuilder $mappingInterfacesBuilder, array $exte
public function test_gacela_file_set_suffix_types(): void
{
$fileIo = $this->createStub(FileIoInterface::class);
$fileIo->method('include')->willReturn(static fn () => (new SetupGacela())
->setSuffixTypes(
static function (SuffixTypesBuilder $suffixTypesBuilder): void {
$suffixTypesBuilder->addDependencyProvider('Binding');
}
));
$fileIo->method('include')->willReturn(
(new SetupGacela())
->setSuffixTypes(
static function (SuffixTypesBuilder $suffixTypesBuilder): void {
$suffixTypesBuilder->addDependencyProvider('Binding');
}
)
);

$factory = new GacelaConfigUsingGacelaPhpFileFactory(
'gacelaPhpPath',
Expand Down

0 comments on commit 2a952b9

Please sign in to comment.