Completed
Push — feature/EVO-7007-selfversion-w... ( eb9b6b...73d775 )
by
unknown
11:44
created

GenerateVersionsCommandTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 6
dl 0
loc 53
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B testGenerateVersions() 0 45 5
1
<?php
2
/** GenerateVersionsCommandTest **/
3
4
namespace Graviton\CoreBundle\Tests\Command;
5
6
use Graviton\CoreBundle\Command\GenerateVersionsCommand;
7
use Graviton\TestBundle\Test\GravitonTestCase;
8
use Symfony\Bundle\FrameworkBundle\Console\Application;
9
use Symfony\Component\Console\Tester\CommandTester;
10
use Symfony\Component\Yaml\Parser;
11
12
/**
13
 * GenerateVersionsCommandTest
14
 *
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @link     http://swisscom.ch
18
 */
19
class GenerateVersionsCommandTest extends \PHPUnit_Framework_TestCase
20
{
21
    /**
22
     * Tests the execution of the command
23
     *
24
     * @return void
25
     */
26
    public function testGenerateVersions()
27
    {
28
        $kernel = GravitonTestCase::createKernel();
29
        $application = new Application($kernel);
30
31
        $application->add(new GenerateVersionsCommand());
32
33
        $command = $application->find('graviton:core:generateversions');
34
35
        $commandTester = new CommandTester($command);
36
        $commandTester->execute(array());
37
38
        $rootDir = $kernel->getContainer()->getParameter("kernel.root_dir");
39
        $contextDir = $rootDir;
40
        // are we in Wrapper context?
41
        if (strstr($contextDir, 'vendor')) {
42
            $contextDir.='/../../../../app';
43
        }
44
45
        $parser = new Parser();
46
        $config = $parser->parse(file_get_contents($contextDir.'/config/version_service.yml'));
47
        $versions = $parser->parse(file_get_contents($rootDir.'/../versions.yml'));
48
49
        $deliveredVersions = [];
50
        foreach ($versions as $version) {
0 ignored issues
show
Bug introduced by
The expression $versions of type array|string|object<stdClass> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
51
            $deliveredVersions[$version['id']] = $version['version'];
52
        };
53
54
        // check if all keys are in the resulting array (canonical, because the sorting doesnt matter)
55
        $this->assertEquals(
56
            $config['desiredVersions'],
57
            array_keys($deliveredVersions),
58
            "canonical = true",
59
            0.0,
60
            10,
61
            true
62
        );
63
64
        // check if the version contains at least 4 chars (i.e. minimum dev-)
65
        foreach ($config['desiredVersions'] as $desiredVersion) {
66
            if (isset($deliveredVersions[$desiredVersion])) {
67
                $this->assertTrue(strlen($deliveredVersions[$desiredVersion])>3);
0 ignored issues
show
introduced by
Instead of assertTrue() use $this->assertGreaterThan...ions[$desiredVersion])). This will lead to a better error message when the test fails.
Loading history...
68
            }
69
        }
70
    }
71
}
72