Completed
Push — master ( 91b512...bab6b4 )
by Martin
13:02 queued 07:59
created

YamlConvertCommandTest::testConvertJsonToYaml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
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 $output;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
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->output = new BufferedOutput();
32
33
        $this->cwd = getcwd();
34
        chdir(sys_get_temp_dir());
35
        @unlink('composer.json');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
36
        @unlink('composer.yml');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
37
    }
38
39
    protected function tearDown()
40
    {
41
        chdir(sys_get_temp_dir());
42
        @unlink('composer.json');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
43
        @unlink('composer.yml');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
44
        chdir($this->cwd);
45
    }
46
47
    public function testDefaultInputFileDoesNotExists()
48
    {
49
        $this->app->run(new ArrayInput(['yaml-convert']), $this->output);
50
        $this->assertContains('The input file "composer.yml" does not exist.', $this->output->fetch());
51
    }
52
53
    public function testCustomInputFileDoesNotExists()
54
    {
55
        $this->app->run(new ArrayInput(['yaml-convert', 'input' => 'composer.json']), $this->output);
56
        $this->assertContains('The input file "composer.json" does not exist.', $this->output->fetch());
57
    }
58
59
    public function testInvalidFormat()
60
    {
61
        $this->app->run(new ArrayInput(['yaml-convert', 'input' => 'composer.json']), $this->output);
62
        $this->assertContains('The input file "composer.json" does not exist.', $this->output->fetch());
63
    }
64
65
    public function testSameFormat()
66
    {
67
        file_put_contents('composer.yml', 'name: package');
68
        $this->app->run(new ArrayInput(['yaml-convert', 'output' => 'composer.yml']), $this->output);
69
        $this->assertContains('Input format is same as output format.', $this->output->fetch());
70
    }
71
72
    public function testConvertYamlToJson()
73
    {
74
        file_put_contents('composer.yml', 'name: package');
75
        $this->app->run(new ArrayInput(['yaml-convert']), $this->output);
76
        $this->assertContains('Converted "composer.yml" to "composer.json"', $this->output->fetch());
77
        $this->assertFileExists('composer.json');
78
        $this->assertEquals("{\n    \"name\": \"package\"\n}", file_get_contents('composer.json'));
79
    }
80
81
    public function testConvertJsonToYaml()
82
    {
83
        file_put_contents('composer.json', "{\n    \"name\": \"package\"\n}");
84
        $this->app->run(new ArrayInput(['yaml-convert', 'input' => 'composer.json', 'output' => 'composer.yml']), $this->output);
85
        $this->assertContains('Converted "composer.json" to "composer.yml"', $this->output->fetch());
86
        $this->assertFileExists('composer.yml');
87
        $this->assertEquals("name: package\n", file_get_contents('composer.yml'));
88
    }
89
}
90