Add simple unit test

This commit is contained in:
Joseph Bielawski 2023-09-05 12:28:12 +02:00 committed by Jerry Ma
parent 8d348b9e14
commit 33798ff108
4 changed files with 97 additions and 7 deletions

6
.gitignore vendored
View File

@ -17,11 +17,15 @@ composer.lock
# default source build root directory
/buildroot/
# php cs fixer cache file
# tools cache files
.php-cs-fixer.cache
.phpunit.result.cache
# exclude self-runtime
/bin/*
!/bin/spc
!/bin/setup-runtime
!/bin/spc-alpine-docker
# default test directory
/tests/var/

View File

@ -65,5 +65,5 @@ return (new PhpCsFixer\Config())
'phpdoc_var_without_name' => false,
])
->setFinder(
PhpCsFixer\Finder::create()->in(__DIR__ . '/src')
PhpCsFixer\Finder::create()->in([__DIR__ . '/src', __DIR__ . '/tests/SPC'])
);

View File

@ -17,11 +17,12 @@
"zhamao/logger": "^1.0"
},
"require-dev": {
"nunomaduro/collision": "*",
"friendsofphp/php-cs-fixer": "^3.2 != 3.7.0",
"phpstan/phpstan": "^1.1",
"captainhook/captainhook": "^5.10",
"captainhook/plugin-composer": "^5.3"
"captainhook/plugin-composer": "^5.3",
"friendsofphp/php-cs-fixer": "^3.2 != 3.7.0",
"nunomaduro/collision": "*",
"phpstan/phpstan": "^1.1",
"phpunit/phpunit": "^10.3"
},
"autoload": {
"psr-4": {
@ -32,13 +33,18 @@
"src/globals/functions.php"
]
},
"autoload-dev": {
"psr-4": {
"SPC\\Tests\\": "tests/SPC"
}
},
"bin": [
"bin/spc"
],
"scripts": {
"analyse": "phpstan analyse --memory-limit 300M",
"cs-fix": "php-cs-fixer fix",
"test": "bin/phpunit --no-coverage"
"test": "vendor/bin/phpunit tests/ --no-coverage"
},
"config": {
"allow-plugins": {

View File

@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace SPC\Tests\util;
use PHPUnit\Framework\TestCase;
use SPC\store\Config;
use SPC\util\LicenseDumper;
/**
* @internal
*/
final class LicenseDumperTest extends TestCase
{
private const DIRECTORY = '../../var/license-dump';
protected function setUp(): void
{
@rmdir(self::DIRECTORY);
}
protected function tearDown(): void
{
array_map('unlink', glob(self::DIRECTORY . '/*.txt'));
}
public function testDumpWithSingleLicense(): void
{
Config::$lib = [
'fake_lib' => [
'source' => 'fake_lib',
],
];
Config::$source = [
'fake_lib' => [
'license' => [
'type' => 'text',
'text' => 'license',
],
],
];
$dumper = new LicenseDumper();
$dumper->addLibs(['fake_lib']);
$dumper->dump(self::DIRECTORY);
$this->assertFileExists(self::DIRECTORY . '/lib_fake_lib_0.txt');
}
public function testDumpWithMultipleLicenses(): void
{
Config::$lib = [
'fake_lib' => [
'source' => 'fake_lib',
],
];
Config::$source = [
'fake_lib' => [
'license' => [
[
'type' => 'text',
'text' => 'license',
],
[
'type' => 'text',
'text' => 'license',
],
],
],
];
$dumper = new LicenseDumper();
$dumper->addLibs(['fake_lib']);
$dumper->dump(self::DIRECTORY);
$this->assertFileExists(self::DIRECTORY . '/lib_fake_lib_0.txt');
$this->assertFileExists(self::DIRECTORY . '/lib_fake_lib_1.txt');
}
}