Completed
Pull Request — develop (#76)
by
unknown
01:55
created

AbstractEloquentTests   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 141
wmc 9
lcom 1
cbo 13
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A setupContainer() 0 19 1
A setupDatabase() 0 10 1
A migrate() 0 18 1
A seed() 0 15 1
A schema() 0 4 1
A tearDown() 0 6 1
A userRepository() 0 5 1
A postRepository() 0 5 1
1
<?php
2
3
use Illuminate\Database\Capsule\Manager;
4
use Illuminate\Database\Eloquent\Model;
5
use Rinvex\Tests\Stubs\EloquentPost;
6
use Rinvex\Tests\Stubs\EloquentPostRepository;
7
use Rinvex\Tests\Stubs\EloquentUser;
8
use Rinvex\Tests\Stubs\EloquentUserRepository;
9
10
abstract class AbstractEloquentTests extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
11
{
12
    use \Illuminate\Support\Traits\CapsuleManagerTrait;
13
14
    /**
15
     * @var \Illuminate\Container\Container
16
     */
17
    protected $container;
18
19
    /**
20
     * Setup the database schema.
21
     *
22
     * @return void
23
     */
24
    public function setUp()
25
    {
26
        $this->setupContainer();
27
        $this->setupDatabase(new Manager($this->getContainer()));
0 ignored issues
show
Documentation introduced by
$this->getContainer() is of type object<Illuminate\Contracts\Container\Container>, but the function expects a null|object<Illuminate\Container\Container>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
28
        $this->migrate();
29
        $this->seed();
30
    }
31
32
    /**
33
     * Setup the IoC container instance.
34
     *
35
     */
36
    protected function setupContainer()
37
    {
38
        $config = [
39
            'models' => 'Models',
40
            'cache' => [
41
                'keys_file' => '',
42
                'lifetime' => 0,
43
                'clear_on' => [
44
                    'create',
45
                    'update',
46
                    'delete',
47
                ],
48
                'skip_uri' => 'skipCache'
49
            ]
50
        ];
51
        $this->container = new \Illuminate\Container\Container();
52
        $this->container->instance('config', new \Illuminate\Config\Repository());
53
        $this->getContainer()['config']->offsetSet('rinvex.repository', $config);
54
    }
55
56
    protected function setupDatabase(Manager $db)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $db. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
57
    {
58
        $db->addConnection([
59
            'driver'    => 'sqlite',
60
            'database'  => ':memory:',
61
        ]);
62
63
        $db->bootEloquent();
64
        $db->setAsGlobal();
65
    }
66
67
    /**
68
     * Create tables
69
     *
70
     * @return void
71
     */
72
    protected function migrate()
73
    {
74
        $this->schema()->create('users', function ($table) {
75
            $table->increments('id');
76
            $table->string('name')->nullable();
77
            $table->string('email');
78
            $table->timestamps();
79
        });
80
81
        $this->schema()->create('posts', function ($table) {
82
            $table->increments('id');
83
            $table->integer('user_id');
84
            $table->integer('parent_id')->nullable();
85
            $table->string('name');
86
            $table->timestamps();
87
        });
88
89
    }
90
91
    /**
92
     * Create test users and posts
93
     */
94
    protected function seed()
95
    {
96
        $evsign = EloquentUser::create(['name' => 'evsign', 'email' => '[email protected]']);
97
        $omranic = EloquentUser::create(['name' => 'omranic', 'email' => '[email protected]']);
98
99
        $evsign->posts()->saveMany([
100
            new EloquentPost(['name' => 'first post']),
101
            new EloquentPost(['name' => 'second post']),
102
        ]);
103
104
        $omranic->posts()->saveMany([
105
            new EloquentPost(['name' => 'third post']),
106
            new EloquentPost(['name' => 'fourth post']),
107
        ]);
108
    }
109
110
    /**
111
     * Get Schema Builder
112
     *
113
     * @return \Illuminate\Database\Schema\Builder
114
     */
115
    protected function schema()
116
    {
117
        return Model::resolveConnection()->getSchemaBuilder();
118
    }
119
120
    /**
121
     * Tear down the database schema.
122
     *
123
     * @return void
124
     */
125
    public function tearDown()
126
    {
127
        $this->schema()->drop('users');
128
        $this->schema()->drop('posts');
129
        unset($this->container);
130
    }
131
132
    /**
133
     * @return EloquentUserRepository
134
     */
135
    protected function userRepository()
136
    {
137
        return (new EloquentUserRepository())
138
            ->setContainer($this->getContainer());
139
    }
140
141
    /**
142
     * @return EloquentPostRepository
143
     */
144
    protected function postRepository()
145
    {
146
        return (new EloquentPostRepository())
147
            ->setContainer(new \Illuminate\Container\Container());
148
    }
149
150
}