Skip to content

Commit

Permalink
Improved resolve handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Nov 3, 2023
1 parent cc04125 commit 7ba0592
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/DependencyInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function resolveObject(string $className, ...$parameters): object
return $reflection->newInstance();
}

public function resolve(string $class, ...$parameters): object
public function resolve(string $class, ...$parameters): mixed
{
if(stristr($class, ':'))
{
Expand Down
17 changes: 17 additions & 0 deletions tests/DependencyInjectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Packaged\Tests\DiContainer\Supporting\BasicObject;
use Packaged\Tests\DiContainer\Supporting\Cache;
use Packaged\Tests\DiContainer\Supporting\CacheInterface;
use Packaged\Tests\DiContainer\Supporting\MethodCaller;
use Packaged\Tests\DiContainer\Supporting\NeedyObject;
use Packaged\Tests\DiContainer\Supporting\ServiceInterface;
use Packaged\Tests\DiContainer\Supporting\ServiceOne;
Expand Down Expand Up @@ -232,4 +233,20 @@ public function testRetrieveAll()
static::assertInstanceOf(TestObject::class, $tObj);
static::assertEquals(2, $tObj->paramCount());
}

public function testResolve()
{
$di = new DependencyInjector();
$resolved = $di->resolve(TestObject::class, ['a', 'b']);
static::assertInstanceOf(TestObject::class, $resolved);
static::assertEquals(2, $resolved->paramCount());

$di->share(ServiceInterface::class, new ServiceOne());
$result = $di->resolve(MethodCaller::class . ':darkMode', 'apple');
static::assertEquals('light apple', $result);

$di->share(ServiceInterface::class, new ServiceTwo());
$result = $di->resolve(MethodCaller::class . ':darkMode', 'apple');
static::assertEquals('dark apple', $result);
}
}
18 changes: 18 additions & 0 deletions tests/Supporting/MethodCaller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Packaged\Tests\DiContainer\Supporting;

class MethodCaller
{
protected ServiceInterface $service;

public function __construct(ServiceInterface $service)
{
$this->service = $service;
}

public function darkMode(string $text = 'moon')
{
return ($this->service->process() ? 'dark' : 'light') . ' ' . $text;
}
}

0 comments on commit 7ba0592

Please sign in to comment.