Completed
Push — master ( 9ad51b...7e7a81 )
by Tim
10s
created

RoboFile::runTestsUnit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/**
4
 * RoboFile.php
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-cli-simple
18
 * @link      http://www.techdivision.com
19
 */
20
21
use Lurker\Event\FilesystemEvent;
22
use Symfony\Component\Finder\Finder;
23
24
/**
25
 * Defines the available build tasks.
26
 *
27
 * @author    Tim Wagner <[email protected]>
28
 * @copyright 2016 TechDivision GmbH <[email protected]>
29
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
30
 * @link      https://github.com/techdivision/import-cli-simple
31
 * @link      http://www.techdivision.com
32
 */
33
class RoboFile extends \Robo\Tasks
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
34
{
35
36
    /**
37
     * The build properties.
38
     *
39
     * @var array
40
     */
41
    protected $properties = array(
42
        'base.dir' => __DIR__,
43
        'etc.dir' => __DIR__ . '/etc',
44
        'src.dir' => __DIR__ . '/src',
45
        'dist.dir' => __DIR__ . '/dist',
46
        'vendor.dir' => __DIR__ . '/vendor',
47
        'target.dir' => __DIR__ . '/target',
48
        'symfony.dir' => __DIR__ . '/symfony',
49
        'webapp.name' => 'import-cli-simple',
50
        'webapp.version' => '1.0.0-beta.56'
51
    );
52
53
    /**
54
     * The Magento versions to run the integration tests against.
55
     *
56
     * @var array
57
     */
58
    protected $magentoVersions = array('ce' => array('2.1.7'));
59
60
    /**
61
     * Run's the composer install command.
62
     *
63
     * @return void
64
     */
65
    public function composerInstall()
66
    {
67
        // optimize autoloader with custom path
68
        $this->taskComposerInstall()
69
             ->preferDist()
70
             ->optimizeAutoloader()
71
             ->run();
72
    }
73
74
    /**
75
     * Run's the composer update command.
76
     *
77
     * @return void
78
     */
79
    public function composerUpdate()
80
    {
81
        // optimize autoloader with custom path
82
        $this->taskComposerUpdate()
83
             ->preferDist()
84
             ->optimizeAutoloader()
85
             ->run();
86
    }
87
88
    /**
89
     * Clean up the environment for a new build.
90
     *
91
     * @return void
92
     */
93
    public function clean()
94
    {
95
        $this->taskDeleteDir($this->properties['target.dir'])->run();
96
    }
97
98
    /**
99
     * Prepare's the environment for a new build.
100
     *
101
     * @return void
102
     */
103
    public function prepare()
104
    {
105
        $this->taskFileSystemStack()
106
             ->mkdir($this->properties['dist.dir'])
107
             ->mkdir($this->properties['target.dir'])
108
             ->mkdir(sprintf('%s/reports', $this->properties['target.dir']))
109
             ->run();
110
    }
111
112
    /**
113
     * Creates the a PHAR archive from the sources.
114
     *
115
     * @return void
116
     */
117
    public function createPhar()
118
    {
119
120
        // run the build process
121
        $this->build();
122
123
        // prepare the PHAR archive name
124
        $archiveName = sprintf(
125
            '%s/%s.phar',
126
            $this->properties['target.dir'],
127
            $this->properties['webapp.name']
128
        );
129
130
        // prepare the target directory
131
        $targetDir = $this->properties['target.dir'] . DIRECTORY_SEPARATOR . $this->properties['webapp.version'];
132
133
        // copy the composer.json file
134
        $this->taskFilesystemStack()
135
             ->copy(
136
                  __DIR__ . DIRECTORY_SEPARATOR . 'composer.json',
137
                  $targetDir. DIRECTORY_SEPARATOR. 'composer.json'
138
             )->run();
139
140
          // copy the .semver file
141
          $this->taskFilesystemStack()
142
               ->copy(
143
                   __DIR__ . DIRECTORY_SEPARATOR . '.semver',
144
                   $targetDir. DIRECTORY_SEPARATOR. '.semver'
145
               )->run();
146
147
          // copy the bootstrap.php file
148
          $this->taskFilesystemStack()
149
               ->copy(
150
                  __DIR__ . DIRECTORY_SEPARATOR . 'bootstrap.php',
151
                  $targetDir. DIRECTORY_SEPARATOR. 'bootstrap.php'
152
               )->run();
153
154
        // copy the src/etc directory
155
        $this->taskCopyDir(
156
                  array(
157
                      $this->properties['src.dir'] => $targetDir . DIRECTORY_SEPARATOR . 'src'
158
                  )
159
               )->run();
160
161
        // copy the syfmony directory
162
        $this->taskCopyDir(
163
                   array(
164
                       $this->properties['symfony.dir'] => $targetDir . DIRECTORY_SEPARATOR . 'symfony'
165
                   )
166
               )->run();
167
168
        // install the composer dependencies
169
        $this->taskComposerInstall()
170
            ->dir($targetDir)
171
            ->noDev()
172
            ->optimizeAutoloader()
173
            ->run();
174
175
        // prepare the task
176
        $pharTask = $this->taskPackPhar($archiveName)
177
            ->compress()
178
            ->stub('stub.php');
179
180
        // load a list with all the source files from the vendor directory
181
        $finder = Finder::create()->files()
182
            ->name('*.php')
183
            ->name('.semver')
184
            ->name('services.xml')
185
            ->name('services-1.0.xsd')
186
            ->name('techdivision-import.json')
187
            ->in($targetDir)
188
            ->ignoreDotFiles(false);
189
190
        // iterate over the source files of the vendor directory and add them to the PHAR archive
191
        foreach ($finder as $file) {
192
            $pharTask->addFile($file->getRelativePathname(), $file->getRealPath());
193
        }
194
195
        // create the PHAR archive
196
        $pharTask->run();
197
198
        // verify PHAR archive is packed correctly
199
        $this->_exec(sprintf('php %s', $archiveName));
200
201
        // prepare the PHAR archive distribution name
202
        $distArchiveName = sprintf('%s/%s.phar', $this->properties['dist.dir'], $this->properties['webapp.name']);
203
204
        // clean up the dist directory
205
        $this->taskCleanDir($this->properties['dist.dir'])->run();
206
207
        // copy the latest PHAR archive to the dist directory
208
        $this->taskFilesystemStack()->copy($archiveName, $distArchiveName)->run();
209
    }
210
211
    /**
212
     * Run's the PHPMD.
213
     *
214
     * @return void
215
     */
216 View Code Duplication
    public function runMd()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
217
    {
218
219
        // run the mess detector
220
        $this->_exec(
221
            sprintf(
222
                '%s/bin/phpmd %s xml phpmd.xml --reportfile %s/reports/pmd.xml --ignore-violations-on-exit',
223
                $this->properties['vendor.dir'],
224
                $this->properties['src.dir'],
225
                $this->properties['target.dir']
226
            )
227
        );
228
    }
229
230
    /**
231
     * Run's the PHPCPD.
232
     *
233
     * @return void
234
     */
235 View Code Duplication
    public function runCpd()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
236
    {
237
238
        // run the copy past detector
239
        $this->_exec(
240
            sprintf(
241
                '%s/bin/phpcpd %s --log-pmd %s/reports/pmd-cpd.xml --names-exclude *Factory.php',
242
                $this->properties['vendor.dir'],
243
                $this->properties['src.dir'],
244
                $this->properties['target.dir']
245
            )
246
        );
247
    }
248
249
    /**
250
     * Run's the PHPCodeSniffer.
251
     *
252
     * @return void
253
     */
254 View Code Duplication
    public function runCs()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
255
    {
256
257
        // run the code sniffer
258
        $this->_exec(
259
            sprintf(
260
                '%s/bin/phpcs -n --report-full --extensions=php --standard=phpcs.xml --report-checkstyle=%s/reports/phpcs.xml %s',
261
                $this->properties['vendor.dir'],
262
                $this->properties['target.dir'],
263
                $this->properties['src.dir']
264
            )
265
        );
266
    }
267
268
    /**
269
     * Run's the PHPUnit testsuite.
270
     *
271
     * @return void
272
     */
273
    public function runTestsUnit()
274
    {
275
276
        // run PHPUnit
277
        $this->taskPHPUnit(sprintf('%s/bin/phpunit --testsuite "techdivision/import-cli-simple PHPUnit testsuite"', $this->properties['vendor.dir']))
278
             ->configFile('phpunit.xml')
279
             ->run();
280
    }
281
282
    /**
283
     * Run's the integration testsuite.
284
     *
285
     * @return void
286
     */
287
    public function runTestsIntegration()
288
    {
289
290
        // start the containers
291
        $this->taskExec(sprintf('docker-compose up -d'))
292
             ->dir('tests')
293
             ->run();
294
295
        // initialize the flag to identify if MySQL is available or not
296
        $mysqlNotAvailable = false;
0 ignored issues
show
Unused Code introduced by
$mysqlNotAvailable is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
297
298
        do {
299
            // try to connect to MySQL
300
            $mysqlNotAvailable = $this->taskDockerExec('mysql')
301
                                      ->interactive()
302
                                      ->exec('mysql -uroot -pappserver.i0 -hmysql -e "exit"')
303
                                      ->run()
304
                                      ->getExitCode();
305
306
            // wait a second if MySQL is NOT available yet
307
            if ($mysqlNotAvailable) {
308
                sleep(1);
309
            }
310
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
311
        } while ($mysqlNotAvailable);
312
313
        // grant the privilieges to connection from outsite the container
314
        $this->taskDockerExec('mysql')
315
             ->interactive()
316
             ->exec('mysql -uroot -pappserver.i0 -hmysql -e \'GRANT ALL ON *.* TO "root"@"%" IDENTIFIED BY "appserver.i0"\'')
317
             ->run();
318
319
        // flush the privileges
320
        $this->taskDockerExec('mysql')
321
             ->interactive()
322
             ->exec('mysql -uroot -pappserver.i0 -hmysql -e "FLUSH PRIVILEGES"')
323
             ->run();
324
325
        // run the integration test suite for the configured editions/versions
326
        foreach ($this->magentoVersions as $edition => $versions) {
327
            foreach ($versions as $version) {
328
                // strip the dots from the version number
329
                $strippedVersion = str_replace('.', '', $version);
330
331
                // create the database for the specific Magento 2 edition/version
332
                $this->taskDockerExec('mysql')
333
                     ->interactive()
334
                     ->exec(sprintf('mysql -uroot -pappserver.i0 -hmysql -e "CREATE DATABASE magento2_%s_%s"', $edition, $strippedVersion))
335
                     ->run();
336
337
                // initialze the Magento 2 instance
338
                $this->taskDockerExec('appserver')
339
                     ->interactive()
340
                     ->exec(
341
                         str_replace(
342
                             array('{edition}', '{version}', '{stripped-version}'),
343
                             array($edition, $version, $strippedVersion),
344
                             'bash -c "mkdir /opt/appserver/webapps/magento2-{edition}-{version} \
345
                                && cd /opt/appserver/webapps/magento2-{edition}-{version} \
346
                                && wget http://apps.appserver.io/magento2/magento2-{edition}-{version}.phar \
347
                                && /opt/appserver/bin/phar.phar extract -f magento2-{edition}-{version}.phar \
348
                                && chmod +x bin/magento \
349
                                && bin/magento setup:install \
350
                                    --db-name=magento2_{edition}_{stripped-version} \
351
                                    --db-host=mysql \
352
                                    --db-user=root \
353
                                    --db-password=appserver.i0 \
354
                                    --admin-lastname=server \
355
                                    --admin-firstname=app \
356
                                    [email protected] \
357
                                    --admin-user=appserver \
358
                                    --admin-password=appserver.i0 \
359
                                && supervisorctl restart appserver"'
360
                         )
361
                     )
362
                     ->run();
363
364
                // run the integration testsuite
365
                $this->taskPHPUnit(sprintf('%s/bin/phpunit --testsuite "techdivision/import-cli-simple PHPUnit integration testsuite"', $this->properties['vendor.dir']))
366
                     ->configFile('phpunit.xml')
367
                     ->run();
368
            }
369
        }
370
371
        // stop the containers
372
        $this->taskExec(sprintf('docker-compose stop'))->dir('tests')->run();
373
374
        // remove the containers
375
        $this->taskExec(sprintf('docker-compose rm -f'))->dir('tests')->run();
376
    }
377
378
    /**
379
     * Raising the semver version number.
380
     *
381
     * @return void
382
     */
383
    public function semver()
384
    {
385
        $this->taskSemVer('.semver')
386
             ->prerelease('beta')
387
             ->run();
388
    }
389
390
    /**
391
     * The complete build process.
392
     *
393
     * @return void
394
     */
395
    public function build()
396
    {
397
        $this->clean();
398
        $this->prepare();
399
        $this->runCs();
400
        $this->runCpd();
401
        $this->runMd();
402
        $this->runTestsUnit();
403
    }
404
}
405