Skip to content

Commit 847333e

Browse files
committed
Split tests.
1 parent e15478a commit 847333e

File tree

10 files changed

+242
-130
lines changed

10 files changed

+242
-130
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ jobs:
6363
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
6464
6565
- name: Execute tests
66-
# we need to split the tests into two groups one for secure and one for insecure redis
67-
# run: vendor/bin/phpunit --exclude-group=redis-secure
68-
run: vendor/bin/phpunit --testdox
69-
66+
run: vendor/bin/phpunit --exclude-group=redis-secure
7067

7168
redis-with-password:
7269
runs-on: ${{ matrix.os }}
@@ -127,6 +124,4 @@ jobs:
127124
run: docker ps -a
128125

129126
- name: Execute tests
130-
# we need to split the tests into two groups one for secure and one for insecure redis
131-
# run: vendor/bin/phpunit --exclude-group=redis-insecure
132-
run: vendor/bin/phpunit --testdox
127+
run: vendor/bin/phpunit --exclude-group=redis-insecure

tests/Cache/ArrayCacheTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace BotMan\BotMan\Tests;
3+
namespace BotMan\BotMan\Tests\Cache;
44

55
use BotMan\BotMan\Cache\ArrayCache;
66
use PHPUnit\Framework\TestCase;

tests/Cache/CodeIgniterCacheTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace BotMan\BotMan\Tests;
3+
namespace BotMan\BotMan\Tests\Cache;
44

55
use BotMan\BotMan\Cache\CodeIgniterCache;
66
use CI_Cache;

tests/Cache/DoctrineCacheTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace BotMan\BotMan\Tests;
3+
namespace BotMan\BotMan\Tests\Cache;
44

55
use BotMan\BotMan\Cache\DoctrineCache;
66
use Doctrine\Common\Cache\CacheProvider;

tests/Cache/Psr6CacheTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace BotMan\BotMan\Tests;
3+
namespace BotMan\BotMan\Tests\Cache;
44

55
use BotMan\BotMan\Cache\Psr6Cache;
66
use Mockery as m;
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace BotMan\BotMan\Tests\Cache\RedisCache;
4+
5+
use BotMan\BotMan\Cache\ArrayCache;
6+
use BotMan\BotMan\Cache\RedisCache;
7+
use PHPUnit\Framework\TestCase;
8+
use Redis;
9+
use RedisException;
10+
11+
/**
12+
* @group integration
13+
* @group redis-insecure
14+
*/
15+
class InsecureTest extends TestCase
16+
{
17+
protected function setUp(): void
18+
{
19+
if (! extension_loaded('redis')) {
20+
$this->markTestSkipped('Redis extension required');
21+
}
22+
}
23+
24+
protected function tearDown(): void
25+
{
26+
$script = sprintf("for i, name in ipairs(redis.call('KEYS', '%s*')) do redis.call('DEL', name); end", RedisCache::KEY_PREFIX);
27+
28+
$redis = new Redis();
29+
$redis->connect($this->getRedisHost(), $this->getRedisPort());
30+
$redis->eval($script);
31+
$redis->close();
32+
}
33+
34+
/** @test */
35+
public function has()
36+
{
37+
$cache = new RedisCache($this->getRedisHost(), $this->getRedisPort());
38+
$cache->put('foo', 'bar', 1);
39+
static::assertTrue($cache->has('foo'));
40+
}
41+
42+
/** @test */
43+
public function has_not()
44+
{
45+
$cache = new RedisCache($this->getRedisHost(), $this->getRedisPort());
46+
static::assertFalse($cache->has('foo'));
47+
}
48+
49+
/** @test */
50+
public function get_existing_key()
51+
{
52+
$cache = new RedisCache($this->getRedisHost(), $this->getRedisPort());
53+
$cache->put('foo', 'bar', 5);
54+
static::assertTrue($cache->has('foo'));
55+
static::assertEquals('bar', $cache->get('foo'));
56+
}
57+
58+
/** @test */
59+
public function get_non_existing_key()
60+
{
61+
$cache = new RedisCache($this->getRedisHost(), $this->getRedisPort());
62+
static::assertNull($cache->get('foo'));
63+
}
64+
65+
/** @test */
66+
public function pull_existing_key()
67+
{
68+
$cache = new RedisCache($this->getRedisHost(), $this->getRedisPort());
69+
$cache->put('foo', 'bar', 5);
70+
static::assertTrue($cache->has('foo'));
71+
static::assertEquals('bar', $cache->pull('foo'));
72+
static::assertFalse($cache->has('foo'));
73+
static::assertNull($cache->get('foo'));
74+
}
75+
76+
/** @test */
77+
public function pull_non_existing_key()
78+
{
79+
$cache = new RedisCache($this->getRedisHost(), $this->getRedisPort());
80+
static::assertNull($cache->pull('foo'));
81+
}
82+
83+
/** @test */
84+
public function pull_non_existing_key_with_default_value()
85+
{
86+
$cache = new ArrayCache();
87+
static::assertEquals('bar', $cache->pull('foo', 'bar'));
88+
}
89+
90+
/**
91+
* Get redis host.
92+
*
93+
* @return string
94+
*/
95+
protected function getRedisHost()
96+
{
97+
return $_ENV['REDIS_HOST'] ?? '127.0.0.1';
98+
}
99+
100+
/**
101+
* Get redis port.
102+
*
103+
* @return int
104+
*/
105+
protected function getRedisPort()
106+
{
107+
return (int) ($_ENV['REDIS_PORT'] ?? 6380);
108+
}
109+
}
Lines changed: 17 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace BotMan\BotMan\Tests;
3+
namespace BotMan\BotMan\Tests\Cache\RedisCache;
44

