Skip to content

Commit 76e3cf5

Browse files
committed
Refactor Laravel 5.7 support, add 5.7 to Travis config
Signed-off-by: Cy Rossignol <[email protected]>
1 parent 3e7eb44 commit 76e3cf5

13 files changed

+141
-59
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ docker-compose.override.yml
88
phpcs.xml
99
phpspec.yml
1010
phpunit.xml
11-
/.idea

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ env:
1515
- LARAVEL_VERSION="5.5.*" HORIZON=1
1616
- LARAVEL_VERSION="5.6.*"
1717
- LARAVEL_VERSION="5.6.*" HORIZON=1
18+
- LARAVEL_VERSION="5.7.*"
19+
- LARAVEL_VERSION="5.7.*" HORIZON=1
1820
- LUMEN_VERSION="5.4.*"
1921
- LUMEN_VERSION="5.5.*"
2022
- LUMEN_VERSION="5.6.*"
23+
- LUMEN_VERSION="5.7.*"
2124

2225
matrix:
2326
exclude:
@@ -33,6 +36,12 @@ matrix:
3336
env: LARAVEL_VERSION="5.6.*" HORIZON=1
3437
- php: 5.6
3538
env: LUMEN_VERSION="5.6.*"
39+
- php: 5.6
40+
env: LARAVEL_VERSION="5.7.*"
41+
- php: 5.6
42+
env: LARAVEL_VERSION="5.7.*" HORIZON=1
43+
- php: 5.6
44+
env: LUMEN_VERSION="5.7.*"
3645
- php: 7.0
3746
env: LARAVEL_VERSION="5.5.*" HORIZON=1
3847
- php: 7.0
@@ -41,6 +50,12 @@ matrix:
4150
env: LARAVEL_VERSION="5.6.*" HORIZON=1
4251
- php: 7.0
4352
env: LUMEN_VERSION="5.6.*"
53+
- php: 7.0
54+
env: LARAVEL_VERSION="5.7.*"
55+
- php: 7.0
56+
env: LARAVEL_VERSION="5.7.*" HORIZON=1
57+
- php: 7.0
58+
env: LUMEN_VERSION="5.7.*"
4459

4560
before_install:
4661
- if [ -n "$LARAVEL_VERSION" ]; then composer remove --dev --no-update "laravel/lumen-framework"; fi

src/Configuration/Loader.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -243,16 +243,6 @@ public function getApplicationVersion()
243243
return \Illuminate\Foundation\Application::VERSION;
244244
}
245245

246-
/**
247-
* Get the current Laravel or Lumen application.
248-
*
249-
* @return \Illuminate\Contracts\Foundation\Application
250-
*/
251-
public function getApplication()
252-
{
253-
return $this->app;
254-
}
255-
256246
/**
257247
* Fetch the specified application configuration value.
258248
*

src/Horizon/HorizonServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function register()
9898
protected function registerServices()
9999
{
100100
$this->app->bindIf('redis-sentinel', function ($app) {
101-
return VersionedManagerFactory::make($this->config);
101+
return VersionedManagerFactory::make($this->app, $this->config);
102102
}, true);
103103

104104
$this->app->bindIf('redis-sentinel.manager', function ($app) {

src/Manager/VersionedManagerFactory.php

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Monospice\LaravelRedisSentinel\Manager;
44

5+
use Illuminate\Contracts\Container\Container;
56
use Illuminate\Support\Arr;
67
use Monospice\LaravelRedisSentinel\Configuration\Loader as ConfigurationLoader;
78
use Monospice\LaravelRedisSentinel\RedisSentinelManager;
@@ -18,6 +19,14 @@
1819
*/
1920
class VersionedManagerFactory
2021
{
22+
/**
23+
* The current application instance that Laravel's RedisManager depends on
24+
* in version 5.7+.
25+
*
26+
* @var Container
27+
*/
28+
protected $app;
29+
2130
/**
2231
* Detects the application version and provides configuration values.
2332
*
@@ -28,26 +37,31 @@ class VersionedManagerFactory
2837
/**
2938
* Create a factory using the provided configuration.
3039
*
40+
* @param Container $app The current application instance that
41+
* Laravel's RedisManager depends on in version 5.7+.
3142
* @param ConfigurationLoader $config Detects the application version and
3243
* provides configuration values.
3344
*/
34-
public function __construct(ConfigurationLoader $config)
45+
public function __construct(Container $app, ConfigurationLoader $config)
3546
{
47+
$this->app = $app;
3648
$this->config = $config;
3749
}
3850

3951
/**
4052
* Create an instance of the package's core Redis Sentinel service.
4153
*
54+
* @param Container $app The current application instance that
55+
* Laravel's RedisManager depends on in version 5.7+.
4256
* @param ConfigurationLoader $config Detects the application version and
4357
* provides configuration values.
4458
*
4559
* @return \Monospice\LaravelRedisSentinel\Contracts\Factory A configured
4660
* Redis Sentinel connection manager.
4761
*/
48-
public static function make(ConfigurationLoader $config)
62+
public static function make(Container $app, ConfigurationLoader $config)
4963
{
50-
return (new static($config))->makeInstance();
64+
return (new static($app, $config))->makeInstance();
5165
}
5266

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

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

70-
return new RedisSentinelManager(new $class($app, $driver, $config));
84+
return new RedisSentinelManager(new $class($this->app, $driver, $config));
7185

7286
}
7387

