/** * @param string $fileName * @return Container * @throws AyamlBasePathNotFoundException */ public static function file($fileName) { if (empty(self::$basePaths)) { throw new AyamlBasePathNotFoundException(); } $yamlData = YamlData::load(self::$basePaths, $fileName); return new Container($yamlData); }
<?php use Ayaml\Fixture\YamlData; use Ayaml\Container; describe('\\Ayaml\\Container', function () { beforeEach(function () { $yamlData = YamlData::load([__DIR__ . '/../SampleYaml'], 'user'); $this->subject = new Container($yamlData); }); context('normal case', function () { it('should return correct array', function () { $actual = $this->subject->schema('valid_user')->dump(); $expected = ['id' => 1, 'name' => 'Taro', 'created' => '2014-01-01 00:00:00']; expect($actual)->to->equal($expected); }); context('calling "with"', function () { it('should return overwritten array', function () { $actual = $this->subject->schema('valid_user')->with(['name' => 'John'])->dump(); $expected = ['id' => 1, 'name' => 'John', 'created' => '2014-01-01 00:00:00']; expect($actual)->to->equal($expected); }); }); }); context('abnormal case', function () { it('should throw when you call methods with invalid order', function () { expect(function () { $this->subject->with(['name' => 'John'])->dump(); })->to->throw('\\Ayaml\\AyamlSchemaNotSpecifiedException'); }); it('should throw when you update no existing key', function () { expect(function () {
/** * @param string $name * @return $this * @throws AyamlSchemaNotFoundException */ public function schema($name) { $this->resultData = $this->yamlData->getSchema($name); return $this; }
beforeEach(function () { $data = SymfonyYaml::parse(file_get_contents(__DIR__ . '/../../SampleYaml/user.yml', SymfonyYaml::PARSE_EXCEPTION_ON_INVALID_TYPE)); $this->yamlData = new YamlData($data); }); it('should get schema correctly', function () { $validUser = $this->yamlData->getSchema('valid_user'); $expected = ['id' => 1, 'name' => 'Taro', 'created' => '2014-01-01 00:00:00']; expect($validUser)->to->equal($expected); }); it('should get nested data', function () { $nested = $this->yamlData->getSchema('nested.1.2.3.4.data'); $expected = ['id' => 2, 'name' => 'Jiro', 'created' => '2014-01-01 00:00:00']; expect($nested)->to->equal($expected); }); }); context('abnormal case', function () { it('should throw when file not found', function () { expect(function () { YamlData::load(['invalid path'], 'invalid file'); })->to->throw('\\Ayaml\\Fixture\\AyamlFixtureFileNotFoundException'); }); it('should throw when schema not found', function () { expect(function () { $data = SymfonyYaml::parse(file_get_contents(__DIR__ . '/../../SampleYaml/user.yml', SymfonyYaml::PARSE_EXCEPTION_ON_INVALID_TYPE)); $yamlData = new YamlData($data); $yamlData->getSchema('no existing schema'); })->to->throw('\\Ayaml\\Fixture\\AyamlSchemaNotFoundException'); }); }); }); });