Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/redis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jobs:
- name: Execute Cache tests
run: vendor/bin/phpunit tests/Integration/Cache
env:
CACHE_STORE: redis
REDIS_CACHE_CONNECTION: cache
REDIS_CACHE_LOCK_CONNECTION: cache
REDIS_CLIENT: ${{ matrix.client }}
Expand Down Expand Up @@ -117,6 +118,7 @@ jobs:
- name: Execute Cache tests
run: vendor/bin/phpunit tests/Integration/Cache
env:
CACHE_STORE: redis
REDIS_CACHE_CONNECTION: default
REDIS_CACHE_LOCK_CONNECTION: default
REDIS_CLIENT: ${{ matrix.client }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<?php

namespace Illuminate\Tests\Cache;
namespace Illuminate\Tests\Integration\Cache;

use Illuminate\Cache\RateLimiter;
use Illuminate\Cache\RedisStore;
use Illuminate\Cache\Repository;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\Concerns\InteractsWithRedis;
use Illuminate\Redis\RedisManager;
Expand All @@ -16,14 +13,19 @@
use Redis;

#[RequiresPhpExtension('redis')]
class RedisCacheIntegrationTest extends TestCase
class PhpRedisBackoffTest extends TestCase
{
use InteractsWithRedis;

protected function setUp(): void
{
parent::setUp();
$this->setUpRedis();

$client = $this->redis['phpredis']->connection()->client();
if (! $client instanceof Redis) {
$this->markTestSkipped('Backoff option is only supported with phpredis in non-cluster mode');
}
}

protected function tearDown(): void
Expand All @@ -32,64 +34,6 @@ protected function tearDown(): void
$this->tearDownRedis();
}

/**
* @param string $driver
*/
#[DataProvider('redisDriverProvider')]
public function testRedisCacheAddTwice($driver)
{
$store = new RedisStore($this->redis[$driver]);
$repository = new Repository($store);
$this->assertTrue($repository->add('k', 'v', 3600));
$this->assertFalse($repository->add('k', 'v', 3600));
$this->assertGreaterThan(3500, $this->redis[$driver]->connection()->ttl('k'));
}

/**
* @param string $driver
*/
#[DataProvider('redisDriverProvider')]
public function testRedisCacheRateLimiter($driver)
{
$store = new RedisStore($this->redis[$driver]);
$repository = new Repository($store);
$rateLimiter = new RateLimiter($repository);

$this->assertFalse($rateLimiter->tooManyAttempts('key', 1));
$this->assertEquals(1, $rateLimiter->hit('key', 60));
$this->assertTrue($rateLimiter->tooManyAttempts('key', 1));
$this->assertFalse($rateLimiter->tooManyAttempts('key', 2));
}

/**
* Breaking change.
*
* @param string $driver
*/
#[DataProvider('redisDriverProvider')]
public function testRedisCacheAddFalse($driver)
{
$store = new RedisStore($this->redis[$driver]);
$repository = new Repository($store);
$repository->forever('k', false);
$this->assertFalse($repository->add('k', 'v', 60));
$this->assertEquals(-1, $this->redis[$driver]->connection()->ttl('k'));
}

/**
* Breaking change.
*
* @param string $driver
*/
#[DataProvider('redisDriverProvider')]
public function testRedisCacheAddNull($driver)
{
$store = new RedisStore($this->redis[$driver]);
$repository = new Repository($store);
$repository->forever('k', null);
$this->assertFalse($repository->add('k', 'v', 60));
}

#[DataProvider('phpRedisBackoffAlgorithmsProvider')]
public function testPhpRedisBackoffAlgorithmParsing($friendlyAlgorithmName, $expectedAlgorithm)
{
Expand Down
87 changes: 87 additions & 0 deletions tests/Integration/Cache/RedisCacheIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Illuminate\Tests\Integration\Cache;

use Illuminate\Cache\RateLimiter;
use Illuminate\Cache\RedisStore;
use Illuminate\Cache\Repository;
use Illuminate\Foundation\Testing\Concerns\InteractsWithRedis;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\TestCase;

#[RequiresPhpExtension('redis')]
class RedisCacheIntegrationTest extends TestCase
{
use InteractsWithRedis;

protected function setUp(): void
{
parent::setUp();
$this->setUpRedis();
}

protected function tearDown(): void
{
parent::tearDown();
$this->tearDownRedis();
}

/**
* @param string $driver
*/
#[DataProvider('redisDriverProvider')]
public function testRedisCacheAddTwice($driver)
{
$store = new RedisStore($this->redis[$driver]);
$repository = new Repository($store);
$this->assertTrue($repository->add('k', 'v', 3600));
$this->assertFalse($repository->add('k', 'v', 3600));
$this->assertGreaterThan(3500, $this->redis[$driver]->connection()->ttl('k'));
}

/**
* @param string $driver
*/
#[DataProvider('redisDriverProvider')]
public function testRedisCacheRateLimiter($driver)
{
$store = new RedisStore($this->redis[$driver]);
$repository = new Repository($store);
$rateLimiter = new RateLimiter($repository);

$this->assertFalse($rateLimiter->tooManyAttempts('key', 1));
$this->assertEquals(1, $rateLimiter->hit('key', 60));
$this->assertTrue($rateLimiter->tooManyAttempts('key', 1));
$this->assertFalse($rateLimiter->tooManyAttempts('key', 2));
}

/**
* Breaking change.
*
* @param string $driver
*/
#[DataProvider('redisDriverProvider')]
public function testRedisCacheAddFalse($driver)
{
$store = new RedisStore($this->redis[$driver]);
$repository = new Repository($store);
$repository->forever('k', false);
$this->assertFalse($repository->add('k', 'v', 60));
$this->assertEquals(-1, $this->redis[$driver]->connection()->ttl('k'));
}

/**
* Breaking change.
*
* @param string $driver
*/
#[DataProvider('redisDriverProvider')]
public function testRedisCacheAddNull($driver)
{
$store = new RedisStore($this->redis[$driver]);
$repository = new Repository($store);
$repository->forever('k', null);
$this->assertFalse($repository->add('k', 'v', 60));
}
}