Completed
Push — master ( f46321...378618 )
by Greg
03:20
created

RoboFileFixture::testVerbosityThreshold()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
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 Robo\Contract\VerbosityThresholdInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
/**
14
 * RoboFile under test: a fixture containing some commands to use with tests.
15
 */
16
class RoboFileFixture extends \Robo\Tasks implements LoggerAwareInterface, CustomEventAwareInterface
17
{
18
    use LoggerAwareTrait;
19
    use CustomEventAwareTrait;
20
21
    /**
22
     * Demonstrate Robo variable argument passing.
23
     *
24
     * @param $a A list of commandline parameters.
25
     */
26
    public function testArrayArgs(array $a)
27
    {
28
        $this->say("The parameters passed are:\n" . var_export($a, true));
29
    }
30
31
    /**
32
     * Demonstrate use of SymfonyStyle
33
     */
34
    public function testSymfonyStyle()
35
    {
36
        $this->io()->title('My Title');
37
        $this->io()->section('Section 1');
38
        $this->io()->text('Some text in section one.');
39
        $this->io()->comment('This is just an example of different styles.');
40
        $this->io()->section('Section 2');
41
        $this->io()->text('Some text in section two.');
42
    }
43
44
    /**
45
     * @hook command-event test:command-event
46
     */
47
    public function hookCommandEvent()
48
    {
49
        $this->io()->text('This is the command-event hook for the test:command-event command.');
50
    }
51
52
    public function testCommandEvent()
53
    {
54
        $this->io()->text('This is the main method for the test:command-event command.');
55
    }
56
57
    /**
58
     * @hook post-command test:command-event
59
     */
60
    public function hookPostCommand()
61
    {
62
        $this->io()->text('This is the post-command hook for the test:command-event command.');
63
    }
64
65
    /**
66
     * This command uses a custom event 'custom-event' to collect data.  Note that
67
     * the event handlers will not be found unless the hook manager is
68
     * injected into this command handler object via `setHookManager()`
69
     * (defined in CustomEventAwareTrait). The Robo DI container does this
70
     * for us through inflection.
71
     *
72
     * @command test:custom-event
73
     */
74
    public function testCustomEvent()
75
    {
76
        $myEventHandlers = $this->getCustomEventHandlers('custom-event');
77
        $result = [];
78
        foreach ($myEventHandlers as $handler) {
79
            $result[] = $handler();
80
        }
81
        sort($result);
82
        return implode(',', $result);
83
    }
84
85
    /**
86
     * @hook on-event custom-event
87
     */
88
    public function hookOne()
89
    {
90
        return 'one';
91
    }
92
93
    /**
94
     * @hook on-event custom-event
95
     */
96
    public function hookTwo()
97
    {
98
        return 'two';
99
    }
100
101
    /**
102
     * Demonstrate Robo error output and command failure.
103
     */
104
    public function testError()
105
    {
106
        return $this->taskExec('ls xyzzy' . date('U'))->dir('/tmp')->run();
107
    }
108
109
    /**
110
     * Demonstrate what happens when a command or a task
111
     * throws an exception.  Note that typically, Robo commands
112
     * should return Result objects rather than throw exceptions.
113
     */
114
    public function testException($options = ['task' => false])
115
    {
116
        if (!$options['task']) {
117
            throw new \RuntimeException('Command failed with an exception.');
118
        }
119
        throw new \RuntimeException('Task failed with an exception.');
120
    }
121
122
    public function testStopOnFail()
123
    {
124
        $this->stopOnFail();
125
        $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...
126
            ->taskExec('ls xyzzy' . date('U'))
127
                ->dir('/tmp')
128
            ->run();
129
130
        // stopOnFail() should cause the failed task to throw an exception,
131
        // so we should not get here, and instead exit the program with a
132
        // non-zero status.
133
        return 0;
134
    }
135
136
    public function testVerbosity()
137
    {
138
        $this->output()->writeln('This command will print more information at higher verbosity levels.');
139
        $this->output()->writeln('Try running with -v, -vv or -vvv');
140
        $this->output()->writeln('The current verbosity level is ' . $this->output()->getVerbosity());
141
        $this->output()->writeln('This is a verbose message (-v).', OutputInterface::VERBOSITY_VERBOSE);
142
        $this->output()->writeln('This is a very verbose message (-vv).', OutputInterface::VERBOSITY_VERY_VERBOSE);
143
        $this->output()->writeln('This is a debug message (-vvv).', OutputInterface::VERBOSITY_DEBUG);
144
        $this->logger->warning('This is a warning log message.');
145
        $this->logger->notice('This is a notice log message.');
146
        $this->logger->debug('This is a debug log message.');
147
    }
148
149
    public function testVerbosityThreshold()
150
    {
151
        $this->output()->writeln('This command will print more information at higher verbosity levels.');
152
        $this->output()->writeln('Try running with -v, -vv or -vvv');
153
154
        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...
155
            ->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
156
            ->taskExec('echo verbose or higher')
157
                ->interactive(false)
158
            ->taskExec('echo very verbose or higher')
159
                ->interactive(false)
160
                ->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERY_VERBOSE)
161
            ->taskExec('echo always printed')
162
                ->interactive(false)
163
                ->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_NORMAL)
164
            ->run();
165
    }
166
167
    public function testDeploy()
168
    {
169
        $gitTask = $this->taskGitStack()
170
            ->pull();
171
172
        $this->taskSshExec('mysite.com')
173
            ->remoteDir('/var/www/somesite')
174
            ->exec($gitTask)
175
            ->run();
176
    }
177
}
178