Skip to content

Commit fc4d56d

Browse files
authored
Inject and mock yaml dumper (#3164)
1 parent d32f317 commit fc4d56d

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

src/Yaml/Yaml.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
class Yaml
1414
{
1515
protected $file;
16+
protected $yaml;
17+
18+
public function __construct(SymfonyYaml $yaml)
19+
{
20+
$this->yaml = $yaml;
21+
}
1622

1723
public function file($file)
1824
{
@@ -51,7 +57,7 @@ public function parse($str = null)
5157
}
5258

5359
try {
54-
$yaml = SymfonyYaml::parse($str);
60+
$yaml = $this->yaml->parse($str);
5561
} catch (\Exception $e) {
5662
throw $this->viewException($e, $str);
5763
}
@@ -81,7 +87,7 @@ public function dump($data, $content = null)
8187
$data['content'] = $content;
8288
}
8389

84-
return SymfonyYaml::dump($data, 100, 2, SymfonyYaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
90+
return $this->yaml->dump($data, 100, 2, SymfonyYaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
8591
}
8692

8793
/**

tests/Yaml/YamlTest.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Exception;
66
use Statamic\Facades\YAML;
77
use Statamic\Yaml\ParseException;
8+
use Statamic\Yaml\Yaml as StatamicYaml;
9+
use Symfony\Component\Yaml\Yaml as SymfonyYaml;
810
use Tests\TestCase;
911

1012
class YamlTest extends TestCase
@@ -19,19 +21,16 @@ public function it_dumps_yaml()
1921
'array' => ['one', 'two'],
2022
];
2123

22-
$expected = <<<'EOT'
23-
foo: bar
24-
two_words: 'two words'
25-
multiline: |
26-
first
27-
second
28-
array:
29-
- one
30-
- two
24+
$symfonyYaml = $this->mock(SymfonyYaml::class)
25+
->shouldReceive('dump')
26+
->with($array, 100, 2, SymfonyYaml::DUMP_MULTI_LINE_LITERAL_BLOCK)
27+
->once()
28+
->andReturn('some properly dumped yaml from symfony')
29+
->getMock();
3130

32-
EOT;
31+
$this->app->instance(StatamicYaml::class, new StatamicYaml($symfonyYaml));
3332

34-
$this->assertEqualsIgnoringLineEndings($expected, YAML::dump($array));
33+
$this->assertEquals('some properly dumped yaml from symfony', YAML::dump($array));
3534
}
3635

3736
/** @test */

0 commit comments

Comments
 (0)