Skip to content

Commit

Permalink
Refactor Laravel 5.7 support, add 5.7 to Travis config
Browse files Browse the repository at this point in the history
Signed-off-by: Cy Rossignol <[email protected]>
  • Loading branch information
cyrossignol committed Sep 20, 2018
1 parent 3e7eb44 commit 76e3cf5
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 59 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ docker-compose.override.yml
phpcs.xml
phpspec.yml
phpunit.xml
/.idea
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ env:
- LARAVEL_VERSION="5.5.*" HORIZON=1
- LARAVEL_VERSION="5.6.*"
- LARAVEL_VERSION="5.6.*" HORIZON=1
- LARAVEL_VERSION="5.7.*"
- LARAVEL_VERSION="5.7.*" HORIZON=1
- LUMEN_VERSION="5.4.*"
- LUMEN_VERSION="5.5.*"
- LUMEN_VERSION="5.6.*"
- LUMEN_VERSION="5.7.*"

matrix:
exclude:
Expand All @@ -33,6 +36,12 @@ matrix:
env: LARAVEL_VERSION="5.6.*" HORIZON=1
- php: 5.6
env: LUMEN_VERSION="5.6.*"
- php: 5.6
env: LARAVEL_VERSION="5.7.*"
- php: 5.6
env: LARAVEL_VERSION="5.7.*" HORIZON=1
- php: 5.6
env: LUMEN_VERSION="5.7.*"
- php: 7.0
env: LARAVEL_VERSION="5.5.*" HORIZON=1
- php: 7.0
Expand All @@ -41,6 +50,12 @@ matrix:
env: LARAVEL_VERSION="5.6.*" HORIZON=1
- php: 7.0
env: LUMEN_VERSION="5.6.*"
- php: 7.0
env: LARAVEL_VERSION="5.7.*"
- php: 7.0
env: LARAVEL_VERSION="5.7.*" HORIZON=1
- php: 7.0
env: LUMEN_VERSION="5.7.*"

before_install:
- if [ -n "$LARAVEL_VERSION" ]; then composer remove --dev --no-update "laravel/lumen-framework"; fi
Expand Down
10 changes: 0 additions & 10 deletions src/Configuration/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,16 +243,6 @@ public function getApplicationVersion()
return \Illuminate\Foundation\Application::VERSION;
}

/**
* Get the current Laravel or Lumen application.
*
* @return \Illuminate\Contracts\Foundation\Application
*/
public function getApplication()
{
return $this->app;
}

