Chris@5
|
1 <?php
|
Chris@5
|
2
|
Chris@5
|
3 namespace Consolidation\SiteProcess;
|
Chris@5
|
4
|
Chris@5
|
5 use Symfony\Component\Console\Output\BufferedOutput;
|
Chris@5
|
6
|
Chris@5
|
7 trait CommandTesterTrait
|
Chris@5
|
8 {
|
Chris@5
|
9 /** @var string */
|
Chris@5
|
10 protected $appName;
|
Chris@5
|
11
|
Chris@5
|
12 /** @var string */
|
Chris@5
|
13 protected $appVersion;
|
Chris@5
|
14
|
Chris@5
|
15 /**
|
Chris@5
|
16 * Instantiate a new runner
|
Chris@5
|
17 */
|
Chris@5
|
18 public function setupCommandTester($appName, $appVersion)
|
Chris@5
|
19 {
|
Chris@5
|
20 // Define our invariants for our test
|
Chris@5
|
21 $this->appName = $appName;
|
Chris@5
|
22 $this->appVersion = $appVersion;
|
Chris@5
|
23 }
|
Chris@5
|
24
|
Chris@5
|
25 /**
|
Chris@5
|
26 * Prepare our $argv array; put the app name in $argv[0] followed by
|
Chris@5
|
27 * the command name and all command arguments and options.
|
Chris@5
|
28 *
|
Chris@5
|
29 * @param array $functionParameters should usually be func_get_args()
|
Chris@5
|
30 * @param int $leadingParameterCount the number of function parameters
|
Chris@5
|
31 * that are NOT part of argv. Default is 2 (expected content and
|
Chris@5
|
32 * expected status code).
|
Chris@5
|
33 */
|
Chris@5
|
34 protected function argv($functionParameters, $leadingParameterCount = 2)
|
Chris@5
|
35 {
|
Chris@5
|
36 $argv = $functionParameters;
|
Chris@5
|
37 $argv = array_slice($argv, $leadingParameterCount);
|
Chris@5
|
38 array_unshift($argv, $this->appName);
|
Chris@5
|
39
|
Chris@5
|
40 return $argv;
|
Chris@5
|
41 }
|
Chris@5
|
42
|
Chris@5
|
43 /**
|
Chris@5
|
44 * Simulated front controller
|
Chris@5
|
45 */
|
Chris@5
|
46 protected function execute($argv, $commandClasses, $configurationFile = false)
|
Chris@5
|
47 {
|
Chris@5
|
48 // Define a global output object to capture the test results
|
Chris@5
|
49 $output = new BufferedOutput();
|
Chris@5
|
50
|
Chris@5
|
51 // We can only call `Runner::execute()` once; then we need to tear down.
|
Chris@5
|
52 $runner = new \Robo\Runner($commandClasses);
|
Chris@5
|
53 if ($configurationFile) {
|
Chris@5
|
54 $runner->setConfigurationFilename($configurationFile);
|
Chris@5
|
55 }
|
Chris@5
|
56 $statusCode = $runner->execute($argv, $this->appName, $this->appVersion, $output);
|
Chris@5
|
57
|
Chris@5
|
58 // Destroy our container so that we can call $runner->execute() again for the next test.
|
Chris@5
|
59 \Robo\Robo::unsetContainer();
|
Chris@5
|
60
|
Chris@5
|
61 // Return the output and status code.
|
Chris@5
|
62 return [trim($output->fetch()), $statusCode];
|
Chris@5
|
63 }
|
Chris@5
|
64 }
|