Skip to content

Commit

Permalink
[5.7] Clean up cache integration tests (#25545)
Browse files Browse the repository at this point in the history
* Split cache lock tests for Memcached and Redis

The previous setup incorrectly skipped even the Redis tests when
the Memcached extension was not installed. By separating them, we
can also separate the logic for skipping these tests.

* Redis cache lock tests: Use normal setup() and tearDown()

* Memcached cache lock tests: Test for PHP extension, not class

* Memcached cache lock tests: Also skip if no connection can be established
  • Loading branch information
franzliedke authored and taylorotwell committed Sep 10, 2018
1 parent f12df3e commit bdd0113
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,33 @@
use Illuminate\Support\Carbon;
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Foundation\Testing\Concerns\InteractsWithRedis;

/**
* @group integration
*/
class CacheLockTest extends TestCase
class MemcachedCacheLockTest extends TestCase
{
use InteractsWithRedis;

public function setUp()
{
parent::setUp();

if (! class_exists(Memcached::class)) {
if (! extension_loaded('memcached')) {
$this->markTestSkipped('Memcached module not installed');
}

// Determine whether there is a running Memcached instance
$testConnection = new Memcached;
$testConnection->addServer(
env('MEMCACHED_HOST', '127.0.0.1'),
env('MEMCACHED_PORT', 11211)
);
$testConnection->getVersion();

if ($testConnection->getResultCode() > Memcached::RES_SUCCESS) {
$this->markTestSkipped('Memcached could not establish a connection');
}

$testConnection->quit();
}

public function test_memcached_locks_can_be_acquired_and_released()
Expand All @@ -35,19 +46,6 @@ public function test_memcached_locks_can_be_acquired_and_released()
Cache::store('memcached')->lock('foo')->release();
}

public function test_redis_locks_can_be_acquired_and_released()
{
$this->ifRedisAvailable(function () {
Cache::store('redis')->lock('foo')->release();
$this->assertTrue(Cache::store('redis')->lock('foo', 10)->get());
$this->assertFalse(Cache::store('redis')->lock('foo', 10)->get());
Cache::store('redis')->lock('foo')->release();
$this->assertTrue(Cache::store('redis')->lock('foo', 10)->get());
$this->assertFalse(Cache::store('redis')->lock('foo', 10)->get());
Cache::store('redis')->lock('foo')->release();
});
}

public function test_memcached_locks_can_block_for_seconds()
{
Carbon::setTestNow();
Expand All @@ -61,21 +59,6 @@ public function test_memcached_locks_can_block_for_seconds()
$this->assertTrue(Cache::store('memcached')->lock('foo', 10)->block(1));
}

public function test_redis_locks_can_block_for_seconds()
{
$this->ifRedisAvailable(function () {
Carbon::setTestNow();

Cache::store('redis')->lock('foo')->release();
$this->assertEquals('taylor', Cache::store('redis')->lock('foo', 10)->block(1, function () {
return 'taylor';
}));

Cache::store('redis')->lock('foo')->release();
$this->assertTrue(Cache::store('redis')->lock('foo', 10)->block(1));
});
}

public function test_locks_can_run_callbacks()
{
Cache::store('memcached')->lock('foo')->release();
Expand Down
54 changes: 54 additions & 0 deletions tests/Integration/Cache/RedisCacheLockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Illuminate\Tests\Integration\Cache;

use Illuminate\Support\Carbon;
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Foundation\Testing\Concerns\InteractsWithRedis;

/**
* @group integration
*/
class RedisCacheLockTest extends TestCase
{
use InteractsWithRedis;

public function setUp()
{
parent::setUp();

$this->setUpRedis();
}

public function tearDown()
{
parent::tearDown();

$this->tearDownRedis();
}

public function test_redis_locks_can_be_acquired_and_released()
{
Cache::store('redis')->lock('foo')->release();
$this->assertTrue(Cache::store('redis')->lock('foo', 10)->get());
$this->assertFalse(Cache::store('redis')->lock('foo', 10)->get());
Cache::store('redis')->lock('foo')->release();
$this->assertTrue(Cache::store('redis')->lock('foo', 10)->get());
$this->assertFalse(Cache::store('redis')->lock('foo', 10)->get());
Cache::store('redis')->lock('foo')->release();
}

public function test_redis_locks_can_block_for_seconds()
{
Carbon::setTestNow();

Cache::store('redis')->lock('foo')->release();
$this->assertEquals('taylor', Cache::store('redis')->lock('foo', 10)->block(1, function () {
return 'taylor';
}));

Cache::store('redis')->lock('foo')->release();
$this->assertTrue(Cache::store('redis')->lock('foo', 10)->block(1));
}
}

0 comments on commit bdd0113

Please sign in to comment.