Completed
Pull Request — develop (#89)
by Ionut
02:19
created

AbstractEloquentTests::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
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
    protected function setupContainer()
36
    {
37
        $config = [
38
            'models' => 'Models',
39
            'cache'  => [
40
                'keys_file' => '',
41
                'lifetime'  => 0,
42
                'clear_on'  => [
43
                    'create',
44
                    'update',
45
                    'delete',
46
                ],
47
                'skip_uri' => 'skipCache',
48
            ],
49
        ];
50
        $this->container = new \Illuminate\Container\Container();
51
        $this->container->instance('config', new \Illuminate\Config\Repository());
52
        $this->getContainer()['config']->offsetSet('rinvex.repository', $config);
53
    }
54
55
    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...
56
    {
57
        $db->addConnection([
58
            'driver'    => 'sqlite',
59
            'database'  => ':memory:',
60
        ]);
61
62
        $db->bootEloquent();
63
        $db->setAsGlobal();
64
    }
65
66
    /**
67
     * Create tables.
68
     *
69
     * @return void
70
     */
71
    protected function migrate()
72
    {
73
        $this->schema()->create('users', function ($table) {
74
            $table->increments('id');
75
            $table->string('name')->nullable();
76
            $table->string('email');
77
            $table->integer('age');
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
     * Create test users and posts.
92
     */
93
    protected function seed()
94
    {
95
        $evsign  = EloquentUser::create(['name' => 'evsign', 'email' => '[email protected]', 'age' => '25']);
96
        $omranic = EloquentUser::create(['name' => 'omranic', 'email' => '[email protected]', 'age' => '26']);
97
        $ionut = EloquentUser::create(['name' => 'ionut', 'email' => '[email protected]', 'age' => '24']);
98
        $anotherIonut = EloquentUser::create(['name' => 'ionut', 'email' => '[email protected]', 'age' => '28']);
99
100
        $evsign->posts()->saveMany([
101
            new EloquentPost(['name' => 'first post']),
102
            new EloquentPost(['name' => 'second post']),
103
        ]);
104
105
        $omranic->posts()->saveMany([
106
            new EloquentPost(['name' => 'third post']),
107
            new EloquentPost(['name' => 'fourth post']),
108
        ]);
109
110
        $ionut->posts()->saveMany([
111
            new EloquentPost(['name' => 'fifth post']),
112
            new EloquentPost(['name' => 'sixth post']),
113
        ]);
114
115
        $anotherIonut->posts()->saveMany([
116
            new EloquentPost(['name' => 'seventh post']),
117
            new EloquentPost(['name' => 'eighth post']),
118
        ]);
119
    }
120
121
    /**
122
     * Get Schema Builder.
123
     *
124
     * @return \Illuminate\Database\Schema\Builder
125
     */
126
    protected function schema()
127
    {
128
        return Model::resolveConnection()->getSchemaBuilder();
129
    }
130
131
    /**
132
     * Tear down the database schema.
133
     *
134
     * @return void
135
     */
136
    public function tearDown()
137
    {
138
        $this->schema()->drop('users');
139
        $this->schema()->drop('posts');
140
        unset($this->container);
141
    }
142
143
    /**
144
     * @return EloquentUserRepository
145
     */
146
    protected function userRepository()
147
    {
148
        return (new EloquentUserRepository())
149
            ->setContainer($this->getContainer());
150
    }
151
152
    /**
153
     * @return EloquentPostRepository
154
     */
155
    protected function postRepository()
156
    {
157
        return (new EloquentPostRepository())
158
            ->setContainer(new \Illuminate\Container\Container());
159
    }
160
}
161