Skip to content

Commit

Permalink
Merge pull request #11 from NaokiTsuchiya/feat/support-laravel-10
Browse files Browse the repository at this point in the history
  • Loading branch information
koriym authored Feb 7, 2023
2 parents fa2a582 + ef2a1d2 commit 2d97eec
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 20 deletions.
66 changes: 61 additions & 5 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,68 @@ name: Continuous Integration

on:
push:
paths-ignore:
- '**.md'
pull_request:
workflow_dispatch:

env:
COMPOSER_FLAGS: "--ansi --no-interaction --no-progress --prefer-dist"
COMPOSER_UPDATE_FLAGS: ""

jobs:
ci:
uses: ray-di/.github/.github/workflows/continuous-integration.yml@v1
with:
old_stable: '["8.0", "8.1"]'
current_stable: 8.2
phpunit:
name: PHPUnit
runs-on: ubuntu-latest
strategy:
matrix:
php: [8.0, 8.1, 8.2]
laravel: [8, 9]
dependency: [lowest, highest]
os: [ubuntu-latest]
include:
- php: 8.1
laravel: 10
- php: 8.2
laravel: 10
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: pcov
ini-values: zend.assertions=1

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Handle lowest dependencies update
if: contains(matrix.dependency, 'lowest')
run: echo COMPOSER_UPDATE_FLAGS=$COMPOSER_UPDATE_FLAGS --prefer-lowest >> $GITHUB_ENV

- name: Update dependencies
run: composer update ${{ env.COMPOSER_UPDATE_FLAGS }} ${{ env.COMPOSER_FLAGS }}

- name: Update laravel
run: composer update ${{ env.COMPOSER_UPDATE_FLAGS }} ${{ env.COMPOSER_FLAGS }} -W laravel/framework:^${{ matrix.laravel }}

- name: Run test suite
run: ./vendor/bin/phpunit --coverage-clover=coverage.xml

- name: Upload coverage report
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
12 changes: 12 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
codecov:
notify:
require_ci_to_pass: yes

coverage:
status:
project:
default:
target: 100%
patch:
default:
target: 100%
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
],
"require": {
"php": ">=8.0",
"ray/di": "^2.14",
"ray/compiler": "^1.9.1",
"ray/di": "^2.15.1",
"ray/compiler": "^1.9.2",
"doctrine/cache": "^1.10 || ^2.2"
},
"require-dev": {
"laravel/framework": "^9.8 || ^8.83",
"laravel/framework": "^8.83 || ^9.8 || ^10.0",
"phpunit/phpunit": "^9.5"
},
"suggest": {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="tests/bootstrap.php">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src/di</directory>
<directory suffix=".php">src</directory>
</include>
</coverage>
<testsuites>
Expand Down
20 changes: 10 additions & 10 deletions src/ApcuCacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function doFetch($id)
*/
protected function doContains($id)
{
return apcu_exists($id);
return apcu_exists($id); // @codeCoverageIgnore
}

/**
Expand All @@ -39,51 +39,51 @@ protected function doSave($id, $data, $lifeTime = 0)
protected function doDelete($id)
{
// apcu_delete returns false if the id does not exist
return apcu_delete($id) || ! apcu_exists($id);
return apcu_delete($id) || ! apcu_exists($id); // @codeCoverageIgnore
}

/**
* {@inheritdoc}
*/
protected function doDeleteMultiple(array $keys)
{
$result = apcu_delete($keys);
$result = apcu_delete($keys); // @codeCoverageIgnoreStart

return $result !== false && count($result) !== count($keys);
return $result !== false && count($result) !== count($keys); // @codeCoverageIgnoreEnd
}

/**
* {@inheritdoc}
*/
protected function doFlush()
{
return apcu_clear_cache();
return apcu_clear_cache(); // @codeCoverageIgnore
}

/**
* {@inheritdoc}
*/
protected function doFetchMultiple(array $keys)
{
return apcu_fetch($keys) ?: [];
return apcu_fetch($keys) ?: []; // @codeCoverageIgnore
}

/**
* {@inheritdoc}
*/
protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
{
$result = apcu_store($keysAndValues, null, $lifetime);
$result = apcu_store($keysAndValues, null, $lifetime); // @codeCoverageIgnoreStart

return empty($result);
return empty($result); // @codeCoverageIgnoreEnd
}

/**
* {@inheritdoc}
*/
protected function doGetStats()
{
$info = apcu_cache_info(true);
$info = apcu_cache_info(true); // @codeCoverageIgnoreStart
$sma = apcu_sma_info();

return [
Expand All @@ -92,6 +92,6 @@ protected function doGetStats()
Cache::STATS_UPTIME => $info['start_time'],
Cache::STATS_MEMORY_USAGE => $info['mem_size'],
Cache::STATS_MEMORY_AVAILABLE => $sma['avail_mem'],
];
]; // @codeCoverageIgnoreEnd
}
}
26 changes: 25 additions & 1 deletion tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,39 @@

