ApplicationTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
cbo 7
dl 0
loc 138
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createApplication() 0 16 1
A testFactory() 0 16 1
B testSetBuildName() 0 28 1
A testGetCommands() 0 12 2
A testFunctionalRunthrough() 0 14 1
A functionalAsserts() 0 23 1
1
<?php
2
3
/**
4
 * This file is part of Bldr.io
5
 *
6
 * (c) Aaron Scherer <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE
10
 */
11
12
namespace Bldr\Test;
13
14
use Bldr\Application;
15
use Symfony\Component\Console\Input\ArgvInput;
16
use Symfony\Component\Console\Output\StreamOutput;
17
18
/**
19
 * @author Aaron Scherer <[email protected]>
20
 */
21
class ApplicationTest extends \PHPUnit_Framework_TestCase
22
{
23
    /**
24
     * @return Application
25
     */
26
    public static function createApplication()
27
    {
28
        $package = \Mockery::mock('Composer\Package\PackageInterface');
29
        $package->shouldReceive('getPrettyVersion')->andReturn('test');
30
31
        $embeddedComposer = \Mockery::mock('Dflydev\EmbeddedComposer\Core\EmbeddedComposerInterface');
32
        $embeddedComposer->shouldReceive('findPackage')->once()->andReturn($package);
33
34
        $config = \Mockery::mock('Composer\Config');
35
        $config->shouldReceive('has')->andReturn(false);
36
37
        $embeddedComposer->shouldReceive('getExternalComposerConfig')->andReturn($config);
38
        $embeddedComposer->shouldReceive('getExternalRootDirectory')->andReturn(__DIR__); // doesn't matter.
39
40
        return Application::create($embeddedComposer);
41
    }
42
43
    /**
44
     * Tests the Application::__construct($name, $version) method
45
     *
46
     * @throws \PHPUnit_Framework_Exception
47
     * @return Application
48
     */
49
    public function testFactory()
50
    {
51
        $application = self::createApplication();
52
53
        $this->assertInstanceOf(
54
            'Bldr\Application',
55
            $application
56
        );
57
58
        $this->assertInstanceOf(
59
            'Symfony\Component\Console\Application',
60
            $application
61
        );
62
63
        return $application;
64
    }
65
66
    /**
67
     *
68
     */
69
    public function testSetBuildName()
70
    {
71
        $container = \Mockery::mock('Bldr\DependencyInjection\ContainerBuilder');
72
        $container->shouldReceive('getParameter')
73
            ->once()
74
            ->withArgs(['name'])
75
            ->andReturn('test-app');
76
77
        $app      = self::createApplication();
78
        $ref      = new \ReflectionClass($app);
79
        $property = $ref->getProperty('container');
80
        $property->setAccessible(true);
81
        $property->setValue($app, $container);
82
83
        $travis          = getenv('TRAVIS');
84
        $travisJobNumber = getenv('TRAVIS_JOB_NUMBER');
85
        putenv('TRAVIS=true');
86
        putenv('TRAVIS_JOB_NUMBER=test');
87
        $app->setBuildName();
88
        $this->assertEquals('travis_test', $app::$BUILD_NAME);
89
        putenv('TRAVIS=false');
90
        $date = date('Y-m-d_H-i-s');
91
        $app->setBuildName();
92
        $this->assertEquals('local_test-app_'.$date, $app::$BUILD_NAME);
93
94
        putenv("TRAVIS={$travis}");
95
        putenv("TRAVIS_JOB_NUMBER={$travisJobNumber}");
96
    }
97
98
    /**
99
     *
100
     */
101
    public function testGetCommands()
102
    {
103
        $app      = self::createApplication();
104
        $commands = $app->getCommands();
105
        $this->assertNotEmpty($commands);
106
        foreach ($commands as $command) {
107
            $this->assertInstanceOf(
108
                'Symfony\Component\Console\Command\Command',
109
                $command
110
            );
111
        }
112
    }
113
114
    /**
115
     * @throws \Exception
116
     */
117
    public function testFunctionalRunthrough()
118
    {
119
        $application = self::createApplication();
120
        $application->setAutoExit(false);
121
        $stream  = 'php://memory';
122
123
        $output = new StreamOutput(fopen($stream, 'w', false));
124
        $application->run(new ArgvInput(['bldr', 'run', 'functionalTest']), $output);
125
        $this->functionalAsserts($output);
126
127
        $output = new StreamOutput(fopen($stream, 'w', false));
128
        $application->run(new ArgvInput(['bldr', 'functionalTest']), $output);
129
        $this->functionalAsserts($output);
130
    }
131
132
    /**
133
     * @param StreamOutput $output
134
     */
135
    private function functionalAsserts(StreamOutput $output)
136
    {
137
        $baseDir = realpath(__DIR__.'/..');
138
        rewind($output->getStream());
139
        $content = stream_get_contents($output->getStream());
140
141
        $this->assertContains('Running the fsTest job > Filesystem Block Tests', $content);
142
143
        $this->assertContains('Creating tmp/', $content);
144
        $this->assertFileExists($baseDir.'/tmp');
145
        $this->assertContains('Creating tmp/test/deep', $content);
146
        $this->assertFileExists($baseDir.'/tmp/test');
147
        $this->assertFileExists($baseDir.'/tmp/test/deep');
148
        $this->assertContains('Touching tmp/test.tmp', $content);
149
        $this->assertFileExists($baseDir.'/tmp/test.tmp');
150
        $this->assertContains('Touching tmp/test/deep/test.tmp', $content);
151
        $this->assertFileExists($baseDir.'/tmp/test/deep/test.tmp');
152
153
        $this->assertContains('Running the lint job > Lints the files of the project', $content);
154
155
        $this->assertContains('Lint Task Finished', $content);
156
        $this->assertContains('Build Success!', $content);
157
    }
158
}
159