Skip to content

Commit 881be81

Browse files
imanghafoori1taylorotwell
authored andcommitted
[5.7] Bug fix for app()->call() (#26852)
* Update BoundMethod.php Bug fix for app()->call('class@method'); fixes: #22687 * Update ContainerTest.php Added tests for app()->call() with default values in the called method * Update ContainerTest.php fixes style-ci issues * fixes the tests Fixes the tests * Fixes the bug caught by tests * more tests to be more sure * Fixes style-CI issue * better tests + typo fix * Passes the most recent test (defaultOnlyC) * More tests covering more cases * Adds more tests Adds more tests * Update ContainerTest.php Fix Style-ci
1 parent 3913c10 commit 881be81

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

src/Illuminate/Container/BoundMethod.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected static function getMethodDependencies($container, $callback, array $pa
116116
static::addDependencyForCallParameter($container, $parameter, $parameters, $dependencies);
117117
}
118118

119-
return array_merge($dependencies, $parameters);
119+
return $parameters + $dependencies;
120120
}
121121

122122
/**
@@ -162,6 +162,8 @@ protected static function addDependencyForCallParameter($container, $parameter,
162162
$dependencies[] = $container->make($parameter->getClass()->name);
163163
} elseif ($parameter->isDefaultValueAvailable()) {
164164
$dependencies[] = $parameter->getDefaultValue();
165+
} else {
166+
$dependencies[] = null;
165167
}
166168
}
167169

tests/Container/ContainerTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,77 @@ public function testCallWithDependencies()
516516
$this->assertEquals('taylor', $result[1]);
517517
}
518518

519+
public function testWithDefaultParametersIndexedArraySyntax()
520+
{
521+
$container = new Container;
522+
$result = $container->call(ContainerTestDefaultyParams::class.'@defaulty', ['foo', 'bar']);
523+
524+
$this->assertEquals(['foo', 'bar', 'default c'], $result);
525+
526+
$container = new Container;
527+
$result = $container->call(ContainerTestDefaultyParams::class.'@defaulty', ['foo', 'bar', 'baz']);
528+
529+
$this->assertEquals(['foo', 'bar', 'baz'], $result);
530+
531+
$container = new Container;
532+
$result = $container->call(ContainerTestDefaultyParams::class.'@defaulty');
533+
534+
$this->assertEquals(['default a', 'default b', 'default c'], $result);
535+
536+
$container = new Container;
537+
$result = $container->call(ContainerTestDefaultyParams::class.'@defaultyBandC', ['foo', 'bar']);
538+
539+
$this->assertEquals(['foo', 'bar', 'default c'], $result);
540+
541+
$container = new Container;
542+
$result = $container->call(ContainerTestDefaultyParams::class.'@defaultyBandC', ['foo']);
543+
544+
$this->assertEquals(['foo', 'default b', 'default c'], $result);
545+
546+
$container = new Container;
547+
$result = $container->call(ContainerTestDefaultyParams::class.'@defaultyOnlyC', ['foo', 'bar']);
548+
549+
$this->assertEquals(['foo', 'bar', 'default c'], $result);
550+
551+
$container = new Container;
552+
$result = $container->call(ContainerTestDefaultyParams::class.'@noDefault', ['foo', 'bar', 'baz']);
553+
554+
$this->assertEquals(['foo', 'bar', 'baz'], $result);
555+
}
556+
557+
public function testWithDefaultParametersAssociativeSyntax()
558+
{
559+
$container = new Container;
560+
$result = $container->call(ContainerTestDefaultyParams::class.'@defaulty', ['a' => 'foo', 'b' => 'bar']);
561+
562+
$this->assertEquals(['foo', 'bar', 'default c'], $result);
563+
564+
$container = new Container;
565+
$result = $container->call(ContainerTestDefaultyParams::class.'@defaulty', ['a' => 'foo', 'b' => 'bar', 'c' => 'baz']);
566+
567+
$this->assertEquals(['foo', 'bar', 'baz'], $result);
568+
569+
$container = new Container;
570+
$result = $container->call(ContainerTestDefaultyParams::class.'@defaultyBandC', ['a' => 'foo', 'b' => 'bar']);
571+
572+
$this->assertEquals(['foo', 'bar', 'default c'], $result);
573+
574+
$container = new Container;
575+
$result = $container->call(ContainerTestDefaultyParams::class.'@defaultyBandC', ['a' => 'foo']);
576+
577+
$this->assertEquals(['foo', 'default b', 'default c'], $result);
578+
579+
$container = new Container;
580+
$result = $container->call(ContainerTestDefaultyParams::class.'@defaultyOnlyC', ['a' => 'foo', 'b' => 'bar']);
581+
582+
$this->assertEquals(['foo', 'bar', 'default c'], $result);
583+
584+
$container = new Container;
585+
$result = $container->call(ContainerTestDefaultyParams::class.'@noDefault', ['a' => 'foo', 'b' => 'bar', 'c' => 'baz']);
586+
587+
$this->assertEquals(['foo', 'bar', 'baz'], $result);
588+
}
589+
519590
/**
520591
* @expectedException \ReflectionException
521592
* @expectedExceptionMessage Function ContainerTestCallStub() does not exist
@@ -1271,3 +1342,26 @@ public function __construct()
12711342
static::$instantiations++;
12721343
}
12731344
}
1345+
1346+
class ContainerTestDefaultyParams
1347+
{
1348+
public function defaulty($a = 'default a', $b = 'default b', $c = 'default c')
1349+
{
1350+
return func_get_args();
1351+
}
1352+
1353+
public function defaultyBandC($a, $b = 'default b', $c = 'default c')
1354+
{
1355+
return func_get_args();
1356+
}
1357+
1358+
public function defaultyOnlyC($a, $b, $c = 'default c')
1359+
{
1360+
return func_get_args();
1361+
}
1362+
1363+
public function noDefault($a, $b, $c)
1364+
{
1365+
return func_get_args();
1366+
}
1367+
}

0 commit comments

Comments
 (0)