|
21 | 21 | use Guanguans\SoarPHP\Exceptions\InvalidOptionException; |
22 | 22 | use Guanguans\SoarPHP\Soar; |
23 | 23 |
|
24 | | -it('can add option', function (): void { |
25 | | - expect(Soar::create()) |
26 | | - ->addOption($name = 'foo', $val = 'bar') |
27 | | - ->getOption($name)->toBe($val) |
28 | | - ->addOption($name, $name) |
29 | | - ->getOption($name)->toBe($val); |
30 | | -})->group(__DIR__, __FILE__)->skip(); |
31 | | - |
32 | | -it('can remove option', function (): void { |
33 | | - expect(Soar::create([$name = 'foo' => $val = 'bar'])) |
34 | | - ->getOption($name)->toBe($val) |
35 | | - ->exceptOption($name)->getOption($name)->toBeNull(); |
| 24 | +it('will throw BadMethodCallException when call not exist method', function (): void { |
| 25 | + /** @noinspection PhpUndefinedMethodInspection */ |
| 26 | + Soar::create()->foo(); |
| 27 | +})->group(__DIR__, __FILE__)->throws(BadMethodCallException::class, 'foo'); |
| 28 | + |
| 29 | +it('can call option methods via magic method __call', function (): void { |
| 30 | + expect(Soar::create([])) |
| 31 | + ->setVersion(true)->getOption('-version')->toBeTrue() |
| 32 | + ->withVersion(false)->getOption('-version')->toBeFalse() |
| 33 | + ->withVerbose(true)->onlyVersion()->getOptions()->toHaveKey('-version')->not->toHaveKey('-verbose') |
| 34 | + ->exceptVersion()->getOptions()->not->toHaveKey('-version'); |
| 35 | +})->group(__DIR__, __FILE__); |
| 36 | + |
| 37 | +it('can flush options', function (): void { |
| 38 | + expect(Soar::create(['foo' => 'bar']))->flushOptions()->getOptions()->toBeEmpty(); |
| 39 | +})->group(__DIR__, __FILE__); |
| 40 | + |
| 41 | +it('can only dsns', function (): void { |
| 42 | + expect(Soar::create([ |
| 43 | + $name1 = '-test-dsn' => 'bar', |
| 44 | + $name2 = '-foo' => 'bar', |
| 45 | + ])) |
| 46 | + ->onlyDsns() |
| 47 | + ->getOptions() |
| 48 | + ->toHaveKey($name1) |
| 49 | + ->not->toHaveKey($name2); |
36 | 50 | })->group(__DIR__, __FILE__); |
37 | 51 |
|
38 | 52 | it('can only option', function (): void { |
39 | 53 | expect(Soar::create([ |
40 | | - $name1 = 'key1' => $val = 'val', |
41 | | - $name2 = 'key2' => $val, |
| 54 | + $name1 = '-name1' => $val = 'val', |
| 55 | + $name2 = '-name2' => $val, |
42 | 56 | ])) |
43 | 57 | ->onlyOption($name1) |
44 | | - ->getOption($name1)->toBe($val) |
45 | | - ->getOption($name2)->toBeNull(); |
| 58 | + ->getOptions() |
| 59 | + ->toHaveKey($name1) |
| 60 | + ->not->toHaveKey($name2); |
46 | 61 | })->group(__DIR__, __FILE__); |
47 | 62 |
|
48 | | -it('can only dsn', function (): void { |
| 63 | +it('can except option', function (): void { |
49 | 64 | expect(Soar::create([ |
50 | | - $name1 = '-test-dsn' => $val = 'val', |
51 | | - $name2 = 'key2' => $val, |
| 65 | + $name = '-foo' => 'bar', |
52 | 66 | ])) |
53 | | - ->onlyDsns() |
54 | | - ->getOption($name1)->toBe($val) |
55 | | - ->getOption($name2)->toBeNull(); |
| 67 | + ->exceptOption($name) |
| 68 | + ->getOptions() |
| 69 | + ->not->toHaveKey($name); |
56 | 70 | })->group(__DIR__, __FILE__); |
57 | 71 |
|
58 | | -it('can with option', function (): void { |
59 | | - expect(Soar::create()) |
60 | | - ->withOption($name = 'foo', $str = 'bar')->getOption($name)->toBe($str) |
61 | | - ->withOption( |
62 | | - $name = '-online-dsn', |
63 | | - $arr = [ |
64 | | - 'host' => '192.168.10.10', |
65 | | - 'port' => '3306', |
66 | | - 'dbname' => 'laravel', |
67 | | - 'username' => 'homestead', |
68 | | - 'password' => 'secret', |
69 | | - 'disable' => false, |
70 | | - 'options' => [], |
71 | | - ] |
72 | | - )->getOption($name)->toBe($arr) |
73 | | - ->withOption($name = '-foo', $arr = ['a', 'b', 'c'])->getOption($name)->toBe($arr); |
74 | | -})->group(__DIR__, __FILE__); |
| 72 | +it('can array access', function (): void { |
| 73 | + $soar = Soar::create(); |
75 | 74 |
|
76 | | -it('can merge option', function (): void { |
77 | | - expect(Soar::create([$name = 'foo', 'bar'])) |
78 | | - ->withOption($name, $name) |
79 | | - ->getOption($name)->toBe($name); |
80 | | -})->group(__DIR__, __FILE__); |
| 75 | + $soar[$name = '-foo'] = $val = 'bar'; |
81 | 76 |
|
82 | | -it('can get options', function (): void { |
83 | | - expect(Soar::create($options = ['foo' => 'bar']))->getOptions()->toBe($options); |
84 | | -})->group(__DIR__, __FILE__); |
| 77 | + expect(isset($soar[$name]))->toBeTrue() |
| 78 | + ->and($soar[$name])->toBe($val); |
85 | 79 |
|
86 | | -it('will throw BadMethodCallException when call not exist method', function (): void { |
87 | | - /** @noinspection PhpUndefinedMethodInspection */ |
88 | | - Soar::create()->foo(); |
89 | | -}) |
90 | | - ->group(__DIR__, __FILE__) |
91 | | - ->throws(BadMethodCallException::class, 'foo'); |
92 | | - |
93 | | -it('can call option methods via magic call', function (): void { |
94 | | - // $prefixes = ['add', 'except', 'only', 'set', 'with', 'getNormalized', 'get']; |
95 | | - expect(Soar::create()) |
96 | | - // ->addVersion($val = 'version')->getVersion()->toBe($val) |
97 | | - ->withVersion($val = 'version')->exceptVersion()->getVersionc()->toBeNull() |
98 | | - ->onlyVersion()->getVersion()->toBeNull() |
99 | | - ->withVersion($val)->getVersion()->toBe($val) |
100 | | - ->withVersion($val)->getVersion()->toBe($val); |
101 | | -})->group(__DIR__, __FILE__)->skip(); |
| 80 | + unset($soar[$name]); |
| 81 | + expect($soar)->getOption($name)->toBeNull(); |
| 82 | +})->group(__DIR__, __FILE__); |
102 | 83 |
|
103 | 84 | it('will throw InvalidOptionException when normalize invalid option', function (): void { |
104 | | - (fn (): array => $this->getNormalizedOptions())->call(Soar::create(['foo' => $this->createMock(stdClass::class)])); |
105 | | -}) |
106 | | - ->group(__DIR__, __FILE__) |
107 | | - ->throws(InvalidOptionException::class, 'object'); |
| 85 | + Soar::create(['-foo' => (object) []])->run(); |
| 86 | +})->group(__DIR__, __FILE__)->throws(InvalidOptionException::class, 'object'); |
108 | 87 |
|
109 | 88 | it('can normalize options', function (): void { |
110 | 89 | expect(fn (): array => $this->getNormalizedOptions()) |
111 | 90 | ->call(Soar::create([ |
112 | | - 'foo' => 'bar', |
113 | | - '-verbose' => true, |
114 | | - '-test-dsn' => [ |
115 | | - 'host' => 'you_host', |
116 | | - 'addr' => 'you_host', |
117 | | - 'port' => 'you_port', |
118 | | - 'dbname' => 'you_dbname', |
119 | | - 'schema' => 'you_dbname', |
120 | | - 'username' => 'you_username', |
121 | | - 'user' => 'you_username', |
122 | | - 'password' => 'you_password', |
123 | | - 'disable' => false, |
124 | | - ], |
125 | | - '-online-dsn' => [ |
126 | | - 'host' => 'you_host', |
127 | | - 'port' => 'you_port', |
128 | | - 'dbname' => 'you_dbname', |
129 | | - 'username' => 'you_username', |
130 | | - 'password' => 'you_password', |
131 | | - 'disable' => true, |
132 | | - ], |
133 | | - 'arr' => ['foo', 'bar', 'baz'], |
134 | | - 'closure' => static fn (Soar $soar): string => $soar->getOption('foo'), |
135 | | - 'stringable' => new class { |
136 | | - public function __toString(): string |
| 91 | + '-report-type' => 'json', |
| 92 | + '-config' => null, |
| 93 | + '-log-level' => 3, |
| 94 | + '-allow-online-as-test' => false, |
| 95 | + '-explain' => new class { |
| 96 | + public function __invoke(): bool |
137 | 97 | { |
138 | | - return self::class; |
| 98 | + return true; |
139 | 99 | } |
140 | 100 | }, |
141 | | - 'invoke' => new class { |
142 | | - public function __invoke(): string |
| 101 | + '-explain-format' => new class { |
| 102 | + public function __toString(): string |
143 | 103 | { |
144 | | - return self::class; |
| 104 | + return 'traditional'; |
145 | 105 | } |
146 | 106 | }, |
| 107 | + '-allow-charsets' => [ |
| 108 | + 'utf8', |
| 109 | + 'utf8mb4', |
| 110 | + ], |
| 111 | + '-test-dsn' => [ |
| 112 | + 'user' => '', |
| 113 | + 'password' => '********', |
| 114 | + 'net' => 'tcp', |
| 115 | + // 'addr' => '127.0.0.1:3306', |
| 116 | + 'host' => '127.0.0.1', |
| 117 | + 'port' => 3306, |
| 118 | + 'schema' => 'information_schema', |
| 119 | + 'charset' => 'utf8', |
| 120 | + 'collation' => 'utf8mb4_general_ci', |
| 121 | + 'loc' => 'UTC', |
| 122 | + 'tls' => '', |
| 123 | + 'server-public-key' => '', |
| 124 | + 'max-allowed-packet' => 4194304, |
| 125 | + 'params' => [ |
| 126 | + 'charset' => 'utf8', |
| 127 | + ], |
| 128 | + 'timeout' => '3s', |
| 129 | + 'read-timeout' => '0s', |
| 130 | + 'write-timeout' => '0s', |
| 131 | + 'allow-native-passwords' => true, |
| 132 | + 'allow-old-passwords' => false, |
| 133 | + 'disable' => false, |
| 134 | + ], |
| 135 | + '-online-dsn' => [ |
| 136 | + 'user' => '', |
| 137 | + 'password' => '********', |
| 138 | + 'net' => 'tcp', |
| 139 | + 'addr' => '127.0.0.1:3306', |
| 140 | + 'schema' => 'information_schema', |
| 141 | + 'charset' => 'utf8', |
| 142 | + 'collation' => 'utf8mb4_general_ci', |
| 143 | + 'loc' => 'UTC', |
| 144 | + 'tls' => '', |
| 145 | + 'server-public-key' => '', |
| 146 | + 'max-allowed-packet' => 4194304, |
| 147 | + 'params' => [ |
| 148 | + 'charset' => 'utf8', |
| 149 | + ], |
| 150 | + 'timeout' => '3s', |
| 151 | + 'read-timeout' => '0s', |
| 152 | + 'write-timeout' => '0s', |
| 153 | + 'allow-native-passwords' => true, |
| 154 | + 'allow-old-passwords' => false, |
| 155 | + 'disable' => true, |
| 156 | + ], |
147 | 157 | ])) |
148 | 158 | ->toBeArray(); |
149 | | -}); |
| 159 | +})->group(__DIR__, __FILE__); |
| 160 | + |
| 161 | +it('will throw InvalidOptionException when normalize invalid dsn', function (): void { |
| 162 | + Soar::create([ |
| 163 | + '-test-dsn' => [ |
| 164 | + 'user' => '', |
| 165 | + // 'password' => '********', |
| 166 | + 'net' => 'tcp', |
| 167 | + 'addr' => '127.0.0.1:3306', |
| 168 | + 'host' => '127.0.0.1', |
| 169 | + 'port' => 3306, |
| 170 | + 'schema' => 'information_schema', |
| 171 | + 'disable' => false, |
| 172 | + ], |
| 173 | + ])->run(); |
| 174 | +})->group(__DIR__, __FILE__)->throws(InvalidOptionException::class, 'The option [-test-dsn.password] is required.'); |
0 commit comments