Skip to content

Commit

Permalink
Add and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lozovoyv committed Feb 3, 2021
1 parent de2dcca commit 46ab4a5
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 4 deletions.
50 changes: 50 additions & 0 deletions tests/BadConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/*
* This file is part of the OpxCore.
*
* Copyright (c) Lozovoy Vyacheslav <opxcore@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace OpxCore\Tests\Config;

use OpxCore\Config\Config;
use OpxCore\Config\Exceptions\ConfigException;
use OpxCore\Tests\Config\Fixtures\BadRepo;
use OpxCore\Tests\Config\Fixtures\CacheLoadException;
use OpxCore\Tests\Config\Fixtures\CacheSaveException;
use OpxCore\Tests\Config\Fixtures\Repo;
use PHPUnit\Framework\TestCase;

class BadConfigTest extends TestCase
{
public function testBadRepo(): void
{
$config = new Config(new BadRepo());

$this->expectException(ConfigException::class);

$config->load();
}

public function testCacheReadError(): void
{
$repo = new Repo(['app' => ['name' => 'test']]);
$cache = new CacheLoadException();
$config = new Config($repo, $cache);

$this->expectException(ConfigException::class);
$config->load();
}
public function testCacheWriteError(): void
{
$repo = new Repo(['app' => ['name' => 'test']]);
$cache = new CacheSaveException();
$config = new Config($repo, $cache);

$this->expectException(ConfigException::class);
$config->load();
}
}
17 changes: 16 additions & 1 deletion tests/ConfigLoadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
namespace OpxCore\Tests\Config;

use OpxCore\Config\Config;
use OpxCore\Config\Exceptions\ConfigException;
use OpxCore\Tests\Config\Fixtures\Cache;
use OpxCore\Tests\Config\Fixtures\Repo;
use OpxCore\Tests\Config\Fixtures\Env;
Expand Down Expand Up @@ -65,6 +64,22 @@ public function testCacheOkNoEnv(): void
self::assertEquals('cached', $config->get('app.name'));
}

/**
* Cache is ok, no env, load from cache
*/
public function testCacheExpiredNoEnv(): void
{
$repo = new Repo(['app' => ['name' => 'test']]);
$cache = new Cache();
$cache->config = ['app' => ['name' => 'cached']];
$cache->expired = true;

$config = new Config($repo, $cache,);

self::assertTrue($config->load());
self::assertEquals('test', $config['app.name']);
}

/**
* Cache disabled by env, load from repo
*/
Expand Down
3 changes: 0 additions & 3 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
namespace OpxCore\Tests\Config;

use OpxCore\Config\Config;
use OpxCore\Config\Exceptions\ConfigException;
use OpxCore\Tests\Config\Fixtures\Cache;
use OpxCore\Tests\Config\Fixtures\Repo;
use OpxCore\Tests\Config\Fixtures\Env;
use PHPUnit\Framework\TestCase;

class ConfigTest extends TestCase
Expand Down
42 changes: 42 additions & 0 deletions tests/Fixtures/BadRepo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/*
* This file is part of the OpxCore.
*
* Copyright (c) Lozovoy Vyacheslav <opxcore@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace OpxCore\Tests\Config\Fixtures;

use Error;
use Exception;
use OpxCore\Config\Exceptions\ConfigRepositoryException;
use OpxCore\Config\Interfaces\ConfigRepositoryInterface;

class BadRepo implements ConfigRepositoryInterface
{
/**
* @inheritDoc
* @throws ConfigRepositoryException
*/
public function load(array &$config, $profile = null, $overrides = null): bool
{
try {
$config = require __DIR__ . DIRECTORY_SEPARATOR . 'bad.php';
} catch (Exception | Error $e) {
throw new ConfigRepositoryException("Error reading configuration file {$e->getFile()}:{$e->getLine()} {$e->getMessage()}");
}

return true;
}

/**
* @inheritDoc
*/
public function save(array $config, $profile = null): bool
{
return true;
}
}
37 changes: 37 additions & 0 deletions tests/Fixtures/CacheLoadException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the OpxCore.
*
* Copyright (c) Lozovoy Vyacheslav <opxcore@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace OpxCore\Tests\Config\Fixtures;

use OpxCore\Config\Exceptions\ConfigCacheException;
use OpxCore\Config\Interfaces\ConfigCacheInterface;

class CacheLoadException implements ConfigCacheInterface
{
public array $config = [];

public bool $expired = false;
public ?string $profile = null;
public ?int $ttl = null;

public function load(array &$config, $profile = null): bool
{
throw new ConfigCacheException('Load rejected');
}

public function save(array $config, $profile = null, $ttl = null): bool
{
$this->config = $config;
$this->profile = $profile;
$this->ttl = $ttl;

return true;
}
}
27 changes: 27 additions & 0 deletions tests/Fixtures/CacheSaveException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/*
* This file is part of the OpxCore.
*
* Copyright (c) Lozovoy Vyacheslav <opxcore@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace OpxCore\Tests\Config\Fixtures;

use OpxCore\Config\Exceptions\ConfigCacheException;
use OpxCore\Config\Interfaces\ConfigCacheInterface;

class CacheSaveException implements ConfigCacheInterface
{
public function load(array &$config, $profile = null): bool
{
return false;
}

public function save(array $config, $profile = null, $ttl = null): bool
{
throw new ConfigCacheException('Save rejected');
}
}
14 changes: 14 additions & 0 deletions tests/Fixtures/bad.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/*
* This file is part of the OpxCore.
*
* Copyright (c) Lozovoy Vyacheslav <opxcore@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

return [
'app' => 'bad',
e
];

0 comments on commit 46ab4a5

Please sign in to comment.