Skip to content

Commit

Permalink
test: refactor test
Browse files Browse the repository at this point in the history
  • Loading branch information
guanguans committed Jan 16, 2024
1 parent 23a5025 commit 41900fa
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 212 deletions.
35 changes: 20 additions & 15 deletions tests/Concerns/ConcreteMagicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,27 @@

it('can serialize and unserialize', function (): void {
$soar = Soar::create(require __DIR__.'/../../examples/soar.options.full.php');
$serializedSoar = serialize($soar);
$unserializedSoar = unserialize($serializedSoar);

expect($unserializedSoar)->toBeInstanceOf(Soar::class)
expect(unserialize(serialize($soar)))->toBeInstanceOf(Soar::class)
->getSoarBinary()->not->toBeEmpty()
->getOptions()->not->toBeEmpty();
});

it('can export', function (): void {
$soar = Soar::create(require __DIR__.'/../../examples/soar.options.full.php');
/** @noinspection DebugFunctionUsageInspection */
$exportedSoarStr = var_export($soar, true);
$exportedSoar = null;
eval("\$exportedSoar = $exportedSoarStr;");

expect($exportedSoar)->toBeInstanceOf(Soar::class)
})->group(__DIR__, __FILE__);

it('can export code block and eval it', function (): void {
expect(
(static function () {
$soar = null;
/** @noinspection DebugFunctionUsageInspection */
$exportedSoarCodeBlock = var_export(
Soar::create(require __DIR__.'/../../examples/soar.options.full.php'),
true
);
eval("\$soar = $exportedSoarCodeBlock;");

/** @noinspection PhpExpressionAlwaysNullInspection */
return $soar;
})()
)
->toBeInstanceOf(Soar::class)
->getSoarBinary()->not->toBeEmpty()
->getOptions()->not->toBeEmpty();
});
})->group(__DIR__, __FILE__);
89 changes: 51 additions & 38 deletions tests/Concerns/ConcreteScoresTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

/** @noinspection PhpInternalEntityUsedInspection */
/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection SqlNoDataSourceInspection */
/** @noinspection SqlResolve */
Expand Down Expand Up @@ -50,7 +52,8 @@
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
sqls;

expect($arrayScores = Soar::create()->arrayScores($sqls))
expect(Soar::create())
->arrayScores($sqls)
->toBeArray()
->not->toBeEmpty()
->each(function (Pest\Expectation $arrayScore): void {
Expand All @@ -64,57 +67,67 @@
'IndexRules',
'Tables',
]);
})
->when(OS::isWindows(), function (Pest\Expectation $expectation): void {
/** @noinspection ForgottenDebugOutputInspection */
/** @noinspection DebugFunctionUsageInspection */
dump($expectation->value);
})
->when(OS::isWindows() && \PHP_VERSION_ID >= 80100, function (Pest\Expectation $expectation): void {
$this->assertMatchesYamlSnapshot($expectation->value);
});

/** @noinspection ForgottenDebugOutputInspection */
/** @noinspection DebugFunctionUsageInspection */
OS::isWindows() and dump($arrayScores);
OS::isWindows() or \PHP_VERSION_ID >= 80100 or $this->assertMatchesYamlSnapshot($arrayScores);
});
})->group(__DIR__, __FILE__);

it('can get json scores', function (): void {
$sqls = 'select * from foo';

expect($jsonScores = Soar::create()->jsonScores($sqls))->toBeJson()->not->toBeEmpty();

$this->assertMatchesJsonSnapshot($jsonScores);
});
expect(Soar::create())
->jsonScores('select * from foo')
->toBeJson()
->not->toBeEmpty()
->assert(function (string $jsonScores): void {
$this->assertMatchesJsonSnapshot($jsonScores);
});
})->group(__DIR__, __FILE__);

it('can get html scores', function (): void {
$htmlScores = Soar::create()->htmlScores('select * from foo');

expect($htmlScores)
expect(Soar::create())
->htmlScores('select * from foo')
->toBeString()
->not->toBeEmpty()
->toContain('foo', '<h1>', '<p>', '<pre>', '<h2>', '<ul>', '<li>');

OS::isWindows() or $this->assertMatchesSnapshot($htmlScores);
});
->toContain('foo', '<h1>', '<p>', '<pre>', '<h2>', '<ul>', '<li>')
->when(! OS::isWindows(), function (Pest\Expectation $expectation): void {
$this->assertMatchesSnapshot($expectation->value);
});
})->group(__DIR__, __FILE__);