/**
* Fetch the specified application configuration value.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Horizon/HorizonServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function register()
protected function registerServices()
{
$this->app->bindIf('redis-sentinel', function ($app) {
return VersionedManagerFactory::make($this->config);
return VersionedManagerFactory::make($this->app, $this->config);
}, true);

$this->app->bindIf('redis-sentinel.manager', function ($app) {
Expand Down
46 changes: 37 additions & 9 deletions src/Manager/VersionedManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Monospice\LaravelRedisSentinel\Manager;

use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Arr;
use Monospice\LaravelRedisSentinel\Configuration\Loader as ConfigurationLoader;
use Monospice\LaravelRedisSentinel\RedisSentinelManager;
Expand All @@ -18,6 +19,14 @@
*/
class VersionedManagerFactory
{
/**
* The current application instance that Laravel's RedisManager depends on
* in version 5.7+.
*
* @var Container
*/
protected $app;

/**
* Detects the application version and provides configuration values.
*
Expand All @@ -28,26 +37,31 @@ class VersionedManagerFactory
/**
* Create a factory using the provided configuration.
*
* @param Container $app The current application instance that
* Laravel's RedisManager depends on in version 5.7+.
* @param ConfigurationLoader $config Detects the application version and
* provides configuration values.
*/
public function __construct(ConfigurationLoader $config)
public function __construct(Container $app, ConfigurationLoader $config)
{
$this->app = $app;
$this->config = $config;
}

/**
* Create an instance of the package's core Redis Sentinel service.
*
* @param Container $app The current application instance that
* Laravel's RedisManager depends on in version 5.7+.
* @param ConfigurationLoader $config Detects the application version and
* provides configuration values.
*
* @return \Monospice\LaravelRedisSentinel\Contracts\Factory A configured
* Redis Sentinel connection manager.
*/
public static function make(ConfigurationLoader $config)
public static function make(Container $app, ConfigurationLoader $config)
{
return (new static($config))->makeInstance();
return (new static($app, $config))->makeInstance();
}

/**
Expand All @@ -59,15 +73,15 @@ public static function make(ConfigurationLoader $config)
public function makeInstance()
{
$class = $this->getVersionedRedisSentinelManagerClass();
$app = $this->config->getApplication();
$config = $this->config->get('database.redis-sentinel', [ ]);
$driver = Arr::pull($config, 'client', 'predis');

if (version_compare($this->config->getApplicationVersion(), '5.6') <= 0) {
// Laravel 5.7 introduced the app as the first parameter:
if ($this->appVersionLessThan('5.7')) {
return new RedisSentinelManager(new $class($driver, $config));
}

return new RedisSentinelManager(new $class($app, $driver, $config));
return new RedisSentinelManager(new $class($this->app, $driver, $config));

}

Expand All @@ -80,18 +94,32 @@ public function makeInstance()
*/
protected function getVersionedRedisSentinelManagerClass()
{
$appVersion = $this->config->getApplicationVersion();

if ($this->config->isLumen) {
$frameworkVersion = '5.4';
} else {
$frameworkVersion = '5.4.20';
}

if (version_compare($appVersion, $frameworkVersion, 'lt')) {
if ($this->appVersionLessThan($frameworkVersion)) {
return Laravel540RedisSentinelManager::class;
}

return Laravel5420RedisSentinelManager::class;
}

/**
* Determine whether the current Laravel framework version is less than the
* specified version.
*
* @param string $version The version to compare to the current version.
*
* @return bool TRUE current framework version is less than the specified
* version.
*/
protected function appVersionLessThan($version)
{
$appVersion = $this->config->getApplicationVersion();

return version_compare($appVersion, $version, 'lt');
}
}
2 changes: 1 addition & 1 deletion src/RedisSentinelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function register()
protected function registerServices()
{
$this->app->singleton('redis-sentinel', function () {
return VersionedManagerFactory::make($this->config);
return VersionedManagerFactory::make($this->app, $this->config);
});

$this->app->singleton('redis-sentinel.manager', function ($app) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Drivers/BroadcastingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function setUp()
$app->config->set('database.redis-sentinel', $this->config);
$app->register(RedisSentinelServiceProvider::class);

if (! ApplicationFactory::isLumen()) {
if (ApplicationFactory::supportsBoot()) {
$app->boot();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Drivers/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function setUp()
$app->config->set('database.redis-sentinel', $this->config);
$app->register(RedisSentinelServiceProvider::class);

if (! ApplicationFactory::isLumen()) {
if (ApplicationFactory::supportsBoot()) {
$app->boot();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Drivers/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function setUp()
$app->config->set('horizon.driver', 'default');
$app->register(RedisSentinelServiceProvider::class);

if (! ApplicationFactory::isLumen()) {
if (ApplicationFactory::supportsBoot()) {
$app->boot();
}

Expand Down
29 changes: 25 additions & 4 deletions tests/Integration/RedisSentinelManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ public function setUp()
{
parent::setUp();

$version = ApplicationFactory::getVersionedRedisSentinelManagerClass();
$versionedManager = new $version('predis', $this->config);

$this->subject = new RedisSentinelManager($versionedManager);
$this->subject = $this->makeSubject('predis', $this->config);
}

public function testExecutesRedisCommands()
Expand All @@ -50,4 +47,28 @@ public function testExecutesRedisCommandsOnSpecificConnection()
$this->assertRedisKeyEquals('test-key', 'test value');
$this->assertEquals('test value', $connection->get('test-key'));
}

/**
* Create an instance of the subject under test.
*
* @param string $client The name of the Redis client implementation.
* @param array $config A set of connection manager config values.
*
* @return VersionedRedisSentinelManager The correct version of the
* Sentinel connection manager for the current version of Laravel.
*/
protected function makeSubject($client, array $config)
{
$class = ApplicationFactory::getVersionedRedisSentinelManagerClass();
$version = ApplicationFactory::getApplicationVersion();

if (version_compare($version, '5.7', 'lt')) {
return new RedisSentinelManager(new $class($client, $config));
}

// Laravel 5.7 introduced the app as the first parameter:
return new RedisSentinelManager(
new $class(ApplicationFactory::make(), $client, $config)
);
}
}
11 changes: 11 additions & 0 deletions tests/Support/ApplicationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ public static function isHorizonAvailable()
return class_exists('Laravel\Horizon\Horizon');
}

/**
* Determine whether the application supports the boot() method.
*
* @return bool True if any supported Laravel version OR Lumen 5.7+.
*/
public static function supportsBoot()
{
return ! static::isLumen()
|| version_compare(static::getApplicationVersion(), '5.7', 'ge');
}

/**
* Get the fully-qualified class name of the RedisSentinelManager class
* for the current version of Laravel or Lumen under test.
Expand Down
10 changes: 7 additions & 3 deletions tests/Unit/Manager/VersionedManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Monospice\LaravelRedisSentinel\Tests\Unit\Manager;

use Illuminate\Contracts\Container\Container;
use Illuminate\Redis\Connections\Connection;
use Mockery;
use Monospice\LaravelRedisSentinel\Configuration\Loader as ConfigurationLoader;
Expand Down Expand Up @@ -41,8 +42,10 @@ public function setUp()
$this->configLoaderMock->shouldReceive('get')
->andReturn([ ])->byDefault();


$this->subject = new VersionedManagerFactory($this->configLoaderMock);
$this->subject = new VersionedManagerFactory(
Mockery::mock(Container::class),
$this->configLoaderMock
);
}

/**
Expand Down Expand Up @@ -83,7 +86,8 @@ public function testBuildsAConnectionFactoryWithFactoryMethod()
->with('database.redis-sentinel', [ ])
->andReturn([ 'connection' => [ ] ]);

$manager = VersionedManagerFactory::make($this->configLoaderMock);
$app = Mockery::mock(Container::class);
$manager = VersionedManagerFactory::make($app, $this->configLoaderMock);
$connection = $manager->connection('connection');

$this->assertInstanceOf(ManagerContract::class, $manager);
Expand Down
Loading

0 comments on commit 76e3cf5

Please sign in to comment.