1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
<?php
declare(strict_types=1);
namespace DI\Test\IntegrationTest;
use DI\ContainerBuilder;
use function DI\create;
use function DI\get;
use function DI\value;
/**
* Tests the set() method from the container.
*/
class ContainerSetTest extends BaseContainerTest
{
/**
* We should be able to set a null value.
* @see https://github.com/mnapoli/PHP-DI/issues/79
* @dataProvider provideContainer
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
public function testSetNullValue(ContainerBuilder $builder)
{
$container = $builder->build();
$container->set('foo', null);
$this->assertNull($container->get('foo'));
}
/**
* @see https://github.com/mnapoli/PHP-DI/issues/126
* @dataProvider provideContainer
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
public function testSetGetSetGet(ContainerBuilder $builder)
{
$container = $builder->build();
$container->set('foo', 'bar');
$container->get('foo');
$container->set('foo', 'hello');
$this->assertSame('hello', $container->get('foo'));
}
/**
* @test
* @dataProvider provideContainer
*/
#[\PHPUnit\Framework\Attributes\Test]
#[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
public function array_entries_can_be_overridden_by_values(ContainerBuilder $builder)
{
$builder->addDefinitions([
'foo' => 'bar',
]);
$container = $builder->build();
$container->set('foo', 'hello');
$this->assertSame('hello', $container->get('foo'));
}
/**
* @see https://github.com/PHP-DI/PHP-DI/issues/674
* @test
* @dataProvider provideContainer
*/
#[\PHPUnit\Framework\Attributes\Test]
#[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
public function value_definitions_are_interpreted_as_raw_values(ContainerBuilder $builder)
{
$container = $builder->build();
$foo = 'foo';
$bar = new ContainerSetTest\Dummy();
$baz = function() {};
$container->set('foo', value($foo));
$container->set('bar', value($bar));
$container->set('baz', value($baz));
$this->assertSame($foo, $container->get('foo'));
$this->assertSame($bar, $container->get('bar'));
$this->assertSame($baz, $container->get('baz'));
}
/**
* @test
* @dataProvider provideContainer
*/
#[\PHPUnit\Framework\Attributes\Test]
#[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
public function array_entries_can_be_overridden_by_definitions(ContainerBuilder $builder)
{
if ($builder->isCompilationEnabled()) {
// This behavior is not allowed on the compiled container
return;
}
$builder->addDefinitions([
'foo' => create(\stdClass::class),
]);
$container = $builder->build();
$container->set('foo', create(ContainerSetTest\Dummy::class));
$this->assertInstanceOf(ContainerSetTest\Dummy::class, $container->get('foo'));
}
/**
* @see https://github.com/PHP-DI/PHP-DI/issues/614
* @test
* @dataProvider provideContainer
*/
#[\PHPUnit\Framework\Attributes\Test]
#[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
public function interfaces_can_be_mapped_to_implementations(ContainerBuilder $builder)
{
if ($builder->isCompilationEnabled()) {
// This behavior is not allowed on the compiled container
return;
}
$container = $builder->build();
$container->set(ContainerSetTest\DummyInterface::class, get(ContainerSetTest\DummyConcrete::class));
$this->assertInstanceOf(ContainerSetTest\DummyImplementation::class, $container->get(ContainerSetTest\DummyImplementation::class));
}
}
namespace DI\Test\IntegrationTest\ContainerSetTest;
class Dummy
{
}
interface DummyInterface
{
}
class DummyConcrete implements DummyInterface
{
}
class DummyImplementation
{
function __construct(DummyInterface $a)
{
}
}
|