Completed
Push — master ( 061d1e...31cfb3 )
by Greg
02:26
created

tests/src/RoboFileFixture.php (1 issue)

duplicate/similar methods.

Duplication Informational

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
210
    {
211
        $a = $input->getArgument('a');
212
        $this->say("The parameters passed are:\n" . var_export($a, true));
213
        $foo = $input->getOption('foo');
214
        if (!empty($foo)) {
215
            $this->say("The options passed via --foo are:\n" . var_export($foo, true));
216
        }
217
    }
218
}
219