Skip to content

Commit 63b9e41

Browse files
committed
test(HasOptions): Refactor option tests and add new cases
- Removed unused inspections from ConcreteScoresTest.php - Modified existing tests in HasOptionsTest.php for better clarity - Added tests to validate option flushing and handling non-existent methods - Ensured all tests respect grouping and expected exceptions
1 parent 7e5024b commit 63b9e41

File tree

2 files changed

+122
-98
lines changed

2 files changed

+122
-98
lines changed

tests/Concerns/ConcreteScoresTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
/** @noinspection DebugFunctionUsageInspection */
1010
/** @noinspection ForgottenDebugOutputInspection */
1111
/** @noinspection PhpInternalEntityUsedInspection */
12-
/** @noinspection SqlNoDataSourceInspection */
1312
/** @noinspection SqlResolve */
1413

1514
declare(strict_types=1);

tests/Concerns/HasOptionsTest.php

Lines changed: 122 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -21,129 +21,154 @@
2121
use Guanguans\SoarPHP\Exceptions\InvalidOptionException;
2222
use Guanguans\SoarPHP\Soar;
2323

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);
3650
})->group(__DIR__, __FILE__);
3751

3852
it('can only option', function (): void {
3953
expect(Soar::create([
40-
$name1 = 'key1' => $val = 'val',
41-
$name2 = 'key2' => $val,
54+
$name1 = '-name1' => $val = 'val',
55+
$name2 = '-name2' => $val,
4256
]))
4357
->onlyOption($name1)
44-
->getOption($name1)->toBe($val)
45-
->getOption($name2)->toBeNull();
58+
->getOptions()
59+
->toHaveKey($name1)
60+
->not->toHaveKey($name2);
4661
})->group(__DIR__, __FILE__);
4762

48-
it('can only dsn', function (): void {
63+
it('can except option', function (): void {
4964
expect(Soar::create([
50-
$name1 = '-test-dsn' => $val = 'val',
51-
$name2 = 'key2' => $val,
65+
$name = '-foo' => 'bar',
5266
]))
53-
->onlyDsns()
54-
->getOption($name1)->toBe($val)
55-
->getOption($name2)->toBeNull();
67+
->exceptOption($name)
68+
->getOptions()
69+
->not->toHaveKey($name);
5670
})->group(__DIR__, __FILE__);
5771

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();
7574

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';
8176

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);
8579

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__);
10283

10384
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');
10887

10988
it('can normalize options', function (): void {
11089
expect(fn (): array => $this->getNormalizedOptions())
11190
->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
13797
{
138-
return self::class;
98+
return true;
13999
}
140100
},
141-
'invoke' => new class {
142-
public function __invoke(): string
101+
'-explain-format' => new class {
102+
public function __toString(): string
143103
{
144-
return self::class;
104+
return 'traditional';
145105
}
146106
},
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+
],
147157
]))
148158
->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

Comments
 (0)