| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <?php
- declare(strict_types=1);
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 3.6.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Console;
- use Cake\Console\Arguments;
- use Cake\Console\Exception\ConsoleException;
- use Cake\TestSuite\TestCase;
- /**
- * Arguments test case.
- */
- class ArgumentsTest extends TestCase
- {
- /**
- * Get all arguments
- */
- public function testGetArguments(): void
- {
- $values = ['big', 'brown', 'bear'];
- $args = new Arguments($values, [], []);
- $this->assertSame($values, $args->getArguments());
- }
- /**
- * Get arguments by index
- */
- public function testGetArgumentAt(): void
- {
- $values = ['big', 'brown', 'bear'];
- $args = new Arguments($values, [], []);
- $this->assertSame($values[0], $args->getArgumentAt(0));
- $this->assertSame($values[1], $args->getArgumentAt(1));
- $this->assertNull($args->getArgumentAt(3));
- }
- /**
- * check arguments by index
- */
- public function testHasArgumentAt(): void
- {
- $values = ['big', 'brown', 'bear'];
- $args = new Arguments($values, [], []);
- $this->assertTrue($args->hasArgumentAt(0));
- $this->assertTrue($args->hasArgumentAt(1));
- $this->assertFalse($args->hasArgumentAt(3));
- $this->assertFalse($args->hasArgumentAt(-1));
- }
- /**
- * check arguments by name
- */
- public function testHasArgument(): void
- {
- $values = ['big', 'brown', 'bear'];
- $names = ['size', 'color', 'species', 'odd'];
- $args = new Arguments($values, [], $names);
- $this->assertTrue($args->hasArgument('size'));
- $this->assertTrue($args->hasArgument('color'));
- $this->assertFalse($args->hasArgument('odd'));
- $this->assertFalse($args->hasArgument('undefined'));
- }
- /**
- * get arguments by name
- */
- public function testGetArgument(): void
- {
- $values = ['big', 'brown', 'bear'];
- $names = ['size', 'color', 'species', 'odd'];
- $args = new Arguments($values, [], $names);
- $this->assertSame($values[0], $args->getArgument('size'));
- $this->assertSame($values[1], $args->getArgument('color'));
- $this->assertNull($args->getArgument('odd'));
- }
- /**
- * get arguments missing value
- */
- public function testGetArgumentMissing(): void
- {
- $values = [];
- $names = ['size', 'color'];
- $args = new Arguments($values, [], $names);
- $this->assertNull($args->getArgument('size'));
- $this->assertNull($args->getArgument('color'));
- }
- /**
- * get arguments by name
- */
- public function testGetArgumentInvalid(): void
- {
- $values = [];
- $names = ['size'];
- $args = new Arguments($values, [], $names);
- $this->expectException(ConsoleException::class);
- $args->getArgument('color');
- }
- /**
- * test getOptions()
- */
- public function testGetOptions(): void
- {
- $options = [
- 'verbose' => true,
- 'off' => false,
- 'empty' => '',
- ];
- $args = new Arguments([], $options, []);
- $this->assertSame($options, $args->getOptions());
- }
- /**
- * test hasOption()
- */
- public function testHasOption(): void
- {
- $options = [
- 'verbose' => true,
- 'off' => false,
- 'zero' => 0,
- 'empty' => '',
- ];
- $args = new Arguments([], $options, []);
- $this->assertTrue($args->hasOption('verbose'));
- $this->assertTrue($args->hasOption('off'));
- $this->assertTrue($args->hasOption('empty'));
- $this->assertTrue($args->hasOption('zero'));
- $this->assertFalse($args->hasOption('undef'));
- }
- /**
- * test getOption()
- */
- public function testGetOption(): void
- {
- $options = [
- 'verbose' => true,
- 'off' => false,
- 'zero' => '0',
- 'empty' => '',
- ];
- $args = new Arguments([], $options, []);
- $this->assertTrue($args->getOption('verbose'));
- $this->assertFalse($args->getOption('off'));
- $this->assertSame('', $args->getOption('empty'));
- $this->assertSame('0', $args->getOption('zero'));
- $this->assertNull($args->getOption('undef'));
- }
- /**
- * test getOption() checks types
- */
- public function testGetOptionInvalidType(): void
- {
- $options = [
- 'list' => [1, 2],
- ];
- $args = new Arguments([], $options, []);
- $this->expectException(ConsoleException::class);
- $args->getOption('list');
- }
- public function testGetBooleanOption(): void
- {
- $options = [
- 'verbose' => true,
- ];
- $args = new Arguments([], $options, []);
- $this->assertTrue($args->getBooleanOption('verbose'));
- $this->assertNull($args->getBooleanOption('missing'));
- }
- /**
- * test getOption() checks types
- */
- public function testGetOptionBooleanInvalidType(): void
- {
- $options = [
- 'list' => [1, 2],
- ];
- $args = new Arguments([], $options, []);
- $this->expectException(ConsoleException::class);
- $args->getBooleanOption('list');
- }
- public function testGetMultipleOption(): void
- {
- $options = [
- 'types' => ['one', 'two', 'three'],
- ];
- $args = new Arguments([], $options, []);
- $this->assertSame(['one', 'two', 'three'], $args->getMultipleOption('types'));
- $this->assertNull($args->getMultipleOption('missing'));
- }
- public function testGetMultipleOptionInvalidType(): void
- {
- $options = [
- 'connection' => 'test',
- ];
- $args = new Arguments([], $options, []);
- $this->expectException(ConsoleException::class);
- $args->getMultipleOption('connection');
- }
- }
|