Skip to content

Commit

Permalink
Merge pull request #340 from wenbinye/feature-make-index-based-parame…
Browse files Browse the repository at this point in the history
…ters

make function accept index-based array as parameters
  • Loading branch information
huangzhhui committed Aug 6, 2019
2 parents abf5146 + 7ba1fd0 commit e60cd3e
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -18,6 +18,7 @@
- [#333](https://github.com/hyperf-cloud/hyperf/pull/333) Fixed Function Redis::delete() is deprecated.
- [#334](https://github.com/hyperf-cloud/hyperf/pull/334) Fixed configuration of aliyun acm is not work expected.
- [#337](https://github.com/hyperf-cloud/hyperf/pull/337) Fixed 500 response when key of header is not string.
- [#340](https://github.com/hyperf-cloud/hyperf/pull/340) Fixed function `make` not support index-based array as parameters.

# v1.0.9 - 2019-08-03

Expand Down
2 changes: 2 additions & 0 deletions src/di/src/Resolver/ParameterResolver.php
Expand Up @@ -44,6 +44,8 @@ public function resolveParameters(
foreach ($method->getParameters() as $index => $parameter) {
if (array_key_exists($parameter->getName(), $parameters)) {
$value = &$parameters[$parameter->getName()];
} elseif (array_key_exists($index, $parameters)) {
$value = &$parameters[$index];
} elseif (array_key_exists($index, $definitionParameters)) {
$value = &$definitionParameters[$index];
} else {
Expand Down
47 changes: 44 additions & 3 deletions src/di/tests/MakeTest.php
Expand Up @@ -12,6 +12,12 @@

namespace HyperfTest\Di;

use Hyperf\Di\Annotation\Scanner;
use Hyperf\Di\Container;
use Hyperf\Di\Definition\DefinitionSource;
use Hyperf\Utils\ApplicationContext;
use HyperfTest\Di\Stub\Bar;
use HyperfTest\Di\Stub\Demo;
use HyperfTest\Di\Stub\Foo;
use PHPUnit\Framework\TestCase;

Expand All @@ -21,18 +27,53 @@
*/
class MakeTest extends TestCase
{
public function setUp()
{
$container = new Container(new DefinitionSource([], [], new Scanner()));
ApplicationContext::setContainer($container);
}

public function testMakeFunction()
{
$this->assertTrue(function_exists('make'));
$this->assertInstanceOf(Foo::class, $foo = make(Foo::class, [
'string' => '123',
'int' => 123,
'int' => 234,
]));
$this->assertSame('123', $foo->string);
$this->assertSame(123, $foo->int);
$this->assertSame(234, $foo->int);

$this->assertInstanceOf(Foo::class, $foo = make(Foo::class, ['123', 123]));
$this->assertInstanceOf(Foo::class, $foo = make(Foo::class, ['123', 234]));
$this->assertSame('123', $foo->string);
$this->assertSame(234, $foo->int);
}

public function testMakeIndexedParameters()
{
$this->assertTrue(function_exists('make'));
$this->assertInstanceOf(Foo::class, $foo = make(Foo::class, ['123', 'int' => 234]));
$this->assertSame('123', $foo->string);
$this->assertSame(234, $foo->int);

$this->assertInstanceOf(Foo::class, $foo = make(Foo::class, [1 => 123, 'string' => '123']));
$this->assertSame('123', $foo->string);
$this->assertSame(123, $foo->int);
}

public function testMakeDependenceParameters()
{
$id = uniqid();
$this->assertInstanceOf(Bar::class, $bar = make(Bar::class, ['id' => $id]));
$this->assertSame($id, $bar->id);
$this->assertInstanceOf(Demo::class, $bar->demo);
$this->assertSame(1, $bar->demo->getId());
$this->assertNull($bar->name);

$id = uniqid();
$this->assertInstanceOf(Bar::class, $bar = make(Bar::class, [$id, 2 => 'Hyperf']));
$this->assertSame($id, $bar->id);
$this->assertInstanceOf(Demo::class, $bar->demo);
$this->assertSame(1, $bar->demo->getId());
$this->assertSame('Hyperf', $bar->name);
}
}
29 changes: 29 additions & 0 deletions src/di/tests/Stub/Bar.php
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/

namespace HyperfTest\Di\Stub;

class Bar
{
public $demo;

public $id;

public $name;

public function __construct(string $id, Demo $demo, $name = null)
{
$this->demo = $demo;
$this->id = $id;
$this->name = $name;
}
}
21 changes: 21 additions & 0 deletions src/di/tests/Stub/Demo.php
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/

namespace HyperfTest\Di\Stub;

class Demo
{
public function getId()
{
return 1;
}
}

0 comments on commit e60cd3e

Please sign in to comment.