Skip to content

Commit

Permalink
DI: set type properly
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Aug 24, 2022
1 parent 7fb8abe commit f93137d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Extension/ResourceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public function beforeCompile(): void
}

$def = $builder->addDefinition($this->prefix($name . '.' . $counter++))
->setFactory($class);
->setFactory($class)
->setType($class);

$decorator = $resource->decorator;

Expand Down
28 changes: 28 additions & 0 deletions tests/Cases/Extension/ResourceExtension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ use Contributte\DI\Extension\ResourceExtension;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Nette\DI\Extensions\InjectExtension;
use Nette\DI\MissingServiceException;
use Nette\DI\ServiceCreationException;
use Tester\Assert;
use Tester\FileMock;
use Tests\Fixtures\Bar\BarService;
use Tests\Fixtures\Baz\BazService;
use Tests\Fixtures\Baz\Nested\NestedBazService;
use Tests\Fixtures\Decorator\InjectService;
use Tests\Fixtures\Foo\FooBarService;
use Tests\Fixtures\Foo\FooService;
use Tests\Fixtures\Scalar\ScalarService;
Expand Down Expand Up @@ -265,3 +267,29 @@ test(static function (): void {
}, 12);
}, ServiceCreationException::class, "Service 'autoload._Tests_Fixtures_Scalar_.2' (type of Tests\Fixtures\Scalar\ScalarService): Parameter \$text in ScalarService::__construct() has no class type or default value, so its value must be specified.");
});

// Register services manually (exception)
test(static function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
$class = $loader->load(static function (Compiler $compiler): void {
$compiler->addExtension('autoload', new ResourceExtension());
$compiler->addExtension('inject', new InjectExtension());
$compiler->addConfig(['parameters' => ['appDir' => TESTER_DIR]]);
$compiler->loadConfig(FileMock::create('
autoload:
resources:
Tests\Fixtures\Decorator\:
paths: [%appDir%/fixtures/Decorator]
decorator:
inject: true
', 'neon'));
}, 13);

/** @var Container $container */
$container = new $class();

/** @var InjectService $service */
$service = $container->getByType(InjectService::class);

Assert::notNull($service->authenticator);
});
8 changes: 8 additions & 0 deletions tests/fixtures/Decorator/Authenticator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace Tests\Fixtures\Decorator;

class Authenticator
{

}
16 changes: 16 additions & 0 deletions tests/fixtures/Decorator/InjectService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

namespace Tests\Fixtures\Decorator;

class InjectService
{

/** @var Authenticator */
public $authenticator;

public function injectAuthenticator(Authenticator $authenticator): void
{
$this->authenticator = $authenticator;
}

}

0 comments on commit f93137d

Please sign in to comment.