it('can get markdown scores', function (): void {
$markdownScores = Soar::create()->markdownScores('select * from foo');

expect($markdownScores)
expect(Soar::create())
->markdownScores('select * from foo')
->toBeString()
->not->toBeEmpty()
->toContain('foo', '#', '```sql', '##', '*');

OS::isWindows() or $this->assertMatchesSnapshot($markdownScores);
->toContain('foo', '#', '```sql', '##', '*')
->when(! OS::isWindows(), function (Pest\Expectation $expectation): void {
$this->assertMatchesSnapshot($expectation->value);
});
});

it('will throw an exception when scores is not a string or array', function (): void {
Soar::create()->scores(true);
})->throws(InvalidArgumentException::class);
})
->group(__DIR__, __FILE__)
->throws(InvalidArgumentException::class, gettype(true));

it('can get scores', function (): void {
$soar = Soar::create();
$scores = $soar->scores('select * from users;');
expect($scores)->toBeString()->not->toBeEmpty();

$scores = $soar->scores(['select * from a; select * from b', 'select * from c', 'select * from d']);
expect($scores)->toBeString()->not->toBeEmpty();

$soar = Soar::create(require __DIR__.'/../../examples/soar.options.full.php');
$scores = $soar->scores('select * from users;');
expect($scores)->toBeString()->not->toBeEmpty();
});
expect(Soar::create())->scores('select * from users;')
->toBeString()
->not->toBeEmpty()
->and(Soar::create(require __DIR__.'/../../examples/soar.options.full.php'))->scores('select * from users;')
->toBeString()
->not->toBeEmpty()
->and(Soar::create())->scores([
'select * from a; select * from b',
'select * from c',
'select * from d',
])
->toBeString()
->not->toBeEmpty();
})->group(__DIR__, __FILE__);
125 changes: 57 additions & 68 deletions tests/Concerns/HasOptionsTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

/** @noinspection NullPointerExceptionInspection */
/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

Expand All @@ -18,102 +19,90 @@
use Guanguans\SoarPHP\Soar;

it('can add option', function (): void {
$options = [$key = 'foo' => $val = 'bar'];
$soar = Soar::create($options);
$option = $soar->addOption($key, $addVal = 'foo')->getOption($key);

expect($val)->toBe($option)
->and($addVal)->not->toBe($option);
});
expect(Soar::create())
->addOption($key = 'foo', $val = 'bar')
->getOption($key)->toBe($val)
->addOption($key, 'foo')
->getOption($key)->toBe($val);
})->group(__DIR__, __FILE__);

it('can remove option', function (): void {
$options = [$key = 'foo' => $val = 'bar'];
$soar = Soar::create($options);
$option = $soar->removeOption($key)->getOption($key);

expect($option)->toBeNull()->not->toBe($val);
});
expect(Soar::create([$key = 'foo' => $val = 'bar']))
->getOption($key)->toBe($val)
->removeOption($key)
->getOption($key)->toBeNull();
})->group(__DIR__, __FILE__);

it('can only option', function (): void {
$soar = Soar::create([
expect(Soar::create([
$key1 = 'key1' => $val = 'val',
$key2 = 'key2' => $val,
]);
$soar = $soar->onlyOption($key1);

expect($val)->toBe($soar->getOption($key1))
->not->toBe($soar->getOption($key2))
->and($soar->getOption($key2))->toBeNull();
});
]))
->onlyOption($key1)
->getOption($key1)->toBe($val)
->getOption($key2)->toBeNull();
})->group(__DIR__, __FILE__);

it('can only dsn', function (): void {
$soar = Soar::create([
expect(Soar::create([
$key1 = '-test-dsn' => $val = 'val',
$key2 = 'key2' => $val,
]);
$soar = $soar->onlyDsn();

expect($val)->toBe($soar->getOption($key1))
->not->toBe($soar->getOption($key2))
->and($soar->getOption($key2))->toBeNull();
});
]))
->onlyDsn()
->getOption($key1)->toBe($val)
->getOption($key2)->toBeNull();
})->group(__DIR__, __FILE__);

it('can set option', function (): void {
$soar = Soar::create();

expect($soar->setOption($key = 'foo', $str = 'bar')->getOption($key))->toBe($str)
->and($soar->setOption($key = '-online-dsn', $arr = [
'host' => '192.168.10.10',
'port' => '3306',
'dbname' => 'laravel',
'username' => 'homestead',
'password' => 'secret',
'disable' => false,
'options' => [],
])->getOption($key))->toBe($arr)
->and($soar->setOption($key = '-foo', $arr = ['a', 'b', 'c'])->getOption($key))->toBe($arr);
});
expect(Soar::create())
->setOption($key = 'foo', $str = 'bar')->getOption($key)->toBe($str)
->setOption(
$key = '-online-dsn',
$arr = [
'host' => '192.168.10.10',
'port' => '3306',
'dbname' => 'laravel',
'username' => 'homestead',
'password' => 'secret',
'disable' => false,
'options' => [],
]
)->getOption($key)->toBe($arr)
->setOption($key = '-foo', $arr = ['a', 'b', 'c'])->getOption($key)->toBe($arr);
})->group(__DIR__, __FILE__);

