0d9fb0198301b329ceb7a4d158267bfa24f06043
[yaffs-website] / vendor / consolidation / site-alias / tests / SiteAliasCommandsTest.php
1 <?php
2
3 namespace Consolidation\SiteAlias;
4
5 use PHPUnit\Framework\TestCase;
6 use Symfony\Component\Console\Output\BufferedOutput;
7
8 class ExampleCommandsTest extends TestCase
9 {
10     /** @var string[] */
11     protected $commandClasses;
12
13     /** @var string */
14     protected $appName;
15
16     /** @var string */
17     protected $appVersion;
18
19     const STATUS_OK = 0;
20     const STATUS_ERROR = 1;
21
22     /**
23      * Instantiate a new runner
24      */
25     public function setUp()
26     {
27         // Store the command classes we are going to test
28         $this->commandClasses = [ \Consolidation\SiteAlias\Cli\SiteAliasCommands::class ];
29
30         // Define our invariants for our test
31         $this->appName = 'TestFixtureApp';
32         $this->appVersion = '1.0.1';
33     }
34
35     /**
36      * Data provider for testExample.
37      *
38      * Return an array of arrays, each of which contains the parameter
39      * values to be used in one invocation of the testExample test function.
40      */
41     public function exampleTestCommandParameters()
42     {
43         return [
44
45             [
46                 'Add search location: /fixtures/sitealiases/sites', self::STATUS_ERROR,
47                 'site:list', '/fixtures/sitealiases/sites',
48             ],
49
50             [
51                 'List available site aliases', self::STATUS_OK,
52                 'list',
53             ],
54
55         ];
56     }
57
58     /**
59      * Test our example class. Each time this function is called, it will
60      * be passed data from the data provider function idendified by the
61      * dataProvider annotation.
62      *
63      * @dataProvider exampleTestCommandParameters
64      */
65     public function testExampleCommands($expectedOutput, $expectedStatus, $variable_args)
66     {
67         // Create our argv array and run the command
68         $argv = $this->argv(func_get_args());
69         list($actualOutput, $statusCode) = $this->execute($argv);
70
71         // Confirm that our output and status code match expectations
72         $this->assertContains($expectedOutput, $actualOutput);
73         $this->assertEquals($expectedStatus, $statusCode);
74     }
75
76     /**
77      * Prepare our $argv array; put the app name in $argv[0] followed by
78      * the command name and all command arguments and options.
79      */
80     protected function argv($functionParameters)
81     {
82         $argv = $functionParameters;
83         array_shift($argv);
84         array_shift($argv);
85         array_unshift($argv, $this->appName);
86
87         // TODO: replace paths beginning with '/fixtures' with actual path to fixture data
88
89         return $argv;
90     }
91
92     /**
93      * Simulated front controller
94      */
95     protected function execute($argv)
96     {
97         // Define a global output object to capture the test results
98         $output = new BufferedOutput();
99
100         // We can only call `Runner::execute()` once; then we need to tear down.
101         $runner = new \Robo\Runner($this->commandClasses);
102         $statusCode = $runner->execute($argv, $this->appName, $this->appVersion, $output);
103         \Robo\Robo::unsetContainer();
104
105         // Return the output and status code.
106         $actualOutput = trim($output->fetch());
107         return [$actualOutput, $statusCode];
108     }
109 }