|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This is part of the webuni/composer-yaml-plugin package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Martin Hasoň <[email protected]> |
|
7
|
|
|
* (c) Webuni s.r.o. <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Webuni\ComposerYamlPlugin\Tests; |
|
14
|
|
|
|
|
15
|
|
|
use Symfony\Component\Console\Application; |
|
16
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
|
17
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
|
18
|
|
|
use Webuni\ComposerYamlPlugin\YamlConvertCommand; |
|
19
|
|
|
|
|
20
|
|
|
final class YamlConvertCommandTest extends \PHPUnit_Framework_TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
private $app; |
|
23
|
|
|
private $buffer; |
|
24
|
|
|
private $cwd; |
|
25
|
|
|
|
|
26
|
|
|
protected function setUp() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->app = new Application(); |
|
29
|
|
|
$this->app->setAutoExit(false); |
|
30
|
|
|
$this->app->add(new YamlConvertCommand()); |
|
31
|
|
|
$this->buffer = new BufferedOutput(); |
|
32
|
|
|
|
|
33
|
|
|
$this->cwd = getcwd(); |
|
34
|
|
|
chdir(sys_get_temp_dir()); |
|
35
|
|
|
$this->removeFiles(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function tearDown() |
|
39
|
|
|
{ |
|
40
|
|
|
chdir($this->cwd); |
|
41
|
|
|
$this->removeFiles(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testDefaultInputFileDoesNotExists() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->app->run(new ArrayInput(['yaml-convert']), $this->buffer); |
|
47
|
|
|
$this->assertContains('The input file "composer.yml" does not exist.', $this->buffer->fetch()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testCustomInputFileDoesNotExists() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->app->run(new ArrayInput(['yaml-convert', 'input' => 'composer.json']), $this->buffer); |
|
53
|
|
|
$this->assertContains('The input file "composer.json" does not exist.', $this->buffer->fetch()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testInvalidFormat() |
|
57
|
|
|
{ |
|
58
|
|
|
$this->app->run(new ArrayInput(['yaml-convert', 'input' => 'composer.json']), $this->buffer); |
|
59
|
|
|
$this->assertContains('The input file "composer.json" does not exist.', $this->buffer->fetch()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testSameFormat() |
|
63
|
|
|
{ |
|
64
|
|
|
file_put_contents('composer.yml', 'name: package'); |
|
65
|
|
|
$this->app->run(new ArrayInput(['yaml-convert', 'output' => 'composer.yml']), $this->buffer); |
|
66
|
|
|
$this->assertContains('Input format is same as output format.', $this->buffer->fetch()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testConvertYamlToJson() |
|
70
|
|
|
{ |
|
71
|
|
|
file_put_contents('composer.yml', 'name: package'); |
|
72
|
|
|
$this->app->run(new ArrayInput(['yaml-convert']), $this->buffer); |
|
73
|
|
|
$this->assertContains('Converted "composer.yml" to "composer.json"', $this->buffer->fetch()); |
|
74
|
|
|
$this->assertFileExists('composer.json'); |
|
75
|
|
|
$this->assertEquals("{\n \"name\": \"package\"\n}", file_get_contents('composer.json')); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testConvertJsonToYaml() |
|
79
|
|
|
{ |
|
80
|
|
|
file_put_contents('composer.json', "{\n \"name\": \"package\"\n}"); |
|
81
|
|
|
$this->app->run(new ArrayInput(['yaml-convert', 'input' => 'composer.json', 'output' => 'composer.yml']), $this->buffer); |
|
82
|
|
|
$this->assertContains('Converted "composer.json" to "composer.yml"', $this->buffer->fetch()); |
|
83
|
|
|
$this->assertFileExists('composer.yml'); |
|
84
|
|
|
$this->assertEquals("name: package\n", file_get_contents('composer.yml')); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
private function removeFiles() |
|
88
|
|
|
{ |
|
89
|
|
|
foreach (['json', 'yml'] as $extension) { |
|
90
|
|
|
$file = sys_get_temp_dir().'/composer.'.$extension; |
|
91
|
|
|
if (is_file($file)) { |
|
92
|
|
|
unlink($file); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|