Skip to content

Commit

Permalink
register ‘mock’ redis driver
Browse files Browse the repository at this point in the history
Since Laravel 5.8.30 it is possible to register a custom Redis driver. No need to overwrite RedisManager any more.

laravel/framework#29275
  • Loading branch information
josiasmontag committed Aug 2, 2019
1 parent 7ead832 commit 67123c5
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 76 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"require": {
"php" : ">=7.1.0",
"laravel/framework": "5.7.*|5.8.*",
"laravel/framework": "^5.8.30",
"predis/predis": "^1.1",
"m6web/redis-mock": "^4.2"
},
Expand Down
5 changes: 3 additions & 2 deletions src/MockPredisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class MockPredisConnection extends PredisConnection
/**
* Execute commands in a pipeline.
*
* @param callable|null $callback
* @param callable|null $callback
*
* @return \Redis|array
*/
public function pipeline(callable $callback = null)
Expand All @@ -26,4 +27,4 @@ public function pipeline(callable $callback = null)
? $pipeline
: tap($pipeline, $callback)->exec();
}
}
}
10 changes: 5 additions & 5 deletions src/MockPredisConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class MockPredisConnector extends PredisConnector
/**
* Create a new clustered Predis connection.
*
* @param array $config
* @param array $options
* @param array $config
* @param array $options
*
* @return \Illuminate\Redis\Connections\PredisConnection
*/
Expand All @@ -39,9 +39,9 @@ public function connect(array $config, array $options)
/**
* Create a new clustered Predis connection.
*
* @param array $config
* @param array $clusterOptions
* @param array $options
* @param array $config
* @param array $clusterOptions
* @param array $options
*
* @return \Illuminate\Redis\Connections\PredisClusterConnection
*/
Expand Down
32 changes: 4 additions & 28 deletions src/Providers/RedisMockServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@
namespace Lunaweb\RedisMock\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Arr;
use Lunaweb\RedisMock\RedisManager;
use Lunaweb\RedisMock\MockPredisConnector;

class RedisMockServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;

/**
* Bootstrap the application services.
Expand All @@ -27,7 +20,9 @@ class RedisMockServiceProvider extends ServiceProvider
*/
public function boot()
{
//
$this->app->make('redis')->extend('mock', function () {
return new MockPredisConnector();
});
}

/**
Expand All @@ -38,26 +33,7 @@ public function boot()
public function register()
{

$this->app->singleton('redis',function ($app) {
$config = $app->make('config')->get('database.redis');

return new RedisManager($app, Arr::pull($config, 'client', 'predis'), $config);
});

$this->app->bind('redis.connection', function ($app) {
return $app['redis']->connection();
});
}


/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['redis', 'redis.connection'];
}

}
34 changes: 0 additions & 34 deletions src/RedisManager.php

This file was deleted.

3 changes: 1 addition & 2 deletions tests/EnvironmentSetUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Lunaweb\RedisMock\Tests;

use Illuminate\Redis\RedisServiceProvider;
use Lunaweb\RedisMock\Providers\RedisMockServiceProvider;

trait EnvironmentSetUp
Expand All @@ -19,7 +18,7 @@ protected function getEnvironmentSetUp($app)
$app['config']->set('app.debug', true);
$app['config']->set('database.redis.client', 'mock');

$app->register(RedisServiceProvider::class);

$app->register(RedisMockServiceProvider::class);
}

Expand Down
14 changes: 10 additions & 4 deletions tests/RedisMockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@ class RedisMockTest extends TestCase
use EnvironmentSetUp;


public function testMock() {
public function testRedisConnectionInstance()
{

$this->assertInstanceOf(MockPredisConnection::class, Redis::connection());

}

public function testSetAndGet()
{

Redis::set('key', 'test');
$this->assertEquals('test', Redis::get('key'));

}

public function testPipeline() {

$this->assertInstanceOf(MockPredisConnection::class, Redis::connection());
public function testPipeline()
{

Redis::pipeline(function ($pipe) {
$pipe->set('key1', 'test1');
Expand Down

0 comments on commit 67123c5

Please sign in to comment.