4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Finder\Tests\Shell;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Finder\Shell\Command;
20 class CommandTest extends TestCase
22 public function testCreate()
24 $this->assertInstanceOf('Symfony\Component\Finder\Shell\Command', Command::create());
27 public function testAdd()
29 $cmd = Command::create()->add('--force');
30 $this->assertSame('--force', $cmd->join());
33 public function testAddAsFirst()
35 $cmd = Command::create()->add('--force');
37 $cmd->addAtIndex(Command::create()->add('-F'), 0);
38 $this->assertSame('-F --force', $cmd->join());
41 public function testAddAsLast()
43 $cmd = Command::create()->add('--force');
45 $cmd->addAtIndex(Command::create()->add('-F'), 1);
46 $this->assertSame('--force -F', $cmd->join());
49 public function testAddInBetween()
51 $cmd = Command::create()->add('--force');
52 $cmd->addAtIndex(Command::create()->add('-F'), 0);
54 $cmd->addAtIndex(Command::create()->add('-X'), 1);
55 $this->assertSame('-F -X --force', $cmd->join());
58 public function testCount()
60 $cmd = Command::create();
61 $this->assertSame(0, $cmd->length());
64 $this->assertSame(1, $cmd->length());
67 $this->assertSame(2, $cmd->length());
70 public function testTop()
72 $cmd = Command::create()->add('--force');
75 $this->assertSame('--run --force', $cmd->join());
78 public function testTopLabeled()
80 $cmd = Command::create()->add('--force');
83 $cmd->ins('--something');
84 $cmd->top('--something');
85 $this->assertSame('--something --run --force ', $cmd->join());
88 public function testArg()
90 $cmd = Command::create()->add('--force');
93 $this->assertSame('--force '.escapeshellarg('--run'), $cmd->join());
96 public function testCmd()
98 $cmd = Command::create()->add('--force');
101 $this->assertSame('--force run', $cmd->join());
104 public function testInsDuplicateLabelException()
106 $cmd = Command::create()->add('--force');
109 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('RuntimeException');
113 public function testEnd()
115 $parent = Command::create();
116 $cmd = Command::create($parent);
118 $this->assertSame($parent, $cmd->end());
121 public function testEndNoParentException()
123 $cmd = Command::create();
125 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('RuntimeException');
129 public function testGetMissingLabelException()
131 $cmd = Command::create();
133 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('RuntimeException');
134 $cmd->get('invalid');
137 public function testErrorHandler()
139 $cmd = Command::create();
140 $handler = function () { return 'error-handler'; };
141 $cmd->setErrorHandler($handler);
143 $this->assertSame($handler, $cmd->getErrorHandler());
146 public function testExecute()
148 $cmd = Command::create();
150 $cmd->add('--version');
151 $result = $cmd->execute();
153 $this->assertInternalType('array', $result);
154 $this->assertNotEmpty($result);
155 $this->assertRegExp('/PHP|HipHop/', $result[0]);
158 public function testCastToString()
160 $cmd = Command::create();
161 $cmd->add('--force');
164 $this->assertSame('--force --run', (string) $cmd);