Skip to content

Commit

Permalink
Add test disabled cache in params + Improve rector CI (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Jul 24, 2023
1 parent 2c9dcd1 commit c536bd7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/rector.yml
Expand Up @@ -14,6 +14,8 @@ name: rector
jobs:
rector:
uses: yiisoft/actions/.github/workflows/rector.yml@master
secrets:
token: ${{ secrets.YIISOFT_GITHUB_TOKEN }}
with:
os: >-
['ubuntu-latest']
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -33,7 +33,8 @@
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.30|^5.6",
"yiisoft/di": "^1.2"
"yiisoft/di": "^1.2",
"yiisoft/test-support": "^3.0"
},
"provide": {
"yiisoft/router-implementation": "1.0.0"
Expand Down
32 changes: 28 additions & 4 deletions tests/ConfigTest.php
Expand Up @@ -5,13 +5,16 @@
namespace Yiisoft\Router\FastRoute\Tests;

use PHPUnit\Framework\TestCase;
use Psr\SimpleCache\CacheInterface;
use ReflectionObject;
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;
use Yiisoft\Router\FastRoute\UrlGenerator;
use Yiisoft\Router\FastRoute\UrlMatcher;
use Yiisoft\Router\RouteCollectionInterface;
use Yiisoft\Router\UrlGeneratorInterface;
use Yiisoft\Router\UrlMatcherInterface;
use Yiisoft\Test\Support\SimpleCache\MemorySimpleCache;

final class ConfigTest extends TestCase
{
Expand All @@ -31,29 +34,50 @@ public function testDiWeb(): void
$urlMatcher = $container->get(UrlMatcherInterface::class);

$this->assertInstanceOf(UrlMatcher::class, $urlMatcher);
$this->assertInstanceOf(MemorySimpleCache::class, $this->getPropertyValue($urlMatcher, 'cache'));
}

private function createContainer(?string $postfix = null): Container
public function testDiWebWithDisabledCache(): void
{
$params = $this->getParams();
$params['yiisoft/router-fastroute']['enableCache'] = false;
$container = $this->createContainer('web', $params);

$urlMatcher = $container->get(UrlMatcherInterface::class);

$this->assertInstanceOf(UrlMatcher::class, $urlMatcher);
$this->assertNull($this->getPropertyValue($urlMatcher, 'cache'));
}

private function createContainer(?string $postfix = null, ?array $params = null): Container
{
return new Container(
ContainerConfig::create()->withDefinitions(
$this->getDiConfig($postfix)
$this->getDiConfig($postfix, $params)
+
[
CacheInterface::class => new MemorySimpleCache(),
RouteCollectionInterface::class => $this->createMock(RouteCollectionInterface::class),
]
)
);
}

private function getDiConfig(?string $postfix = null): array
private function getDiConfig(?string $postfix = null, ?array $params = null): array
{
$params = $this->getParams();
$params ??= $this->getParams();
return require dirname(__DIR__) . '/config/di' . ($postfix !== null ? '-' . $postfix : '') . '.php';
}

private function getParams(): array
{
return require dirname(__DIR__) . '/config/params.php';
}

private function getPropertyValue(object $object, string $propertyName): mixed
{
$property = (new ReflectionObject($object))->getProperty($propertyName);
$property->setAccessible(true);
return $property->getValue($object);
}
}

0 comments on commit c536bd7

Please sign in to comment.