diff --git a/README.md b/README.md index c79960b..9541f06 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Laravel Drivers for Redis Sentinel #### Redis Sentinel integration for Laravel and Lumen. -[Redis Sentinel][sentinel] provides high-availability, monitoring, and +[Redis Sentinel][sentinel] facilitates high-availability, monitoring, and load-balancing for Redis servers configured for master-slave replication. [Laravel][laravel] includes built-in support for Redis, but we cannot configure Sentinel setups flexibly out-of-the-box. This limits configuration of Sentinel @@ -18,13 +18,13 @@ types of data like we can in a standard, single-server Redis setup without Sentinel. This causes issues when we need to clear the cache, because Laravel erases stored session information as well. -This package wraps the configuration of Laravel's caching, session, and queue -APIs for Sentinel with the ability to set options for our Redis services -independently. We configure the package separately from Laravel's standard -Redis configuration so we can choose to use the Sentinel connections as needed -by the environment. A developer may use a standalone Redis server in their -local environment, while production environments operate a Redis Sentinel set -of servers. +This package wraps the configuration of Laravel's broadcasting, cache, session, +and queue APIs for Sentinel with the ability to set options for our Redis +services independently. We configure the package separately from Laravel's +standard Redis configuration so we can choose to use the Sentinel connections +as needed by the environment. A developer may use a standalone Redis server in +their local environment, while production environments operate a Redis Sentinel +set of servers. Contents -------- @@ -78,13 +78,18 @@ composer require monospice/laravel-redis-sentinel-drivers composer require monospice/laravel-redis-sentinel-drivers:^1.0 ``` +**Note:** According to the Laravel release schedule, all Laravel versions prior +to 5.4 exited their active development and support periods in August of 2017. +After December, 2017, this package will no longer provide feature releases on +the `1.x` branch for Laravel versions earlier than 5.4. + If the project does not already use Redis with Laravel, this will install the [Predis][predis] package as well. #### Register the Service Provider Laravel 5.5 brings [package discovery][laravel-package-discovery-docs]! *No -additional registration required in Laravel 5.5+.* +service provider registration required in Laravel 5.5+.* To use the drivers in Laravel 5.4 and below, add the package's service provider to *config/app.php*: @@ -165,8 +170,8 @@ Environment-Based Configuration For suitable applications, the package's ability to configure itself from the environment eliminates the need to create or modify configuration files in many scenarios. The package automatically configures Redis Sentinel connections and -the application cache, session, and queue services for these connections using -environment variables. +the application broadcasting, cache, session, and queue services for these +connections using environment variables. Developers may still configure the package [through standard Laravel configuration files][s-standard-config] when the application requirements @@ -187,10 +192,11 @@ This sets up the *default* Redis Sentinel connection for the package's services that we can access through the [`RedisSentinel` facade][s-facade] (or by resolving `app('redis-sentinel')` from the container) like we would for Laravel's [standard Redis API][laravel-redis-api-docs]. To use this Sentinel -connection for Laravel's cache, session, or queue services, change the -following values as well: +connection for Laravel's broadcasting, cache, session, or queue services, +change the following values as well: ```shell +BROADCAST_DRIVER=redis-sentinel CACHE_DRIVER=redis-sentinel SESSION_DRIVER=redis-sentinel QUEUE_DRIVER=redis-sentinel @@ -199,10 +205,11 @@ QUEUE_DRIVER=redis-sentinel #### Connection-Specific Configuration In many cases, we'd set different connection parameters for the application -cache, session, and queue. We may configure different Redis databases for our -cache and session (so that clearing the cache doesn't erase our user session -information), and the Redis servers that contain the application queue may -reside behind a different Sentinel service (master group name): +broadcasts, cache, session, and queue. We may configure different Redis +databases for our cache and session (so that clearing the cache doesn't erase +our user session information), and the Redis servers that contain the +application queue may reside behind a different Sentinel service (master group +name): ```shell REDIS_CACHE_DATABASE=1 @@ -217,9 +224,9 @@ the value of any `*_HOST` variable to a comma-seperated string of hostnames or IP addresses: ```shell -REDIS_HOST=sentinel1.example.com, sentinel2.example.com +REDIS_HOST=sentinel1.example.com, sentinel2.example.com, ... REDIS_CACHE_HOST=10.0.0.1, 10.0.0.2, 10.0.0.3 -REDIS_QUEUE_HOST=tcp://10.0.0.4:26379, tcp://10.0.0.4:26380 +REDIS_QUEUE_HOST=tcp://10.0.0.4:26379, tcp://10.0.0.4:26380, ... ``` Hosts share the port set for the connection unless we explicitly include the @@ -381,15 +388,42 @@ for a single connection. The default values are shown below: ], ``` -### Cache, Session, and Queue Drivers +### Broadcasting, Cache, Session, and Queue Drivers After configuring the Sentinel database connections, we can instruct Laravel to -use these connections for the application's cache, session, and queue services. +use these connections for the application's other redis-enabled services. Remember that we don't need to set Sentinel connections for all of these services. We could select a standard Redis connection for one and a Sentinel connection for another, if desired, but we likely want to take advantage of Sentinel for all of our Redis connections if it's available. +**Note:** We can omit (or remove) the following configuration blocks entirely +and the package will configure these services for us. If we created custom +Sentinel connections as above, we may need to [declare those connection +names](#broadcast_redis_connection-broadcast_redis_sentinel_connection). + +#### Broadcasting + +Add the following connection definition to *config/broadcasting.php* in the +`'connections'` array: + +```php +'connections' => [ + ... + 'redis-sentinel' => [ + 'driver' => 'redis-sentinel', + 'connection' => 'default', + ], +], +``` + +...and change the `BROADCAST_DRIVER` environment variable to `redis-sentinel` +in *.env*. + +If the application contains a specific connection in the `'redis-sentinel'` +database configuration for the event broadcasting, replace `'default'` with its +name. + #### Cache Add the following store definition to *config/cache.php* in the `'stores'` @@ -477,8 +511,8 @@ as needed. Lumen users may need to create this directory if it doesn't exist. A custom package config file need only contain the top-level elements that developer wishes to customize: in the code shown above, the custom config file only overrides the package's default configuration for Redis Sentinel -connections, so the package will still automatically configure the cache, -session, and queue services using environment variables. +connections, so the package will still automatically configure the broadcasting, +cache, session, and queue services using environment variables. Hybrid Configuration -------------------- @@ -488,7 +522,7 @@ configuration methods provided by this package. For example, an application may contain a [standard][s-standard-config] or [package][s-package-config] config file that defines the Redis Sentinel connections, but rely on the package's automatic [environment-based configuration][s-env-config] to set up the cache, -session, and queue services for Sentinel. +session, queue, and broadcasting services for Sentinel. The package uses configuration data in this order of precedence: @@ -551,7 +585,7 @@ local environments and switch to a full Sentinel set of servers in production. Executing Redis Commands (RedisSentinel Facade) ------------------------------------------------- -If a we need to send Redis commands to Redis instances behind a Sentinel server +If we need to send Redis commands to Redis instances behind a Sentinel server directly, such as we can through the `Redis` facade, but we don't want to [override Laravel's Redis API][s-override-redis-api] as above, we can use the `RedisSentinel` facade provided by this package or resolve the `redis-sentinel` @@ -573,10 +607,9 @@ environment. When possible, follow the approach described in the previous section to uniformly connect to Sentinel throughout the application to decouple the code from the Redis implementation. -The facade is automatically aliased in Laravel 5.5+ through the framework's -[package discovery][laravel-package-discovery-docs]. To enable the facade in -Laravel 5.4 and below, add the following alias to the `'aliases'` array in -*config/app.php*: +The facade is not auto-aliased in Laravel 5.5+ for future compatibility with +the PhpRedis extension. To enable the facade in Laravel, add the following +alias to the `'aliases'` array in *config/app.php*: ```php 'aliases' => [ @@ -689,71 +722,53 @@ how it handles connections to Sentinel servers. Set the value of this variable to `redis-sentinel` to [override Laravel's standard Redis API][s-override-redis-api]. -### `CACHE_DRIVER`, `SESSION_DRIVER`, `QUEUE_DRIVER` +### `BROADCAST_DRIVER`, `CACHE_DRIVER`, `SESSION_DRIVER`, `QUEUE_DRIVER` -Laravel uses these to select the backends for the application cache, session, -and queue services. Set the value to `redis-sentinel` for each service that the -application should use Sentinel connections for. +Laravel uses these to select the backends for the application broadcasting, +cache, session, and queue services. Set the value to `redis-sentinel` for each +service that the application should use Sentinel connections for. -### `REDIS_{CACHE,SESSION,QUEUE}_{HOST,PORT,PASSWORD,DATABASE,SERVICE}` +### `REDIS_{BROADCAST,CACHE,SESSION,QUEUE}_{HOST,PORT,PASSWORD,DATABASE,SERVICE}` These variables configure service-specific connection parameters when they -differ from the default Sentinel connection parameters for the cache, session, -and queue connections. +differ from the default Sentinel connection parameters for the broadcasting, +cache, session, and queue connections. For example: - - `REDIS_CACHE_HOST` - Overrides `REDIS_HOST` or `REDIS_SENTINEL_HOST` for - the default *cache* connection. + - `REDIS_BROADCAST_HOST` - Overrides `REDIS_HOST` or `REDIS_SENTINEL_HOST` for + the default *broadcasting* connection. - `REDIS_CACHE_PORT` - Overrides `REDIS_PORT` or `REDIS_SENTINEL_PORT` for the default *cache* connection. - - `REDIS_CACHE_PASSWORD` - Overrides `REDIS_PASSWORD` or - `REDIS_SENTINEL_PASSWORD` for the default *cache* connection. - - `REDIS_CACHE_DATABASE` - Overrides `REDIS_DATABASE` or - `REDIS_SENTINEL_DATABASE` for the default *cache* connection. - - `REDIS_CACHE_SERVICE` - Overrides `REDIS_SENTINEL_SERVICE` for the default - *cache* connection. - - - - `REDIS_SESSION_HOST` - Overrides `REDIS_HOST` or `REDIS_SENTINEL_HOST` for - the default *session* connection. - - `REDIS_SESSION_PORT` - Overrides `REDIS_PORT` or `REDIS_SENTINEL_PORT` for - the default *session* connection. - `REDIS_SESSION_PASSWORD` - Overrides `REDIS_PASSWORD` or `REDIS_SENTINEL_PASSWORD` for the default *session* connection. - - `REDIS_SESSION_DATABASE` - Overrides `REDIS_DATABASE` or - `REDIS_SENTINEL_DATABASE` for the default *session* connection. - - `REDIS_SESSION_SERVICE` - Overrides `REDIS_SENTINEL_SERVICE` for the default - *session* connection. - - - - `REDIS_QUEUE_HOST` - Overrides `REDIS_HOST` or `REDIS_SENTINEL_HOST` for - the default *queue* connection. - - `REDIS_QUEUE_PORT` - Overrides `REDIS_PORT` or `REDIS_SENTINEL_PORT` for - the default *queue* connection. - - `REDIS_QUEUE_PASSWORD` - Overrides `REDIS_PASSWORD` or - `REDIS_SENTINEL_PASSWORD` for the default *queue* connection. - `REDIS_QUEUE_DATABASE` - Overrides `REDIS_DATABASE` or `REDIS_SENTINEL_DATABASE` for the default *queue* connection. - `REDIS_QUEUE_SERVICE` - Overrides `REDIS_SENTINEL_SERVICE` for the default *queue* connection. +### `BROADCAST_REDIS_CONNECTION`, `BROADCAST_REDIS_SENTINEL_CONNECTION` + +The name of the Sentinel connection to select for application broadcasting when +`BROADCAST_DRIVER` equals `redis-sentinel`. It defaults to the package's +internal, auto-configured *broadcasting* connection when unset. ### `CACHE_REDIS_CONNECTION`, `CACHE_REDIS_SENTINEL_CONNECTION` -The name of the Sentinel connection to set for the application cache when +The name of the Sentinel connection to select for the application cache when `CACHE_DRIVER` equals `redis-sentinel`. It defaults to the package's internal, auto-configured *cache* connection when unset. ### `QUEUE_REDIS_CONNECTION`, `QUEUE_REDIS_SENTINEL_CONNECTION` -The name of the Sentinel connection to set for the application queue when +The name of the Sentinel connection to select for the application queue when `QUEUE_DRIVER` equals `redis-sentinel`. It defaults to the package's internal, auto-configured *queue* connection when unset. ### `SESSION_CONNECTION` -The name of the Sentinel connection to set for storing application sessions +The name of the Sentinel connection to select for storing application sessions when `SESSION_DRIVER` equals `redis-sentinel`. It defaults to the package's -internal, auto-configured *session* connection when unset. +internal, auto-configured *session* connection when unset unless the +application configuration already contains a value for `session.connection`. Appendix: Configuration Examples -------------------------------- diff --git a/composer.json b/composer.json index 80ab55e..2db6db2 100644 --- a/composer.json +++ b/composer.json @@ -41,10 +41,7 @@ "laravel": { "providers": [ "Monospice\\LaravelRedisSentinel\\RedisSentinelServiceProvider" - ], - "aliases": { - "RedisSentinel": "Monospice\\LaravelRedisSentinel\\RedisSentinel" - } + ] } } } diff --git a/config/redis-sentinel.php b/config/redis-sentinel.php index a5d5b3a..9a0f139 100644 --- a/config/redis-sentinel.php +++ b/config/redis-sentinel.php @@ -95,10 +95,11 @@ | array that defines options for all connections. | | We can individually configure each of the application service connections - | ("cache", "session", and "queue") with environment variables by setting - | the variables named for each connection. If more than one connection - | shares a common configuration value, we can instead set the environment - | variable that applies to all of the Sentinel connections. + | ("broadcasting", "cache", "session", and "queue") with environment + | variables by setting the variables named for each connection. If more + | than one connection shares a common configuration value, we can instead + | set the environment variable that applies to all of the Sentinel + | connections. | | For example, we may set the following configuration in ".env" for a setup | that uses the same Sentinel hosts for the application's cache and queue, @@ -145,6 +146,20 @@ ], ], + 'broadcasting' => [ + [ + 'host' => env('REDIS_BROADCAST_HOST', $host), + 'port' => env('REDIS_BROADCAST_PORT', $port), + ], + 'options' => [ + 'service' => env('REDIS_BROADCAST_SERVICE', $service), + 'parameters' => [ + 'password' => env('REDIS_BROADCAST_PASSWORD', $password), + 'database' => env('REDIS_BROADCAST_DATABASE', $database), + ], + ], + ], + 'cache' => [ [ 'host' => env('REDIS_CACHE_HOST', $host), @@ -216,6 +231,28 @@ ], + /* + |-------------------------------------------------------------------------- + | Redis Sentinel Broadcasting Connection + |-------------------------------------------------------------------------- + | + | Defines the broadcasting connection that uses a Redis Sentinel connection + | for the application event broadcasting services. + | + */ + + 'broadcasting' => [ + 'connections' => [ + 'redis-sentinel' => [ + 'driver' => 'redis-sentinel', + 'connection' => env( + 'BROADCAST_REDIS_SENTINEL_CONNECTION', + env('BROADCAST_REDIS_CONNECTION', 'broadcasting') + ), + ], + ], + ], + /* |-------------------------------------------------------------------------- | Redis Sentinel Cache Store diff --git a/src/Configuration/Loader.php b/src/Configuration/Loader.php index b716522..9b44600 100644 --- a/src/Configuration/Loader.php +++ b/src/Configuration/Loader.php @@ -210,6 +210,7 @@ protected function shouldLoadConfiguration() protected function configureLumenComponents() { $this->app->configure('database'); + $this->app->configure('broadcasting'); $this->app->configure('cache'); $this->app->configure('queue'); } @@ -224,6 +225,7 @@ protected function loadPackageConfiguration() { $this->setConfigurationFor('database.redis-sentinel'); $this->setConfigurationFor('database.redis.driver'); + $this->setConfigurationFor('broadcasting.connections.redis-sentinel'); $this->setConfigurationFor('cache.stores.redis-sentinel'); $this->setConfigurationFor('queue.connections.redis-sentinel'); $this->setSessionConfiguration(); diff --git a/src/RedisSentinelServiceProvider.php b/src/RedisSentinelServiceProvider.php index 537b9ba..b8a7e28 100644 --- a/src/RedisSentinelServiceProvider.php +++ b/src/RedisSentinelServiceProvider.php @@ -2,12 +2,11 @@ namespace Monospice\LaravelRedisSentinel; -use Illuminate\Cache\CacheManager; +use Illuminate\Broadcasting\Broadcasters\RedisBroadcaster; +use Illuminate\Contracts\Broadcasting\Factory as BroadcastFactory; use Illuminate\Cache\RedisStore; -use Illuminate\Queue\QueueManager; use Illuminate\Queue\Connectors\RedisConnector; use Illuminate\Session\CacheBasedSessionHandler; -use Illuminate\Session\SessionManager; use Illuminate\Support\Arr; use Illuminate\Support\ServiceProvider; use Monospice\LaravelRedisSentinel\Configuration\Loader as ConfigurationLoader; @@ -41,14 +40,7 @@ class RedisSentinelServiceProvider extends ServiceProvider */ public function boot() { - $this->addRedisSentinelCacheDriver($this->app->make('cache')); - $this->addRedisSentinelQueueConnector($this->app->make('queue')); - - // Since version 5.2, Lumen does not include support for sessions by - // default, so we'll only register the session handler if enabled: - if ($this->config->supportsSessions) { - $this->addRedisSentinelSessionHandler($this->app->make('session')); - } + $this->bootComponentDrivers(); // If we want Laravel's Redis API to use Sentinel, we'll remove the // "redis" service from the deferred services in the container: @@ -100,6 +92,25 @@ protected function registerOverrides() }); } + /** + * Extend each of the Laravel services this package supports with the + * corresponding 'redis-sentinel' driver. + * + * @return void + */ + protected function bootComponentDrivers() + { + $this->addRedisSentinelBroadcaster(); + $this->addRedisSentinelCacheStore(); + $this->addRedisSentinelQueueConnector(); + + // Since version 5.2, Lumen does not include support for sessions by + // default, so we'll only register the session handler if enabled: + if ($this->config->supportsSessions) { + $this->addRedisSentinelSessionHandler(); + } + } + /** * Remove the standard Laravel Redis service from the bound deferred * services so they don't overwrite Redis Sentinel registrations. @@ -121,15 +132,32 @@ protected function removeDeferredRedisServices() } /** - * Add "redis-sentinel" as an available driver option to the Laravel cache - * manager. + * Add "redis-sentinel" as an available broadcaster option to the Laravel + * event broadcasting manager. * - * @param CacheManager $cache The Laravel cache manager + * @return void + */ + protected function addRedisSentinelBroadcaster() + { + $this->app->make(BroadcastFactory::class) + ->extend('redis-sentinel', function ($app, $conf) { + $redis = $app->make('redis-sentinel')->getVersionedManager(); + $connection = Arr::get($conf, 'connection', 'default'); + + return new RedisBroadcaster($redis, $connection); + }); + } + + /** + * Add "redis-sentinel" as an available cache store option to the Laravel + * cache manager. * * @return void */ - protected function addRedisSentinelCacheDriver(CacheManager $cache) + protected function addRedisSentinelCacheStore() { + $cache = $this->app->make('cache'); + $cache->extend('redis-sentinel', function ($app, $conf) use ($cache) { $redis = $app->make('redis-sentinel')->getVersionedManager(); $prefix = $app->make('config')->get('cache.prefix'); @@ -144,13 +172,11 @@ protected function addRedisSentinelCacheDriver(CacheManager $cache) * Add "redis-sentinel" as an available driver option to the Laravel * session manager. * - * @param SessionManager $session The Laravel session manager - * * @return void */ - protected function addRedisSentinelSessionHandler(SessionManager $session) + protected function addRedisSentinelSessionHandler() { - $session->extend('redis-sentinel', function ($app) { + $this->app->make('session')->extend('redis-sentinel', function ($app) { $config = $app->make('config'); $cacheDriver = clone $app->make('cache')->driver('redis-sentinel'); $minutes = $config->get('session.lifetime'); @@ -166,13 +192,11 @@ protected function addRedisSentinelSessionHandler(SessionManager $session) * Add "redis-sentinel" as an available queue connection driver option to * the Laravel queue manager. * - * @param QueueManager $queue The Laravel queue manager - * * @return void */ - protected function addRedisSentinelQueueConnector(QueueManager $queue) + protected function addRedisSentinelQueueConnector() { - $queue->extend('redis-sentinel', function () { + $this->app->make('queue')->extend('redis-sentinel', function () { $redis = $this->app->make('redis-sentinel')->getVersionedManager(); return new RedisConnector($redis); diff --git a/tests/Configuration/LoaderTest.php b/tests/Configuration/LoaderTest.php index 4830b6b..ec1bca7 100644 --- a/tests/Configuration/LoaderTest.php +++ b/tests/Configuration/LoaderTest.php @@ -36,6 +36,7 @@ class LoaderTest extends TestCase * @var array */ protected $configKeys = [ + 'broadcast_connection' => 'broadcasting.connections.redis-sentinel', 'cache_store' => 'cache.stores.redis-sentinel', 'sentinel_connections' => 'database.redis-sentinel', 'redis_driver' => 'database.redis.driver', @@ -49,7 +50,7 @@ class LoaderTest extends TestCase * * @var array */ - protected $defaultConfigEnvironmentVars = [ + protected $defaultEnvVars = [ 'REDIS_HOST' => [ 'database.redis-sentinel.default.0.host', 'database.redis-sentinel.cache.0.host', @@ -116,6 +117,21 @@ class LoaderTest extends TestCase 'REDIS_SENTINEL_DISCOVERY' => [ 'database.redis-sentinel.options.update_sentinels', ], + 'REDIS_BROADCAST_HOST' => [ + 'database.redis-sentinel.broadcasting.0.host', + ], + 'REDIS_BROADCAST_PORT' => [ + 'database.redis-sentinel.broadcasting.0.port', + ], + 'REDIS_BROADCAST_SERVICE' => [ + 'database.redis-sentinel.broadcasting.options.service', + ], + 'REDIS_BROADCAST_PASSWORD' => [ + 'database.redis-sentinel.broadcasting.options.parameters.password', + ], + 'REDIS_BROADCAST_DATABASE' => [ + 'database.redis-sentinel.broadcasting.options.parameters.database', + ], 'REDIS_CACHE_HOST' => [ 'database.redis-sentinel.cache.0.host', ], @@ -164,11 +180,17 @@ class LoaderTest extends TestCase 'REDIS_DRIVER' => [ 'database.redis.driver', ], + 'BROADCAST_REDIS_CONNECTION' => [ + 'broadcasting.connections.redis-sentinel.connection', + ], + 'BROADCAST_REDIS_SENTINEL_CONNECTION' => [ + 'broadcasting.connections.redis-sentinel.connection', + ], 'CACHE_REDIS_CONNECTION' => [ - 'cache.stores.redis-sentinel.connection' + 'cache.stores.redis-sentinel.connection', ], 'CACHE_REDIS_SENTINEL_CONNECTION' => [ - 'cache.stores.redis-sentinel.connection' + 'cache.stores.redis-sentinel.connection', ], 'SESSION_CONNECTION' => [ 'session.connection', @@ -192,7 +214,7 @@ public function setUp() if (ApplicationFactory::isLumen()) { unset($this->configKeys['session_connection']); - unset($this->defaultConfigEnvironmentVars['SESSION_CONNECTION']); + unset($this->defaultEnvVars['SESSION_CONNECTION']); } } @@ -247,6 +269,7 @@ public function testLoadsLumenConfigurationDependencies() $this->loader->loadConfiguration(); $this->assertTrue($this->config->has('database')); + $this->assertTrue($this->config->has('broadcasting')); $this->assertTrue($this->config->has('cache')); $this->assertTrue($this->config->has('queue')); } @@ -271,7 +294,7 @@ public function testLoadsDefaultConfigurationFromEnvironment() { $expected = 'environment variable value'; - foreach ($this->defaultConfigEnvironmentVars as $env => $configKeys) { + foreach ($this->defaultEnvVars as $env => $configKeys) { putenv("$env=$expected"); // Set the environment variable // Reset the application configuration for each enviroment variable diff --git a/tests/RedisSentinelServiceProviderTest.php b/tests/RedisSentinelServiceProviderTest.php index 1c10675..4df5760 100644 --- a/tests/RedisSentinelServiceProviderTest.php +++ b/tests/RedisSentinelServiceProviderTest.php @@ -3,6 +3,7 @@ namespace Monospice\LaravelRedisSentinel\Tests; use Illuminate\Config\Repository as ConfigRepository; +use Illuminate\Contracts\Broadcasting\Factory as BroadcastFactory; use Illuminate\Contracts\Redis\Factory as RedisFactory; use Illuminate\Redis\RedisManager; use Monospice\LaravelRedisSentinel\RedisSentinelManager; @@ -50,6 +51,14 @@ public function testIsInitializable() public function testLoadsDefaultConfiguration() { + $expectedConfigKeys = [ + 'database.redis-sentinel', + 'database.redis.driver', + 'broadcasting.connections.redis-sentinel', + 'cache.stores.redis-sentinel', + 'queue.connections.redis-sentinel', + ]; + $config = new ConfigRepository(); $this->app->config = $config; @@ -57,17 +66,13 @@ public function testLoadsDefaultConfiguration() // equals "redis-sentinel" if (! ApplicationFactory::isLumen()) { $config->set('session.driver', 'redis-sentinel'); + $expectedConfigKeys[] = 'session.connection'; } $this->provider->register(); - $this->assertTrue($config->has('database.redis-sentinel')); - $this->assertTrue($config->has('database.redis.driver')); - $this->assertTrue($config->has('cache.stores.redis-sentinel')); - $this->assertTrue($config->has('queue.connections.redis-sentinel')); - - if (! ApplicationFactory::isLumen()) { - $this->assertTrue($config->has('session.connection')); + foreach ($expectedConfigKeys as $configKey) { + $this->assertTrue($config->has($configKey), $configKey); } } @@ -104,6 +109,16 @@ public function testRegisterOverridesStandardRedisApi() $this->assertInstanceOf(RedisSentinelManager::class, $redisService); } + public function testBootExtendsBroadcastConnections() + { + $this->provider->register(); + $this->provider->boot(); + + $broadcast = $this->app->make(BroadcastFactory::class); + + $this->assertNotNull($broadcast->connection('redis-sentinel')); + } + public function testBootExtendsCacheStores() { $this->provider->register(); diff --git a/tests/Support/ApplicationFactory.php b/tests/Support/ApplicationFactory.php index e389178..71b108d 100644 --- a/tests/Support/ApplicationFactory.php +++ b/tests/Support/ApplicationFactory.php @@ -2,6 +2,7 @@ namespace Monospice\LaravelRedisSentinel\Tests\Support; +use Illuminate\Broadcasting\BroadcastServiceProvider; use Illuminate\Cache\CacheServiceProvider; use Illuminate\Config\Repository as ConfigRepository; use Illuminate\Queue\QueueServiceProvider; @@ -36,7 +37,7 @@ public static function make($configure = true) public static function getApplicationVersion() { if (static::isLumen()) { - return substr(static::makeLumenApplication()->version(), 7, 3); + return substr(static::makeLumenApplication(false)->version(), 7, 3); } return \Illuminate\Foundation\Application::VERSION; @@ -54,6 +55,8 @@ public static function getApplicationVersion() public static function makeLaravelApplication($configure = true) { $app = new \Illuminate\Foundation\Application(); + + $app->register(new BroadcastServiceProvider($app)); $app->register(new CacheServiceProvider($app)); $app->register(new QueueServiceProvider($app)); $app->register(new SessionServiceProvider($app)); @@ -82,6 +85,7 @@ public static function makeLumenApplication($configure = true) if ($configure) { $app->register(RedisServiceProvider::class); $app->configure('database'); + $app->configure('broadcasting'); $app->configure('cache'); $app->configure('queue'); } diff --git a/tests/stubs/config.php b/tests/stubs/config.php index 1d8d307..0f70028 100644 --- a/tests/stubs/config.php +++ b/tests/stubs/config.php @@ -50,6 +50,16 @@ ], ], + // Represents a subset of config/broadcasting.php + 'broadcasting' => [ + 'connections' => [ + 'redis-sentinel' => [ + 'driver' => 'redis-sentinel', + 'connection' => 'connection1', + ], + ], + ], + // Represents a subset of config/cache.php 'cache' => [ 'stores' => [