Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Foundation\Testing\Concerns;
4
 
5
use Exception;
6
use Illuminate\Foundation\Application;
7
use Illuminate\Redis\RedisManager;
8
use Illuminate\Support\Env;
9
 
10
trait InteractsWithRedis
11
{
12
    /**
13
     * Indicate connection failed if redis is not available.
14
     *
15
     * @var bool
16
     */
17
    private static $connectionFailedOnceWithDefaultsSkip = false;
18
 
19
    /**
20
     * Redis manager instance.
21
     *
22
     * @var \Illuminate\Redis\RedisManager[]
23
     */
24
    private $redis;
25
 
26
    /**
27
     * Setup redis connection.
28
     *
29
     * @return void
30
     */
31
    public function setUpRedis()
32
    {
33
        if (! extension_loaded('redis')) {
34
            $this->markTestSkipped('The redis extension is not installed. Please install the extension to enable '.__CLASS__);
35
        }
36
 
37
        if (static::$connectionFailedOnceWithDefaultsSkip) {
38
            $this->markTestSkipped('Trying default host/port failed, please set environment variable REDIS_HOST & REDIS_PORT to enable '.__CLASS__);
39
        }
40
 
41
        $app = $this->app ?? new Application;
42
        $host = Env::get('REDIS_HOST', '127.0.0.1');
43
        $port = Env::get('REDIS_PORT', 6379);
44
 
45
        foreach ($this->redisDriverProvider() as $driver) {
46
            $this->redis[$driver[0]] = new RedisManager($app, $driver[0], [
47
                'cluster' => false,
48
                'options' => [
49
                    'prefix' => 'test_',
50
                ],
51
                'default' => [
52
                    'host' => $host,
53
                    'port' => $port,
54
                    'database' => 5,
55
                    'timeout' => 0.5,
56
                    'name' => 'default',
57
                ],
58
            ]);
59
        }
60
 
61
        try {
62
            $this->redis['phpredis']->connection()->flushdb();
63
        } catch (Exception $e) {
64
            if ($host === '127.0.0.1' && $port === 6379 && Env::get('REDIS_HOST') === null) {
65
                static::$connectionFailedOnceWithDefaultsSkip = true;
66
 
67
                $this->markTestSkipped('Trying default host/port failed, please set environment variable REDIS_HOST & REDIS_PORT to enable '.__CLASS__);
68
            }
69
        }
70
    }
71
 
72
    /**
73
     * Teardown redis connection.
74
     *
75
     * @return void
76
     */
77
    public function tearDownRedis()
78
    {
79
        if (isset($this->redis['phpredis'])) {
80
            $this->redis['phpredis']->connection()->flushdb();
81
        }
82
 
83
        foreach ($this->redisDriverProvider() as $driver) {
84
            if (isset($this->redis[$driver[0]])) {
85
                $this->redis[$driver[0]]->connection()->disconnect();
86
            }
87
        }
88
    }
89
 
90
    /**
91
     * Get redis driver provider.
92
     *
93
     * @return array
94
     */
95
    public function redisDriverProvider()
96
    {
97
        return [
98
            ['predis'],
99
            ['phpredis'],
100
        ];
101
    }
102
 
103
    /**
104
     * Run test if redis is available.
105
     *
106
     * @param  callable  $callback
107
     * @return void
108
     */
109
    public function ifRedisAvailable($callback)
110
    {
111
        $this->setUpRedis();
112
 
113
        $callback();
114
 
115
        $this->tearDownRedis();
116
    }
117
}