Completed
Pull Request — master (#552)
by Greg
03:27
created

RoboFileFixture::testSimpleList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Robo;
4
5
use Psr\Log\LoggerAwareTrait;
6
use Psr\Log\LoggerAwareInterface;
7
8
use Consolidation\AnnotatedCommand\Events\CustomEventAwareInterface;
9
use Consolidation\AnnotatedCommand\Events\CustomEventAwareTrait;
10
use Consolidation\OutputFormatters\StructuredData\PropertyList;
11
use Robo\Contract\VerbosityThresholdInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * RoboFile under test: a fixture containing some commands to use with tests.
16
 */
17
class RoboFileFixture extends \Robo\Tasks implements LoggerAwareInterface, CustomEventAwareInterface
18
{
19
    use LoggerAwareTrait;
20
    use CustomEventAwareTrait;
21
22
    /**
23
     * Demonstrate Robo variable argument passing.
24
     *
25
     * @param $a A list of commandline parameters.
26
     */
27
    public function testArrayArgs(array $a)
28
    {
29
        $this->say("The parameters passed are:\n" . var_export($a, true));
30
    }
31
32
    /**
33
     * Demonstrate use of SymfonyStyle
34
     */
35
    public function testSymfonyStyle()
36
    {
37
        $this->io()->title('My Title');
38
        $this->io()->section('Section 1');
39
        $this->io()->text('Some text in section one.');
40
        $this->io()->comment('This is just an example of different styles.');
41
        $this->io()->section('Section 2');
42
        $this->io()->text('Some text in section two.');
43
    }
44
45
    /**
46
     * @hook command-event test:command-event
47
     */
48
    public function hookCommandEvent()
49
    {
50
        $this->io()->text('This is the command-event hook for the test:command-event command.');
51
    }
52
53
    public function testCommandEvent()
54
    {
55
        $this->io()->text('This is the main method for the test:command-event command.');
56
    }
57
58
    /**
59
     * @hook post-command test:command-event
60
     */
61
    public function hookPostCommand()
62
    {
63
        $this->io()->text('This is the post-command hook for the test:command-event command.');
64
    }
65
66
    /**
67
     * This command uses a custom event 'custom-event' to collect data.  Note that
68
     * the event handlers will not be found unless the hook manager is
69
     * injected into this command handler object via `setHookManager()`
70
     * (defined in CustomEventAwareTrait). The Robo DI container does this
71
     * for us through inflection.
72
     *
73
     * @command test:custom-event
74
     */
75
    public function testCustomEvent()
76
    {
77
        $myEventHandlers = $this->getCustomEventHandlers('custom-event');
78
        $result = [];
79
        foreach ($myEventHandlers as $handler) {
80
            $result[] = $handler();
81
        }
82
        sort($result);
83
        return implode(',', $result);
84
    }
85
86
    /**
87
     * @hook on-event custom-event
88
     */
89
    public function hookOne()
90
    {
91
        return 'one';
92
    }
93
94
    /**
95
     * @hook on-event custom-event
96
     */
97
    public function hookTwo()
98
    {
99
        return 'two';
100
    }
101
102
    /**
103
     * Test handling of options
104
     *
105
     * @field-labels
106
     *   a: A
107
     *   b: B
108
     */
109
    public function testSimpleList($options = ['a' => '1', 'b' => '2', 'format' => 'yaml'])
110
    {
111
        $result = ['a' => $options['a'], 'b' => $options['b']];
112
        return new PropertyList($result);
113
    }
114
115
    /**
116
     * Demonstrate Robo error output and command failure.
117
     */
118
    public function testError()
119
    {
120
        return $this->taskExec('ls xyzzy' . date('U'))->dir('/tmp')->run();
121
    }
122
123
    public function testExec()
124
    {
125
        return $this->taskExec('pwd')->run();
126
    }
127
128
    /**
129
     * Demonstrate what happens when a command or a task
130
     * throws an exception.  Note that typically, Robo commands
131
     * should return Result objects rather than throw exceptions.
132
     */
133
    public function testException($options = ['task' => false])
134
    {
135
        if (!$options['task']) {
136
            throw new \RuntimeException('Command failed with an exception.');
137
        }
138
        throw new \RuntimeException('Task failed with an exception.');
139
    }
140
141
    public function testStopOnFail()
142
    {
143
        $this->stopOnFail();
144
        $this->collectionBuilder()
0 ignored issues
show
Documentation Bug introduced by
The method taskExec does not exist on object<Robo\Collection\CollectionBuilder>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
145
            ->taskExec('ls xyzzy' . date('U'))
146
                ->dir('/tmp')
147
            ->run();
148
149
        // stopOnFail() should cause the failed task to throw an exception,
150
        // so we should not get here, and instead exit the program with a
151
        // non-zero status.
152
        return 0;
153
    }
154
155
    public function testVerbosity()
156
    {
157
        $this->output()->writeln('This command will print more information at higher verbosity levels.');
158
        $this->output()->writeln('Try running with -v, -vv or -vvv');
159
        $this->output()->writeln('The current verbosity level is ' . $this->output()->getVerbosity());
160
        $this->output()->writeln('This is a verbose message (-v).', OutputInterface::VERBOSITY_VERBOSE);
161
        $this->output()->writeln('This is a very verbose message (-vv).', OutputInterface::VERBOSITY_VERY_VERBOSE);
162
        $this->output()->writeln('This is a debug message (-vvv).', OutputInterface::VERBOSITY_DEBUG);
163
        $this->logger->warning('This is a warning log message.');
164
        $this->logger->notice('This is a notice log message.');
165
        $this->logger->debug('This is a debug log message.');
166
    }
167
168
    public function testVerbosityThreshold()
169
    {
170
        $this->output()->writeln('This command will print more information at higher verbosity levels.');
171
        $this->output()->writeln('Try running with -v, -vv or -vvv');
172
173
        return $this->collectionBuilder()
0 ignored issues
show
Documentation Bug introduced by
The method taskExec does not exist on object<Robo\Collection\CollectionBuilder>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
174
            ->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
175
            ->taskExec('echo verbose or higher')
176
                ->interactive(false)
177
            ->taskExec('echo very verbose or higher')
178
                ->interactive(false)
179
                ->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERY_VERBOSE)
180
            ->taskExec('echo always printed')
181
                ->interactive(false)
182
                ->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_NORMAL)
183
            ->run();
184
    }
185
186
    public function testDeploy()
187
    {
188
        $gitTask = $this->taskGitStack()
189
            ->pull();
190
191
        $this->taskSshExec('mysite.com')
192
            ->remoteDir('/var/www/somesite')
193
            ->exec($gitTask)
194
            ->run();
195
    }
196
}
197