namespace Ray\RayDiForLaravel;

use Illuminate\Contracts\Container\BindingResolutionException;
use PHPUnit\Framework\TestCase;
use Ray\Compiler\AbstractInjectorContext;
use Ray\RayDiForLaravel\Classes\FakeCacheableContext;
use Ray\RayDiForLaravel\Classes\FakeContext;
use Ray\RayDiForLaravel\Classes\FakeInvalidContext;
use Ray\RayDiForLaravel\Classes\GreetingServiceProvider;
use Ray\RayDiForLaravel\Classes\IlluminateGreeting;
use Ray\RayDiForLaravel\Classes\InjectableService;
use Ray\RayDiForLaravel\Classes\NonInjectableService;

class ApplicationTest extends TestCase
{
public function testResolvedByRayWhenMarkedClassGiven(): void
public function testResolvedByRayWhenMarkedClassGiven(): Application
{
$app = $this->createApplication(FakeContext::class);

$service = $app->make(InjectableService::class);
$result = $service->run();

$this->assertEquals('Hello, Ray! Intercepted.', $result);

return $app;
}

/**
* @depends testResolvedByRayWhenMarkedClassGiven
*/
public function testResolvedByRayDiWhenAlreadyResolved(Application $app): void
{
$service = $app->make(InjectableService::class);

$this->assertInstanceOf(InjectableService::class, $service);
}

public function testResolvedByIlluminateWhenNonMarkedClassGiven(): void
Expand Down Expand Up @@ -67,6 +81,16 @@ public function testCache(): void
$this->assertFileExists($baseDir . '/Ray_RayDiForLaravel_Classes_InjectableService-.php');
}

public function testFailedToResolveByRayDi(): void
{
$this->expectException(BindingResolutionException::class);
$this->expectExceptionMessage('Failed to resolve ' . InjectableService::class . ' by Ray\'s injector.');

$app = $this->createApplication(FakeInvalidContext::class);

$app->make(InjectableService::class);
}

/**
* @param class-string<AbstractInjectorContext> $contextClass
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/Classes/FakeInvalidBindModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Ray\RayDiForLaravel\Classes;

use Ray\Di\AbstractModule;

class FakeInvalidBindModule extends AbstractModule
{

protected function configure()
{
$this->bind(InjectableService::class);
}
}
34 changes: 34 additions & 0 deletions tests/Classes/FakeInvalidContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Ray\RayDiForLaravel\Classes;

use Doctrine\Common\Cache\CacheProvider;
use Ray\Compiler\AbstractInjectorContext;
use Ray\Di\AbstractModule;
use Ray\Di\NullCache;

class FakeInvalidContext extends AbstractInjectorContext
{
public function __construct(string $tmpDir)
{
$dir = str_replace('\\', '_', self::class);
parent::__construct($tmpDir . '/tmp/' . $dir);
}

public function __invoke(): AbstractModule
{
return new FakeInvalidBindModule();
}

public function getSavedSingleton(): array
{
return [];
}

public function getCache(): CacheProvider
{
return new NullCache();
}
}

0 comments on commit 2d97eec

Please sign in to comment.