UpdateCommandTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
cbo 3
dl 0
loc 60
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 16 3
A testUpdateDependencies() 0 10 1
A testThrowExceptionWhenPackageIsNotInstalled() 0 8 1
A tearDown() 0 12 4
1
<?php
2
3
namespace Bowerphp\Test\Command;
4
5
use Bowerphp\Factory\CommandFactory;
6
use FilesystemIterator;
7
use PHPUnit\Framework\TestCase;
8
use RecursiveDirectoryIterator;
9
use RecursiveIteratorIterator;
10
11
/**
12
 * @group functional
13
 */
14
class UpdateCommandTest extends TestCase
15
{
16
    private $packageDotBowerFile;
17
18
    private $bowerFile;
19
20
    protected function setUp()
21
    {
22
        $dir = getcwd() . '/bower_components/';
23
        if (!is_dir($dir)) {
24
            mkdir($dir);
25
        }
26
        if (!is_dir($dir . 'jquery')) {
27
            mkdir($dir . 'jquery');
28
        }
29
        touch($dir . 'jquery/.bower.json');
30
        $this->packageDotBowerFile = $dir . 'jquery/.bower.json';
31
        $this->bowerFile = getcwd() . '/bower.json';
32
33
        file_put_contents($this->packageDotBowerFile, '{"name": "jquery", "version": "1.10.1"}');
34
        file_put_contents($this->bowerFile, '{"name": "test", "dependencies": {"jquery": "1.11.1"}}');
35
    }
36
37
    public function testUpdateDependencies()
38
    {
39
        //when
40
        CommandFactory::tester('update', ['package' => 'jquery']);
41
42
        //then
43
        $dotBower = json_decode(file_get_contents($this->packageDotBowerFile), true);
44
        $this->assertEquals('1.11.1', $dotBower['version']);
45
        $this->assertFileExists(getcwd() . '/bower_components/jquery/src/jquery.js');
46
    }
47
48
    /**
49
     * @expectedException \RuntimeException
50
     * @expectedExceptionMessage Package nonexistent-package is not installed
51
     */
52
    public function testThrowExceptionWhenPackageIsNotInstalled()
53
    {
54
        //when
55
        $commandTester = CommandFactory::tester('update', ['package' => 'nonexistent-package']);
56
57
        //then
58
        $this->assertRegExp('/Package nonexistent-package is not installed/', $commandTester->getDisplay());
59
    }
60
61
    protected function tearDown()
62
    {
63
        $dir = getcwd() . '/bower_components/';
64
        if (is_dir($dir)) {
65
            // see http://stackoverflow.com/a/15111679/369194
66
            foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $path) {
67
                $path->isFile() ? unlink($path->getPathname()) : rmdir($path->getPathname());
68
            }
69
            rmdir($dir);
70
        }
71
        unlink(getcwd() . '/bower.json');
72
    }
73
}
74