55
use BotMan\BotMan\Cache\ArrayCache;
66
use BotMan\BotMan\Cache\RedisCache;
@@ -10,9 +10,9 @@
1010

1111
/**
1212
* @group integration
13-
* @group redis-auth
13+
* @group redis-secure
1414
*/
15-
class RedisCacheTest extends TestCase
15+
class SecureTest extends TestCase
1616
{
1717
protected function setUp(): void
1818
{
@@ -25,29 +25,17 @@ protected function tearDown(): void
2525
{
2626
$script = sprintf("for i, name in ipairs(redis.call('KEYS', '%s*')) do redis.call('DEL', name); end", RedisCache::KEY_PREFIX);
2727

28-
if (! $this->isSecure())
29-
{
30-
$redis = new Redis();
31-
$redis->connect($this->getRedisHost(), $this->getAuthRedisPort());
32-
$redis->eval($script);
33-
$redis->close();
34-
} else {
35-
$redis = new Redis();
36-
$redis->connect($this->getRedisHost(), $this->getAuthRedisPort());
37-
$redis->auth('secret');
38-
$redis->eval($script);
39-
$redis->close();
40-
}
28+
$redis = new Redis();
29+
$redis->connect($this->getRedisHost(), $this->getRedisPort());
30+
$redis->auth('secret');
31+
$redis->eval($script);
32+
$redis->close();
4133
}
4234

4335
/** @test */
4436
public function valid_auth()
4537
{
46-
if(! $this->isSecure()) {
47-
$this->markTestSkipped('This function needs a secure instance');
48-
}
49-
50-
$cache = new RedisCache($this->getRedisHost(), $this->getAuthRedisPort(), 'secret');
38+
$cache = new RedisCache($this->getRedisHost(), $this->getRedisPort(), 'secret');
5139
$cache->put('foo', 'bar', 1);
5240
static::assertTrue($cache->has('foo'));
5341
}
@@ -58,45 +46,29 @@ public function valid_auth()
5846
*/
5947
public function invalid_auth()
6048
{
61-
if(! $this->isSecure()) {
62-
$this->markTestSkipped('This function needs a secure instance');
63-
}
64-
65-
$cache = new RedisCache($this->getRedisHost(), $this->getAuthRedisPort(), 'invalid');
49+
$cache = new RedisCache($this->getRedisHost(), $this->getRedisPort(), 'invalid');
6650
$cache->put('foo', 'bar', 1);
6751
}
6852

6953
/** @test */
7054
public function has()
7155
{
72-
if($this->isSecure()) {
73-
$this->markTestSkipped('This function needs an insecure instance');
74-
}
75-
76-
$cache = new RedisCache($this->getRedisHost(), $this->getAuthRedisPort());
56+
$cache = new RedisCache($this->getRedisHost(), $this->getRedisPort(), 'secret');
7757
$cache->put('foo', 'bar', 1);
7858
static::assertTrue($cache->has('foo'));
7959
}
8060

8161
/** @test */
8262
public function has_not()
8363
{
84-
if($this->isSecure()) {
85-
$this->markTestSkipped('This function needs an insecure instance');
86-
}
87-
88-
$cache = new RedisCache($this->getRedisHost(), $this->getAuthRedisPort());
64+
$cache = new RedisCache($this->getRedisHost(), $this->getRedisPort(), 'secret');
8965
static::assertFalse($cache->has('foo'));
9066
}
9167

9268
/** @test */
9369
public function get_existing_key()
9470
{
95-
if($this->isSecure()) {
96-
$this->markTestSkipped('This function needs an insecure instance');
97-
}
98-
99-
$cache = new RedisCache($this->getRedisHost(), $this->getAuthRedisPort());
71+
$cache = new RedisCache($this->getRedisHost(), $this->getRedisPort(), 'secret');
10072
$cache->put('foo', 'bar', 5);
10173
static::assertTrue($cache->has('foo'));
10274
static::assertEquals('bar', $cache->get('foo'));
@@ -105,22 +77,15 @@ public function get_existing_key()
10577
/** @test */
10678
public function get_non_existing_key()
10779
{
108-
if($this->isSecure()) {
109-
$this->markTestSkipped('This function needs an insecure instance');
110-
}
111-
112-
$cache = new RedisCache($this->getRedisHost(), $this->getAuthRedisPort());
80+
$cache = new RedisCache($this->getRedisHost(), $this->getRedisPort(), 'secret');
11381
static::assertNull($cache->get('foo'));
11482
}
11583

11684
/** @test */
11785
public function pull_existing_key()
11886
{
119-
if($this->isSecure()) {
120-
$this->markTestSkipped('This function needs an insecure instance');
121-
}
12287

123-
$cache = new RedisCache($this->getRedisHost(), $this->getAuthRedisPort());
88+
$cache = new RedisCache($this->getRedisHost(), $this->getRedisPort(), 'secret');
12489
$cache->put('foo', 'bar', 5);
12590
static::assertTrue($cache->has('foo'));
12691
static::assertEquals('bar', $cache->pull('foo'));
@@ -131,11 +96,7 @@ public function pull_existing_key()
13196
/** @test */
13297
public function pull_non_existing_key()
13398
{
134-
if($this->isSecure()) {
135-
$this->markTestSkipped('This function needs an insecure instance');
136-
}
137-
138-
$cache = new RedisCache($this->getRedisHost(), $this->getAuthRedisPort());
99+
$cache = new RedisCache($this->getRedisHost(), $this->getRedisPort(), 'secret');
139100
static::assertNull($cache->pull('foo'));
140101
}
141102

@@ -161,18 +122,8 @@ protected function getRedisHost()
161122
*
162123
* @return int
163124
*/
164-
protected function getAuthRedisPort()
125+
protected function getRedisPort()
165126
{
166127
return (int) ($_ENV['REDIS_PORT'] ?? 6380);
167128
}
168-
169-
/**
170-
* is secure.
171-
*
172-
* @return int
173-
*/
174-
protected function isSecure()
175-
{
176-
return (bool) ($_ENV['REDIS_SECURE'] ?? false);
177-
}
178129
}

tests/Cache/SymfonyCacheTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace BotMan\BotMan\Tests;
3+
namespace BotMan\BotMan\Tests\Cache;
44

55
use BotMan\BotMan\Cache\SymfonyCache;
66
use Mockery as m;

0 commit comments

Comments
 (0)