|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of CliArgs. |
| 4 | + * git: https://github.com/cheprasov/php-cli-args |
| 5 | + * |
| 6 | + * (C) Alexander Cheprasov <cheprasov.84@ya.ru> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | +namespace Test\Unit; |
| 12 | + |
| 13 | +use CliArgs\CliArgs; |
| 14 | + |
| 15 | +class CliArgsTest extends \PHPUnit_Framework_TestCase |
| 16 | +{ |
| 17 | + public function providerTestConfig() |
| 18 | + { |
| 19 | + return [ |
| 20 | + [[], null], |
| 21 | + [ |
| 22 | + ['foo', 'bar'], |
| 23 | + [ |
| 24 | + 'foo' => ['key' => 'foo', 'alias' => null, 'default' => null, 'help' => null, 'filter' => null], |
| 25 | + 'bar' => ['key' => 'bar', 'alias' => null, 'default' => null, 'help' => null, 'filter' => null], |
| 26 | + ] |
| 27 | + ], |
| 28 | + [ |
| 29 | + ['foo' => 'f', 'bar' => 'b', 'help'], |
| 30 | + [ |
| 31 | + 'foo' => ['key' => 'foo', 'alias' => 'f', 'default' => null, 'help' => null, 'filter' => null], |
| 32 | + 'bar' => ['key' => 'bar', 'alias' => 'b', 'default' => null, 'help' => null, 'filter' => null], |
| 33 | + 'help' => ['key' => 'help', 'alias' => null, 'default' => null, 'help' => null, 'filter' => null], |
| 34 | + ] |
| 35 | + ], |
| 36 | + [ |
| 37 | + [ |
| 38 | + 'help' => [], |
| 39 | + 'foo' => [ |
| 40 | + 'alias' => 'f', |
| 41 | + 'default' => 'bar', |
| 42 | + 'help' => 'Some help text', |
| 43 | + 'filter' => ['a', 'b', 'c'] |
| 44 | + ] |
| 45 | + ], |
| 46 | + [ |
| 47 | + 'help' => ['key' => 'help', 'alias' => null, 'default' => null, 'help' => null, 'filter' => null], |
| 48 | + 'foo' => ['key' => 'foo', 'alias' => 'f', 'default' => 'bar', 'help' => 'Some help text', 'filter' => ['a', 'b', 'c']], |
| 49 | + ] |
| 50 | + ], |
| 51 | + ]; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @see \CliArgs\CliArgs::setConfig |
| 56 | + * @dataProvider providerTestConfig |
| 57 | + * @param $config |
| 58 | + * @param $expect |
| 59 | + */ |
| 60 | + public function testConfig($config, $expect) |
| 61 | + { |
| 62 | + $CliArgs = new CliArgs(); |
| 63 | + $Reflection = new \ReflectionClass($CliArgs); |
| 64 | + $Method = $Reflection->getMethod('setConfig'); |
| 65 | + $Method->setAccessible(true); |
| 66 | + $Method->invoke($CliArgs, $config); |
| 67 | + $Property = $Reflection->getProperty('config'); |
| 68 | + $Property->setAccessible(true); |
| 69 | + $result = $Property->getValue($CliArgs); |
| 70 | + $this->assertEquals($expect, $result); |
| 71 | + } |
| 72 | + |
| 73 | + public function providerTestParseArray() |
| 74 | + { |
| 75 | + return [ |
| 76 | + [ |
| 77 | + [__FILE__, '-f', 'b', '--foo', 'bar'], |
| 78 | + null, |
| 79 | + [__FILE__, 'f' => 'b', 'foo' => 'bar'] |
| 80 | + ], |
| 81 | + [ |
| 82 | + ['a', 'b', 'c', '-foo', '--bar'], |
| 83 | + false, |
| 84 | + ['a', 'b', 'c', 'f' => false, 'o' => false, 'bar' => false] |
| 85 | + ], |
| 86 | + [ |
| 87 | + ['-f', '-b', 'foo', '--foo=123'], |
| 88 | + 0, |
| 89 | + ['f' => 0, 'b' => 'foo', 'foo' => '123'] |
| 90 | + ], |
| 91 | + [ |
| 92 | + ['-f=1', '-b'], |
| 93 | + 0, |
| 94 | + ['f' => 0, '=' => 0, 1 => 0, 'b' => 0] |
| 95 | + ], |
| 96 | + [ |
| 97 | + ['a', 'b', 'c'], |
| 98 | + null, |
| 99 | + ['a', 'b', 'c'] |
| 100 | + ], |
| 101 | + [ |
| 102 | + ['-abc'], |
| 103 | + 10, |
| 104 | + ['a' => 10, 'b' => 10, 'c' => 10] |
| 105 | + ], |
| 106 | + ]; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @see \CliArgs\CliArgs::parseArray |
| 111 | + * @dataProvider providerTestParseArray |
| 112 | + * @param array $argv |
| 113 | + * @param mixed $default |
| 114 | + * @param mixed $expect |
| 115 | + */ |
| 116 | + public function testParseArray($argv, $default, $expect) |
| 117 | + { |
| 118 | + $CliArgs = new CliArgs(); |
| 119 | + $Reflection = new \ReflectionClass($CliArgs); |
| 120 | + $Method = $Reflection->getMethod('parseArray'); |
| 121 | + $Method->setAccessible(true); |
| 122 | + $result = $Method->invoke($CliArgs, $argv, $default); |
| 123 | + $this->assertEquals($expect, $result); |
| 124 | + } |
| 125 | + |
| 126 | + public function providerTestFilterValue() |
| 127 | + { |
| 128 | + return [ |
| 129 | + ['int', '042', 0, 42], |
| 130 | + ['int', '42a', 0, 42], |
| 131 | + ['int', '-42', 0, -42], |
| 132 | + ['int', null, 0, 0], |
| 133 | + |
| 134 | + ['float', '04.2', 0.0, 4.2], |
| 135 | + ['float', '4.2e2', 0.0, 420.0], |
| 136 | + ['float', '-4.2e2', 0.0, -420.0], |
| 137 | + ['float', null, 0.0, 0.0], |
| 138 | + |
| 139 | + ['bool', 'true', false, true], |
| 140 | + ['bool', 'True', false, true], |
| 141 | + ['bool', 'TRUE', false, true], |
| 142 | + ['bool', 'Yes', false, true], |
| 143 | + ['bool', 'YES', false, true], |
| 144 | + ['bool', '1', false, true], |
| 145 | + ['bool', null, null, null], |
| 146 | + ['bool', null, false, false], |
| 147 | + ['bool', 'No', false, false], |
| 148 | + ['bool', 'False', null, false], |
| 149 | + ['bool', 'false', null, false], |
| 150 | + ['bool', '0', null, false], |
| 151 | + |
| 152 | + ['flag', null, null, true], |
| 153 | + ['flag', null, false, true], |
| 154 | + ['flag', true, null, true], |
| 155 | + |
| 156 | + ['json', '"123"', null, '123'], |
| 157 | + ['json', '[1, 2, "3"]', null, [1, 2, '3']], |
| 158 | + ['json', '{"foo":"bar", "baz":42}', null, ['foo' => 'bar', 'baz' => 42]], |
| 159 | + ['json', 'bad', null, null], |
| 160 | + ['json', 'bad', false, false], |
| 161 | + |
| 162 | + [['1', '2', '3'], 1, false, false], |
| 163 | + [['1', '2', '3'], '1', false, '1'], |
| 164 | + [['1', '2', '3'], '3', false, '3'], |
| 165 | + [['1', '2', '3'], '5', false, false], |
| 166 | + [['foo', 'bar', 'baz'], 'bar', false, 'bar'], |
| 167 | + [['foo', 'bar', 'baz'], 'bazz', 'foo', 'foo'], |
| 168 | + |
| 169 | + [function($a){ return $a * 2;}, '21', null, 42], |
| 170 | + [function($a, $default){ return $a + $default;}, '32', 10, 42], |
| 171 | + [[static::class,'strtotitle'], 'alexander', null, 'Alexander'], |
| 172 | + [[self::class,'strtotitle'], 'alexander cheprasov', null, 'Alexander Cheprasov'], |
| 173 | + ]; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * @param string $str |
| 178 | + * @return string |
| 179 | + */ |
| 180 | + public static function strtotitle($str) |
| 181 | + { |
| 182 | + return mb_convert_case($str, MB_CASE_TITLE, 'UTF-8'); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * @see \CliArgs\CliArgs::filterValue |
| 187 | + * @dataProvider providerTestFilterValue |
| 188 | + * @param string|array|callable $filter |
| 189 | + * @param string $value |
| 190 | + * @param mixed $default |
| 191 | + * @param mixed $expect |
| 192 | + */ |
| 193 | + public function testFilterValue($filter, $value, $default, $expect) |
| 194 | + { |
| 195 | + $CliArgs = new CliArgs(); |
| 196 | + $Reflection = new \ReflectionClass($CliArgs); |
| 197 | + $Method = $Reflection->getMethod('filterValue'); |
| 198 | + $Method->setAccessible(true); |
| 199 | + $result = $Method->invoke($CliArgs, $filter, $value, $default); |
| 200 | + $this->assertEquals($expect, $result); |
| 201 | + } |
| 202 | +} |
0 commit comments