Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  test to build a stack of more than 3 services
  allow the lowest stack service to be from a dependency
  add codecov
  throw a custom exception when service not found
  remove the fixtures folder from the coverage report
  • Loading branch information
Baptouuuu committed Apr 14, 2018
2 parents 49fa61e + c7823f9 commit eb492df
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -7,3 +7,5 @@ script: vendor/bin/phpunit --coverage-clover=coverage.clover
after_script:
- if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
- if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi
after_success:
- bash <(curl -s https://codecov.io/bash)
1 change: 1 addition & 0 deletions phpunit.xml.dist
Expand Up @@ -13,6 +13,7 @@
<exclude>
<directory>./tests</directory>
<directory>./vendor</directory>
<directory>./fixtures</directory>
</exclude>
</whitelist>
</filter>
Expand Down
8 changes: 8 additions & 0 deletions src/Exception/ServiceNotFound.php
@@ -0,0 +1,8 @@
<?php
declare(strict_types = 1);

namespace Innmind\Compose\Exception;

final class ServiceNotFound extends LogicException
{
}
30 changes: 23 additions & 7 deletions src/Services.php
Expand Up @@ -12,7 +12,8 @@
Compilation,
Exception\ArgumentNotProvided,
Exception\ArgumentNotDefined,
Exception\CircularDependency
Exception\CircularDependency,
Exception\ServiceNotFound,
};
use Innmind\Immutable\{
Sequence,
Expand Down Expand Up @@ -84,22 +85,29 @@ public function expose(Name $name, Name $as): self

public function stack(Name $name, Name $highest, Name $lower, Name ...$rest): self
{
$stack = Sequence::of($lower, ...$rest)->reverse();
$stack = Sequence::of($highest, $lower, ...$rest)->reverse();
$stacked = Sequence::of(
$this->get($stack->first())
$this->decorate(
$stack->get(1),
$stack->first(),
$stack->size() === 2 ? $name : null
)
);

$stacked = $stack->drop(1)->reduce(
$stacked = $stack->drop(2)->dropEnd(1)->reduce(
$stacked,
function(Sequence $stacked, Name $decorator): Sequence {
return $stacked->add(
$this->decorate($decorator, $stacked->last()->name())
);
}
);
$stacked = $stacked->add(
$this->decorate($highest, $stacked->last()->name(), $name)
);

if ($stack->size() > 2) {
$stacked = $stacked->add(
$this->decorate($highest, $stacked->last()->name(), $name)
);
}

$self = clone $this;
$self->definitions = $stacked->reduce(
Expand Down Expand Up @@ -201,8 +209,16 @@ public function get(Name $name): Service
try {
return $this->definitions->get((string) $name);
} catch (\Exception $e) {
//pass
}

try {
return $this->exposed->get((string) $name);
} catch (\Exception $e) {
//pass
}

throw new ServiceNotFound((string) $name);
}

/**
Expand Down
57 changes: 56 additions & 1 deletion tests/ServicesTest.php
Expand Up @@ -20,6 +20,7 @@
Definition\Dependency,
Exception\CircularDependency,
Exception\ArgumentNotProvided,
Exception\ServiceNotFound,
Compilation\Services as CompiledServices
};
use Innmind\Immutable\{
Expand Down Expand Up @@ -415,6 +416,12 @@ public function testStack()
new Name('low'),
Construct::fromString(Str::of(Low::class))
),
new Service(
new Name('middle'),
Construct::fromString(Str::of(Middle::class)),
$this->args->load('@decorated'),
$this->args->load('foo')
),
new Service(
new Name('high'),
Construct::fromString(Str::of(High::class)),
Expand All @@ -425,6 +432,7 @@ public function testStack()
$services2 = $services->stack(
new Name('stack'),
new Name('high'),
new Name('middle'),
new Name('dep.bar'),
new Name('low')
);
Expand All @@ -434,7 +442,7 @@ public function testStack()
$this->assertFalse($services->has(new Name('stack')));
$this->assertTrue($services2->has(new Name('stack')));
$this->assertSame(
'high|middle|low|middle|high',
'high|foo|middle|low|middle|foo|high',
$services2->build(new Name('stack'))()
);
}
Expand Down Expand Up @@ -550,4 +558,51 @@ public function testCompile()

$this->assertInstanceOf(CompiledServices::class, $services->compile());
}

public function testThrowWhenTryingToGetUnknownService()
{
$this->expectException(ServiceNotFound::class);
$this->expectExceptionMessage('foo');

(new Services(
new Arguments,
new Dependencies
))->get(new Name('foo'));
}

public function testTheLowestStackServiceCanComeFromADependeny()
{
$services = new Services(
new Arguments,
new Dependencies(
new Dependency(
new Name('dep'),
new Services(
new Arguments,
new Dependencies,
(new Service(
new Name('foo'),
Construct::fromString(Str::of(Low::class))
))->exposeAs(new Name('bar'))
)
)
),
new Service(
new Name('middle'),
Construct::fromString(Str::of(Middle::class)),
$this->args->load('@decorated')
)
);

$services2 = $services->stack(
new Name('stack'),
new Name('middle'),
new Name('dep.bar')
);

$this->assertSame(
'middle|low|middle',
$services2->build(new Name('stack'))()
);
}
}

0 comments on commit eb492df

Please sign in to comment.