Completed
Pull Request — master (#22)
by Sergey
20:45 queued 05:44
created

BaseTestCase   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 73
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A loadDotenv() 0 9 2
A getEnvironmentSetUp() 0 11 1
A sleep() 0 4 1
A deleteIndex() 0 7 2
1
<?php
2
3
namespace Isswp101\Persimmon\Test;
4
5
use Dotenv\Dotenv;
6
use Dotenv\Exception\InvalidPathException;
7
use Elasticsearch\Client;
8
use Elasticsearch\Common\Exceptions\Missing404Exception;
9
use Orchestra\Testbench\TestCase;
10
use Shift31\LaravelElasticsearch\ElasticsearchServiceProvider;
11
12
class BaseTestCase extends TestCase
13
{
14
    /**
15
     * @var Client
16
     */
17
    protected $es;
18
19
    /**
20
     * Setup the test environment.
21
     */
22
    public function setUp()
23
    {
24
        $this->loadDotenv();
25
26
        parent::setUp();
27
28
        $this->es = app(Client::class);
29
    }
30
31
    /**
32
     * Load Dotenv.
33
     */
34
    protected function loadDotenv()
35
    {
36
        $dotenv = new Dotenv(__DIR__);
37
        try {
38
            $dotenv->load();
39
        } catch (InvalidPathException $e) {
40
            // It's workaround for Travis CI
41
        }
42
    }
43
44
    /**
45
     * Define environment setup.
46
     *
47
     * @param \Illuminate\Foundation\Application $app
48
     * @return void
49
     */
50
    protected function getEnvironmentSetUp($app)
51
    {
52
        $host = env('ELASTICSEARCH_AUTH_USER', '') . ':' .
53
            env('ELASTICSEARCH_AUTH_PASS', '') . '@' .
54
            env('ELASTICSEARCH_HOSTS', '');
55
56
        $app['config']->set('elasticsearch.hosts', [$host]);
57
58
        $elasticsearchServiceProvider = new ElasticsearchServiceProvider($app);
59
        $elasticsearchServiceProvider->register();
60
    }
61
62
    /**
63
     * Sleep.
64
     *
65
     * @param int $seconds
66
     */
67
    protected function sleep($seconds = 1)
68
    {
69
        sleep($seconds);
70
    }
71
72
    /**
73
     * Delete index.
74
     *
75
     * @param mixed $index
76
     */
77
    protected function deleteIndex($index)
78
    {
79
        try {
80
            $this->es->indices()->delete(['index' => $index]);
81
        } catch (Missing404Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
82
        }
83
    }
84
}
85