Skip to content

Commit

Permalink
fix: set redis extension as optional dependency
Browse files Browse the repository at this point in the history
Signed-off-by: Emilien Escalle <[email protected]>
  • Loading branch information
neilime committed Aug 2, 2024
1 parent 513e9d8 commit 48708dc
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 32 deletions.
22 changes: 9 additions & 13 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,22 @@ jobs:
strategy:
fail-fast: false
matrix:
version:
- php: '8.3'
symfony: '5.4'
- php: '8.3'
symfony: '6.4'
- php: '8.3'
symfony: '7.0'
name: PHP ${{ matrix.version.php }} Symfony ${{ matrix.version.symfony }}
php: ["8.3"]
symfony: ["5.4", "6.4", "7.0"]
enable-redis: [true, false]
name: PHP ${{ matrix.php }} Symfony ${{ matrix.symfony }} Redis ${{ matrix.enable-redis && 'enabled' || 'disabled' }}
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.version.php }}
php-version: ${{ matrix.php }}
tools: phpunit-bridge, flex
extensions: pdo_sqlite, redis
extensions: "pdo_sqlite ${{ matrix.enable-redis && ', redis' || '' }}"
coverage: none
- run: |
composer config extra.symfony.require ${{ matrix.version.symfony }}
composer config extra.symfony.require ${{ matrix.symfony }}
composer update
- run: vendor/bin/php-cs-fixer fix --dry-run --diff
- run: vendor/bin/phpunit --exclude-group not-${{ matrix.version.symfony }}
- run: vendor/bin/phpunit --exclude-group not-${{ matrix.symfony }} ${{ !matrix.enable-redis && '--exclude-group redis' || '' }}
env:
SYMFONY_DEPRECATIONS_HELPER: 'disabled=1'
SYMFONY_DEPRECATIONS_HELPER: "disabled=1"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ doctrine:
The bundle can also check that redis connections are working. You should add a list of
service names to check

This will require the PHP Redis extension enabled.

```yaml
ekreative_health_check:
redis:
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
},
"require": {
"php": ">=8.0",
"ext-redis": "*",
"symfony/framework-bundle": "^5.0|^6.0|^7.0"
},
"suggest": {
"ext-redis": "To use the Redis health check"
},
"require-dev": {
"symfony/phpunit-bridge": "^5.0|^6.0|^7.0",
"doctrine/doctrine-bundle": "^2",
Expand Down
5 changes: 2 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,40 @@ public function testAction()
// This env connects to real redis and mysql servers
$client = static::createClient(['environment' => 'test_travis']);
} else {
// This env uses a sqlite connection and fakes the redis server
// This env uses a sqlite connection
$client = static::createClient();
}

$client->request('GET', '/healthcheck');

$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertEquals('application/json', $client->getResponse()->headers->get('content-type'));

$data = json_decode($client->getResponse()->getContent(), true);

$this->assertIsArray($data);
$this->assertCount(2, $data);

$this->assertIsBool($data['app']);
$this->assertTrue($data['app']);

$this->assertIsBool($data['database']);
$this->assertTrue($data['database']);
}

#[Group('redis')]
public function testActionWithRedis()
{
if (isset($_ENV['travis'])) {
// This env connects to real redis and mysql servers
$client = static::createClient(['environment' => 'test_travis']);
} else {
// This env uses a sqlite connection and fakes the redis server
$client = static::createClient(['environment' => 'test_with_redis']);

$redis = $this->getMockBuilder(\Redis::class)
->onlyMethods(['ping'])
->getMock()
;
->getMock();
$redis->method('ping')->willReturn(true);

$client->getKernel()->getContainer()->set('redis', $redis);
Expand Down Expand Up @@ -86,6 +113,7 @@ public function testOptionalMySQLFailAction()
$this->assertFalse($data['database']);
}

#[Group('redis')]
public function testRedisFailAction()
{
$client = static::createClient(['environment' => 'test_with_redis']);
Expand All @@ -104,6 +132,7 @@ public function testRedisFailAction()
$this->assertFalse($data['redis']);
}

#[Group('redis')]
public function testOptionalRedisFailAction()
{
$client = static::createClient(['environment' => 'test_with_redis_optional']);
Expand Down Expand Up @@ -134,16 +163,15 @@ public static function getLazyRedis()
throw new \Exception('Should not be called');
}

#[Group('not-5.4')]
#[Group('not-5.4', 'redis')]
public function testAnnoRoutes()
{
// This env uses a sqlite connection and fakes the redis server
$client = static::createClient(['environment' => 'test_anno']);

$redis = $this->getMockBuilder(\Redis::class)
->onlyMethods(['ping'])
->getMock()
;
->getMock();
$redis->method('ping')->willReturn(true);

$client->getKernel()->getContainer()->set('redis', $redis);
Expand Down
9 changes: 0 additions & 9 deletions tests/config_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,3 @@ doctrine:
default:
driver: pdo_sqlite
path: '%kernel.cache_dir%/db.sqlite'

ekreative_health_check:
redis:
- 'redis'

services:
redis:
class: Redis
public: true
4 changes: 4 additions & 0 deletions tests/config_test_with_redis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ framework:
strict_requirements: ~
utf8: true
secret: 'fake_secret'
http_method_override: true
php_errors:
log: true

monolog:
handlers:
Expand All @@ -25,6 +28,7 @@ ekreative_health_check:
services:
redis:
class: Redis
public: true
factory: Ekreative\HealthCheckBundle\DependencyInjection\RedisFactory::get
arguments:
$host: 'example.com'

0 comments on commit 48708dc

Please sign in to comment.