public function testMergingBehaviour() { $config = new Config(); $config->import(new TestSingleValueDirectiveGroup('first', 'other value')); $this->assertCount(1, $config); $this->assertTrue($config->has(TestSingleValueDirectiveGroup::class . '.first')); $this->assertEquals('other value', $config->get(TestSingleValueDirectiveGroup::class . '.first')); $config->import(new TestSingleValueDirectiveGroup('second', 'stacked value')); $this->assertCount(2, $config); $this->assertTrue($config->has(TestSingleValueDirectiveGroup::class . '.second')); $this->assertEquals('stacked value', $config->get(TestSingleValueDirectiveGroup::class . '.second')); // override previously imported value (default behaviour for non-array values) $config->import(new TestSingleValueDirectiveGroup('second', 'overwriting value')); $this->assertCount(2, $config); $this->assertTrue($config->has(TestSingleValueDirectiveGroup::class . '.second')); // next import is ignored because overwriting ability has been denied to hte directive $config->import((new TestSingleValueDirectiveGroup('second', 'over overwriting value'))->setMergePolicy(MergePolicy::SKIP)); $this->assertEquals('overwriting value', $config->get(TestSingleValueDirectiveGroup::class . '.second')); // check that a Multiple Directive full content can be retrieved as subset $this->assertEquals(['first' => 'other value', 'second' => 'overwriting value'], $config->subset(TestSingleValueDirectiveGroup::class)->toArray()); // test default merging behaviour for array values $config->import(new TestSingleValueDirectiveGroup('third', ['x' => 'y', 'z'])); $config->import(new TestSingleValueDirectiveGroup('third', ['x' => 'a', 'b'])); $config->import(new TestSingleValueDirectiveGroup('third', ['c' => 'd'])); $config->import(new TestSingleValueDirectiveGroup('third', 'e')); $this->assertEquals(['x' => 'a', 'z', 'b', 'c' => 'd', 'e'], $config->get(TestSingleValueDirectiveGroup::class . '.third')); }
public function testServicesAreRegistered() { $config = new Config(); $config->import(new BeanstalkServer('default', ['host' => '127.0.0.1', 'tube' => 'test'])); $config->import(new BeanstalkServer('other', ['host' => '127.0.0.1', 'port' => 1234, 'tube' => 'test'])); $servicesFactory = $this->createMock(ServicesFactory::class); $servicesFactory->expects($this->exactly(2))->method('registerService')->with($this->isInstanceOf(ClassServiceSpecs::class)); $app = $this->createMock(ApplicationInterface::class); $app->method('getConfig')->willReturn($config); $app->method('getServicesFactory')->willReturn($servicesFactory); (new BeanstalkPackage())->registerServices($app); }
/** * Extract a configuration subset * * This will return a new Config object, only containing values whose identifiers match * the given filter. * * @note Identifiers in the subset are cleaned up so that the filter part is removed. * This is one of the reasons why the '.*' pattern is automatically added to the filter. * This behaviour does not actually prevent from passing a full Matcher compatible pattern, * but discourages it. * * @param $filter * * @return Config */ public function subset($filter) { $filterLength = strlen($filter) + 1; // + 1 for the '.' following the prefix // normalize filter $filter .= '.*'; $subset = new Config(); foreach ($this as $key => $value) { if ($this->getMatcher()->match($filter, $key)) { $subset->set(substr($key, $filterLength), $value); } } return $subset; }
public function testOverwritingBehaviour() { $config = new Config(); $config->import(new TestStackedValuesDirective('other value')); $this->assertCount(1, $config); $this->assertTrue($config->has(TestStackedValuesDirective::class)); $this->assertEquals(['other value'], $config->get(TestStackedValuesDirective::class)); // this import will stack the second value, because stacking is the default behaviour for StackedValuesDirective $config->import(new TestStackedValuesDirective('stacked value')); $this->assertCount(1, $config); $this->assertTrue($config->has(TestStackedValuesDirective::class)); $this->assertEquals(['other value', 'stacked value'], $config->get(TestStackedValuesDirective::class)); // next import is ignored because overwriting ability has been denied to the directive $config->import((new TestStackedValuesDirective('overwriting value'))->setMergePolicy(MergePolicy::REPLACE)); $this->assertCount(1, $config); $this->assertTrue($config->has(TestStackedValuesDirective::class)); $this->assertEquals(['overwriting value'], $config->get(TestStackedValuesDirective::class)); }
public function testMergingBehaviour() { $config = new Config(); $config->import(new TestSingleValueDirective('other value')); $this->assertCount(1, $config); $this->assertTrue($config->has(TestSingleValueDirective::class)); $this->assertEquals('other value', $config->get(TestSingleValueDirective::class)); // this import will override previous one, because overwriting is allowed by default // on scalar directives $config->import(new TestSingleValueDirective('overwriting value')); // next import is ignored because overwriting ability has been denied to hte directive $config->import((new TestSingleValueDirective('ignored value'))->setMergePolicy(MergePolicy::SKIP)); $this->assertCount(1, $config); $this->assertTrue($config->has(TestSingleValueDirective::class)); $this->assertEquals('overwriting value', $config->get(TestSingleValueDirective::class)); // next import is ignored because overwriting ability has been denied to hte directive $config->import((new TestSingleValueDirective('combined value'))->setMergePolicy(MergePolicy::COMBINE)); $this->assertEquals(['overwriting value', 'combined value'], $config->get(TestSingleValueDirective::class)); }
public function testDirectiveGroupFetchingUsingMatcher() { $config = new Config(); $config->set('x.y', 'a')->set('x.z', 'b'); $this->assertEquals(['y' => 'a', 'z' => 'b'], $config->subset('x')->toArray()); }