testExecuteWithPackageVersionNotFound()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Bowerphp\Test\Command;
4
5
use Bowerphp\Console\Application;
6
use Bowerphp\Repository\GithubRepository;
7
use FilesystemIterator;
8
use Github\Client;
9
use PHPUnit\Framework\TestCase;
10
use RecursiveDirectoryIterator;
11
use RecursiveIteratorIterator;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Tester\CommandTester;
14
15
/**
16
 * @group functional
17
 */
18
class InstallCommandTest extends TestCase
19
{
20
    public function testExecute()
21
    {
22
        $application = new Application();
23
        $commandTester = new CommandTester($command = $application->get('install'));
24
        $commandTester->execute(['command' => $command->getName(), 'package' => 'jquery'], ['decorated' => false]);
25
26
        $this->assertRegExp('/jquery#/m', $commandTester->getDisplay());
27
        $this->assertFileExists(getcwd() . '/bower_components/jquery/.bower.json');
28
        $this->assertFileExists(getcwd() . '/bower_components/jquery/src/jquery.js');
29
        $this->assertFileNotExists(getcwd() . '/bower.json');
30
    }
31
32
    public function testExecuteAndSave()
33
    {
34
        $application = new Application();
35
        //setup
36
        $commandTester = new CommandTester($command = $application->get('init'));
37
        $commandTester->execute(['command' => $command->getName()], ['interactive' => false, 'decorated' => false]);
38
        //install
39
        $commandTester = new CommandTester($command = $application->get('install'));
40
        $commandTester->execute(['command' => $command->getName(), 'package' => 'jquery', '--save' => true], ['decorated' => false]);
41
42
        //Check that the install worked
43
        $this->assertRegExp('/jquery#/m', $commandTester->getDisplay());
44
        $this->assertFileExists(getcwd() . '/bower_components/jquery/.bower.json');
45
        $this->assertFileExists(getcwd() . '/bower_components/jquery/src/jquery.js');
46
47
        //Check that the save worked
48
        $this->assertFileExists(getcwd() . '/bower.json');
49
        $bowerJsonDependencies = ['jquery' => '*'];
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $bowerJsonDependencies exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
50
        $json = json_decode(file_get_contents(getcwd() . '/bower.json'), true);
51
        $this->assertArrayHasKey('dependencies', $json);
52
        $this->assertEquals($bowerJsonDependencies, $json['dependencies']);
53
    }
54
55
    /**
56
     * We need to make sure that it's possible to save a package even if he has already been installed separetly.
57
     * See https://github.com/Bee-Lab/bowerphp/issues/104
58
     */
59
    public function testExecuteAndThenTestSave()
60
    {
61
        $application = new Application();
62
        //setup
63
        $commandTester = new CommandTester($command = $application->get('init'));
64
        $commandTester->execute(['command' => $command->getName()], ['interactive' => false, 'decorated' => false]);
65
        //install
66
        $commandTester = new CommandTester($command = $application->get('install'));
67
        $commandTester->execute(['command' => $command->getName(), 'package' => 'jquery'], ['decorated' => false]);
68
69
        //Check that the install worked
70
        $this->assertRegExp('/jquery#/m', $commandTester->getDisplay());
71
        $this->assertFileExists(getcwd() . '/bower_components/jquery/.bower.json');
72
        $this->assertFileExists(getcwd() . '/bower_components/jquery/src/jquery.js');
73
74
        //Try to save the package in the bower.json
75
        $commandTester = new CommandTester($command = $application->get('install'));
76
        $commandTester->execute(['command' => $command->getName(), 'package' => 'jquery', '--save' => true], ['decorated' => false]);
77
78
        //Check that the save worked
79
        $this->assertFileExists(getcwd() . '/bower.json');
80
        $bowerJsonDependencies = ['jquery' => '*'];
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $bowerJsonDependencies exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
81
        $json = json_decode(file_get_contents(getcwd() . '/bower.json'), true);
82
        $this->assertArrayHasKey('dependencies', $json);
83
        $this->assertEquals($bowerJsonDependencies, $json['dependencies']);
84
    }
85
86
    public function testExecuteVerbose()
87
    {
88
        $application = new Application();
89
        $commandTester = new CommandTester($command = $application->get('install'));
90
        $commandTester->execute(['command' => $command->getName(), 'package' => 'jquery'], ['decorated' => false, 'verbosity' => OutputInterface::VERBOSITY_DEBUG]);
91
92
        $this->assertRegExp('/jquery#/', $commandTester->getDisplay());
93
        $this->assertFileExists(getcwd() . '/bower_components/jquery/.bower.json');
94
        $this->assertFileExists(getcwd() . '/bower_components/jquery/src/jquery.js');
95
    }
96
97
    public function testExecuteWithoutPackage()
98
    {
99
        $application = new Application();
100
        $commandTester = new CommandTester($command = $application->get('install'));
101
        $commandTester->execute(['command' => $command->getName()], ['decorated' => false]);
102
103
        $this->assertRegExp('/No bower.json found/', $commandTester->getDisplay());
104
    }
105
106
    public function testExecuteWithPackageVersionNotFound()
107
    {
108
        $application = new Application();
109
        $commandTester = new CommandTester($command = $application->get('install'));
110
        $commandTester->execute(['command' => $command->getName(), 'package' => 'jquery#999'], ['decorated' => false]);
111
112
        $this->assertRegExp('/Available versions/', $commandTester->getDisplay());
113
    }
114
115
    public function testExecuteInstallPackageWithDependencies()
116
    {
117
        $application = new Application();
118
        $commandTester = new CommandTester($command = $application->get('install'));
119
        $commandTester->execute(['command' => $command->getName(), 'package' => 'jquery-ui'], ['decorated' => false]);
120
121
        $this->assertRegExp('/jquery#/m', $commandTester->getDisplay());
122
        $this->assertFileExists(getcwd() . '/bower_components/jquery-ui/.bower.json');
123
        $this->assertFileExists(getcwd() . '/bower_components/jquery/.bower.json');
124
    }
125
126
    public function testExecuteInstallFromGithubEndpoint()
127
    {
128
        $application = new Application();
129
        $commandTester = new CommandTester($command = $application->get('install'));
130
        $commandTester->execute(['command' => $command->getName(), 'package' => 'https://github.com/select2/select2.git#3.5.1'], ['decorated' => false]);
131
132
        $this->assertRegExp('/select2#/m', $commandTester->getDisplay());
133
        $this->assertFileExists(getcwd() . '/bower_components/select2/.bower.json');
134
    }
135
136
    public function testExecuteInstallFromLocalFile()
137
    {
138
        $file = getcwd() . '/tests/Bowerphp/Test/bower.json';
139
        file_put_contents($file, '{"name": "test", "dependencies": {"jquery": "1.11.1"}}');
140
141
        $application = new Application();
142
        $commandTester = new CommandTester($command = $application->get('install'));
143
        $commandTester->execute(['command' => $command->getName(), 'package' => $file], ['decorated' => false]);
144
145
        $this->assertRegExp('/jquery#1.11.1/m', $commandTester->getDisplay());
146
        $this->assertFileExists(getcwd() . '/bower_components/jquery/.bower.json');
147
148
        unlink($file);
149
    }
150
151
    public function testExecuteInstallFromLocalUnreadableFile()
152
    {
153
        $application = new Application();
154
        $commandTester = new CommandTester($command = $application->get('install'));
155
        $commandTester->execute(['command' => $command->getName(), 'package' => 'doesnotexist/bower.json'], ['decorated' => false]);
156
157
        $this->assertRegExp('/Cannot read/m', $commandTester->getDisplay());
158
    }
159
160
    public function testExecuteInstallFromLocalFileWithoutDependencies()
161
    {
162
        $file = getcwd() . '/tests/Bowerphp/Test/bower.json';
163
        file_put_contents($file, '{"name": "test"}');
164
165
        $application = new Application();
166
        $commandTester = new CommandTester($command = $application->get('install'));
167
        $commandTester->execute(['command' => $command->getName(), 'package' => $file], ['decorated' => false]);
168
169
        $this->assertRegExp('/Nothing to install/m', $commandTester->getDisplay());
170
171
        unlink($file);
172
    }
173
174
    /**
175
     * This test is probably not ideal in that it will only work (meaning break if the code behavior change) as long
176
     * as the jquery-address package has no tag.
177
     * The first assertion is designed to make sure that the test is still valid and does it's job.
178
     */
179
    public function testExecuteInstallWithoutTag()
180
    {
181
        $client = new Client();
182
        $token = getenv('BOWERPHP_TOKEN');
183
        if (!empty($token)) {
184
            $client->authenticate($token, null, Client::AUTH_HTTP_TOKEN);
185
        }
186
        $githubRepo = new GithubRepository();
187
        $githubRepo->setUrl('https://github.com/asual/jquery-address');
188
        $githubRepo->setHttpClient($client);
189
        //The test only make sense if the library has no git tags.
190
        $this->assertEquals([], $githubRepo->getTags());
191
192
        $application = new Application();
193
        $commandTester = new CommandTester($command = $application->get('install'));
194
        $commandTester->execute(['command' => $command->getName(), 'package' => 'jquery-address'], ['decorated' => false]);
195
196
        $this->assertRegExp('/jquery-address#master/m', $commandTester->getDisplay());
197
        $this->assertFileExists(getcwd() . '/bower_components/jquery-address/.bower.json');
198
    }
199
200
    /**
201
     * See https://github.com/Bee-Lab/bowerphp/issues/119
202
     */
203
    public function testExecuteInstallWithAllIgnores()
204
    {
205
        $application = new Application();
206
        $commandTester = new CommandTester($command = $application->get('install'));
207
        $commandTester->execute(['command' => $command->getName(), 'package' => 'blueimp-tmpl'], ['decorated' => false]);
208
209
        $this->assertRegExp('/blueimp-tmpl#/m', $commandTester->getDisplay());
210
        $this->assertFileExists(getcwd() . '/bower_components/blueimp-tmpl/js/tmpl.js');
211
    }
212
213
    protected function tearDown()
214
    {
215
        $dir = getcwd() . '/bower_components/';
216
        if (is_dir($dir)) {
217
            // see http://stackoverflow.com/a/15111679/369194
218
            foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $path) {
219
                $path->isFile() ? unlink($path->getPathname()) : rmdir($path->getPathname());
220
            }
221
            rmdir($dir);
222
        }
223
        if (file_exists(getcwd() . '/bower.json')) {
224
            unlink(getcwd() . '/bower.json');
225
        }
226
    }
227
}
228