@@ -80,18 +94,32 @@ public function makeInstance()
8094
*/
8195
protected function getVersionedRedisSentinelManagerClass()
8296
{
83-
$appVersion = $this->config->getApplicationVersion();
84-
8597
if ($this->config->isLumen) {
8698
$frameworkVersion = '5.4';
8799
} else {
88100
$frameworkVersion = '5.4.20';
89101
}
90102

91-
if (version_compare($appVersion, $frameworkVersion, 'lt')) {
103+
if ($this->appVersionLessThan($frameworkVersion)) {
92104
return Laravel540RedisSentinelManager::class;
93105
}
94106

95107
return Laravel5420RedisSentinelManager::class;
96108
}
109+
110+
/**
111+
* Determine whether the current Laravel framework version is less than the
112+
* specified version.
113+
*
114+
* @param string $version The version to compare to the current version.
115+
*
116+
* @return bool TRUE current framework version is less than the specified
117+
* version.
118+
*/
119+
protected function appVersionLessThan($version)
120+
{
121+
$appVersion = $this->config->getApplicationVersion();
122+
123+
return version_compare($appVersion, $version, 'lt');
124+
}
97125
}

src/RedisSentinelServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function register()
8484
protected function registerServices()
8585
{
8686
$this->app->singleton('redis-sentinel', function () {
87-
return VersionedManagerFactory::make($this->config);
87+
return VersionedManagerFactory::make($this->app, $this->config);
8888
});
8989

9090
$this->app->singleton('redis-sentinel.manager', function ($app) {

tests/Integration/Drivers/BroadcastingTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function setUp()
3131
$app->config->set('database.redis-sentinel', $this->config);
3232
$app->register(RedisSentinelServiceProvider::class);
3333

34-
if (! ApplicationFactory::isLumen()) {
34+
if (ApplicationFactory::supportsBoot()) {
3535
$app->boot();
3636
}
3737

tests/Integration/Drivers/CacheTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function setUp()
3737
$app->config->set('database.redis-sentinel', $this->config);
3838
$app->register(RedisSentinelServiceProvider::class);
3939

40-
if (! ApplicationFactory::isLumen()) {
40+
if (ApplicationFactory::supportsBoot()) {
4141
$app->boot();
4242
}
4343

tests/Integration/Drivers/QueueTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function setUp()
4646
$app->config->set('horizon.driver', 'default');
4747
$app->register(RedisSentinelServiceProvider::class);
4848

49-
if (! ApplicationFactory::isLumen()) {
49+
if (ApplicationFactory::supportsBoot()) {
5050
$app->boot();
5151
}
5252

tests/Integration/RedisSentinelManagerTest.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ public function setUp()
2424
{
2525
parent::setUp();
2626

27-
$version = ApplicationFactory::getVersionedRedisSentinelManagerClass();
28-
$versionedManager = new $version('predis', $this->config);
29-
30-
$this->subject = new RedisSentinelManager($versionedManager);
27+
$this->subject = $this->makeSubject('predis', $this->config);
3128
}
3229

3330
public function testExecutesRedisCommands()
@@ -50,4 +47,28 @@ public function testExecutesRedisCommandsOnSpecificConnection()
5047
$this->assertRedisKeyEquals('test-key', 'test value');
5148
$this->assertEquals('test value', $connection->get('test-key'));
5249
}
50+
51+
/**
52+
* Create an instance of the subject under test.
53+
*
54+
* @param string $client The name of the Redis client implementation.
55+
* @param array $config A set of connection manager config values.
56+
*
57+
* @return VersionedRedisSentinelManager The correct version of the
58+
* Sentinel connection manager for the current version of Laravel.
59+
*/
60+
protected function makeSubject($client, array $config)
61+
{
62+
$class = ApplicationFactory::getVersionedRedisSentinelManagerClass();
63+
$version = ApplicationFactory::getApplicationVersion();
64+
65+
if (version_compare($version, '5.7', 'lt')) {
66+
return new RedisSentinelManager(new $class($client, $config));
67+
}
68+
69+
// Laravel 5.7 introduced the app as the first parameter:
70+
return new RedisSentinelManager(
71+
new $class(ApplicationFactory::make(), $client, $config)
72+
);
73+
}
5374
}

tests/Support/ApplicationFactory.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,17 @@ public static function isHorizonAvailable()
184184
return class_exists('Laravel\Horizon\Horizon');
185185
}
186186

187+
/**
188+
* Determine whether the application supports the boot() method.
189+
*
190+
* @return bool True if any supported Laravel version OR Lumen 5.7+.
191+
*/
192+
public static function supportsBoot()
193+
{
194+
return ! static::isLumen()
195+
|| version_compare(static::getApplicationVersion(), '5.7', 'ge');
196+
}
197+
187198
/**
188199
* Get the fully-qualified class name of the RedisSentinelManager class
189200
* for the current version of Laravel or Lumen under test.

tests/Unit/Manager/VersionedManagerFactoryTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Monospice\LaravelRedisSentinel\Tests\Unit\Manager;
44

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

44-
45-
$this->subject = new VersionedManagerFactory($this->configLoaderMock);
45+
$this->subject = new VersionedManagerFactory(
46+
Mockery::mock(Container::class),
47+
$this->configLoaderMock
48+
);
4649
}
4750

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

86-
$manager = VersionedManagerFactory::make($this->configLoaderMock);
89+
$app = Mockery::mock(Container::class);
90+
$manager = VersionedManagerFactory::make($app, $this->configLoaderMock);
8791
$connection = $manager->connection('connection');
8892

8993
$this->assertInstanceOf(ManagerContract::class, $manager);

0 commit comments

Comments
 (0)