|
3 | 3 | namespace Illuminate\Tests\Container;
|
4 | 4 |
|
5 | 5 | use Illuminate\Container\Container;
|
| 6 | +use Illuminate\Config\Repository; |
6 | 7 | use PHPUnit\Framework\TestCase;
|
7 | 8 |
|
8 | 9 | class ContextualBindingTest extends TestCase
|
@@ -359,6 +360,127 @@ public function testContextualBindingGivesTagsForVariadic()
|
359 | 360 | $this->assertInstanceOf(ContainerContextImplementationStub::class, $resolvedInstance->stubs[0]);
|
360 | 361 | $this->assertInstanceOf(ContainerContextImplementationStubTwo::class, $resolvedInstance->stubs[1]);
|
361 | 362 | }
|
| 363 | + |
| 364 | + public function testContextualBindingGivesValuesFromConfigOptionalValueNull() |
| 365 | + { |
| 366 | + $container = new Container; |
| 367 | + |
| 368 | + $container->singleton('config', function () { |
| 369 | + return new Repository([ |
| 370 | + 'test' => [ |
| 371 | + 'username' => 'laravel', |
| 372 | + 'password' => 'hunter42', |
| 373 | + ] |
| 374 | + ]); |
| 375 | + }); |
| 376 | + |
| 377 | + $container |
| 378 | + ->when(ContainerTestContextInjectFromConfigIndividualValues::class) |
| 379 | + ->needs('$username') |
| 380 | + ->giveConfig('test.username'); |
| 381 | + |
| 382 | + $container |
| 383 | + ->when(ContainerTestContextInjectFromConfigIndividualValues::class) |
| 384 | + ->needs('$password') |
| 385 | + ->giveConfig('test.password'); |
| 386 | + |
| 387 | + $resolvedInstance = $container->make(ContainerTestContextInjectFromConfigIndividualValues::class); |
| 388 | + |
| 389 | + $this->assertEquals('laravel', $resolvedInstance->username); |
| 390 | + $this->assertEquals('hunter42', $resolvedInstance->password); |
| 391 | + $this->assertNull($resolvedInstance->alias); |
| 392 | + } |
| 393 | + |
| 394 | + public function testContextualBindingGivesValuesFromConfigOptionalValueSet() |
| 395 | + { |
| 396 | + $container = new Container; |
| 397 | + |
| 398 | + $container->singleton('config', function () { |
| 399 | + return new Repository([ |
| 400 | + 'test' => [ |
| 401 | + 'username' => 'laravel', |
| 402 | + 'password' => 'hunter42', |
| 403 | + 'alias' => 'lumen', |
| 404 | + ] |
| 405 | + ]); |
| 406 | + }); |
| 407 | + |
| 408 | + $container |
| 409 | + ->when(ContainerTestContextInjectFromConfigIndividualValues::class) |
| 410 | + ->needs('$username') |
| 411 | + ->giveConfig('test.username'); |
| 412 | + |
| 413 | + $container |
| 414 | + ->when(ContainerTestContextInjectFromConfigIndividualValues::class) |
| 415 | + ->needs('$password') |
| 416 | + ->giveConfig('test.password'); |
| 417 | + |
| 418 | + $container |
| 419 | + ->when(ContainerTestContextInjectFromConfigIndividualValues::class) |
| 420 | + ->needs('$alias') |
| 421 | + ->giveConfig('test.alias'); |
| 422 | + |
| 423 | + $resolvedInstance = $container->make(ContainerTestContextInjectFromConfigIndividualValues::class); |
| 424 | + |
| 425 | + $this->assertEquals('laravel', $resolvedInstance->username); |
| 426 | + $this->assertEquals('hunter42', $resolvedInstance->password); |
| 427 | + $this->assertEquals('lumen', $resolvedInstance->alias); |
| 428 | + } |
| 429 | + |
| 430 | + public function testContextualBindingGivesValuesFromConfigWithDefault() |
| 431 | + { |
| 432 | + $container = new Container; |
| 433 | + |
| 434 | + $container->singleton('config', function () { |
| 435 | + return new Repository([ |
| 436 | + 'test' => [ |
| 437 | + 'password' => 'hunter42', |
| 438 | + ] |
| 439 | + ]); |
| 440 | + }); |
| 441 | + |
| 442 | + $container |
| 443 | + ->when(ContainerTestContextInjectFromConfigIndividualValues::class) |
| 444 | + ->needs('$username') |
| 445 | + ->giveConfig('test.username', 'DEFAULT_USERNAME'); |
| 446 | + |
| 447 | + $container |
| 448 | + ->when(ContainerTestContextInjectFromConfigIndividualValues::class) |
| 449 | + ->needs('$password') |
| 450 | + ->giveConfig('test.password'); |
| 451 | + |
| 452 | + $resolvedInstance = $container->make(ContainerTestContextInjectFromConfigIndividualValues::class); |
| 453 | + |
| 454 | + $this->assertEquals('DEFAULT_USERNAME', $resolvedInstance->username); |
| 455 | + $this->assertEquals('hunter42', $resolvedInstance->password); |
| 456 | + $this->assertNull($resolvedInstance->alias); |
| 457 | + } |
| 458 | + |
| 459 | + public function testContextualBindingGivesValuesFromConfigArray() |
| 460 | + { |
| 461 | + $container = new Container; |
| 462 | + |
| 463 | + $container->singleton('config', function () { |
| 464 | + return new Repository([ |
| 465 | + 'test' => [ |
| 466 | + 'username' => 'laravel', |
| 467 | + 'password' => 'hunter42', |
| 468 | + 'alias' => 'lumen', |
| 469 | + ] |
| 470 | + ]); |
| 471 | + }); |
| 472 | + |
| 473 | + $container |
| 474 | + ->when(ContainerTestContextInjectFromConfigArray::class) |
| 475 | + ->needs('$settings') |
| 476 | + ->giveConfig('test'); |
| 477 | + |
| 478 | + $resolvedInstance = $container->make(ContainerTestContextInjectFromConfigArray::class); |
| 479 | + |
| 480 | + $this->assertEquals('laravel', $resolvedInstance->settings['username']); |
| 481 | + $this->assertEquals('hunter42', $resolvedInstance->settings['password']); |
| 482 | + $this->assertEquals('lumen', $resolvedInstance->settings['alias']); |
| 483 | + } |
362 | 484 | }
|
363 | 485 |
|
364 | 486 | interface IContainerContextContractStub
|
@@ -474,3 +596,27 @@ public function __construct(ContainerContextNonContractStub $other, IContainerCo
|
474 | 596 | $this->stubs = $stubs;
|
475 | 597 | }
|
476 | 598 | }
|
| 599 | + |
| 600 | +class ContainerTestContextInjectFromConfigIndividualValues |
| 601 | +{ |
| 602 | + public $username; |
| 603 | + public $password; |
| 604 | + public $alias = null; |
| 605 | + |
| 606 | + public function __construct($username, $password, $alias = null) |
| 607 | + { |
| 608 | + $this->username = $username; |
| 609 | + $this->password = $password; |
| 610 | + $this->alias = $alias; |
| 611 | + } |
| 612 | +} |
| 613 | + |
| 614 | +class ContainerTestContextInjectFromConfigArray |
| 615 | +{ |
| 616 | + public $settings; |
| 617 | + |
| 618 | + public function __construct($settings) |
| 619 | + { |
| 620 | + $this->settings = $settings; |
| 621 | + } |
| 622 | +} |
0 commit comments