it('can merge option', function (): void {
$soar = Soar::create();
$option = $soar->mergeOption($key = 'foo', $val = 'bar')->getOption($key);

expect($option)->toBe($val);
});
expect(Soar::create())
->mergeOption($key = 'foo', $val = 'bar')
->getOption($key)->toBe($val);
})->group(__DIR__, __FILE__);

it('can get options', function (): void {
expect(Soar::create())->getOptions()->toBeArray();
});
})->group(__DIR__, __FILE__);

it('will throw an exception when call unknown method', function (): void {
/** @noinspection PhpUndefinedMethodInspection */
Soar::create()->foo();
})->throws(BadMethodCallException::class, 'foo');
})
->group(__DIR__, __FILE__)
->throws(BadMethodCallException::class, 'foo');

it('can call', function (): void {
it('can call options methods', function (): void {
// $prefixes = ['add', 'remove', 'only', 'set', 'merge', 'getNormalized', 'get'];
$val = 'version';
$version = Soar::create()->addVersion($val)->getVersion();
expect($version)->toBe($val);

$version = Soar::create()->setVersion($val)->removeVersion()->getVersion();
expect($version)->toBeNull();

$version = Soar::create()->onlyVersion()->getVersion();
expect($version)->toBeNull();

$version = Soar::create()->setVersion($val)->getVersion();
expect($version)->toBe($val);

$version = Soar::create()->mergeVersion($val)->getVersion();
expect($version)->toBe($val);
});
expect(Soar::create())
->addVersion($val = 'version')->getVersion()->toBe($val)
->setVersion($val)->removeVersion()->getVersion()->toBeNull()
->onlyVersion()->getVersion()->toBeNull()
->setVersion($val)->getVersion()->toBe($val)
->mergeVersion($val)->getVersion()->toBe($val);
})->group(__DIR__, __FILE__);

it('will throw an exception when normalize invalid option', function (): void {
(function (): array {
return $this->getNormalizedOptions();
})->call(Soar::create(['foo' => $this->createMock(stdClass::class)]));
})->throws(InvalidOptionException::class, 'object');
})->group(__DIR__, __FILE__)->throws(InvalidOptionException::class, 'object');

it('can normalize options', function (): void {
$soar = Soar::create([
Expand Down
18 changes: 11 additions & 7 deletions tests/Concerns/HasSoarBinaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
use Guanguans\SoarPHP\Soar;
use Guanguans\SoarPHP\Support\OS;

it('get soar binary', function (): void {
it('can get soar binary', function (): void {
expect(Soar::create())->getSoarBinary()->toBeFile();
})->group(__DIR__, __FILE__);

it('can also get soar binary', function (): void {
// $mock = \Mockery::mock('alias:'.OS::class)->makePartial();
// $mock->allows('isWindows')->andReturnTrue();
// $soar = Soar::create();
// $this->assertFileExists($soar->getSoarBinary());
// $this->assertStringContainsString('windows', $soar->getSoarBinary());
$soar = Soar::create();
expect($soar->getSoarBinary())->toBeFile();

$this->markTestSkipped(__METHOD__.' is skipped.');

// 暂存
$originals = ['isWindows' => OS::isWindows(), 'isMacOS' => OS::isMacOS()];
Expand All @@ -48,8 +48,12 @@

// 恢复
test::double(OS::class, $originals);
});
})
->group(__DIR__, __FILE__)
->skip('This test is skipped.');

it('will throw an exception when set invalid binary', function (): void {
Soar::create()->setSoarBinary('soar.path');
})->throws(InvalidArgumentException::class);
})
->group(__DIR__, __FILE__)
->throws(InvalidArgumentException::class);
4 changes: 2 additions & 2 deletions tests/Concerns/HasSudoPasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
expect(Soar::create())
->setSudoPassword($sudoPassword = 'foo')
->getSudoPassword()->toBe($sudoPassword);
});
})->group(__DIR__, __FILE__);

it('can get escaped sudo password', function (): void {
$soar = Soar::create();

expect(function (Soar $soar): string {
return $soar->getEscapedSudoPassword();
})->call($soar, $soar)->toBeString();
});
})->group(__DIR__, __FILE__);
Loading

0 comments on commit 41900fa

Please sign in to comment.