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