Completed
Push — develop ( f34a65...a0b033 )
by Abdelrahman
02:30
created

AbstractEloquentTests::migrate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 12
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->timestamps();
78
        });
79
80
        $this->schema()->create('posts', function ($table) {
81
            $table->increments('id');
82
            $table->integer('user_id');
83
            $table->integer('parent_id')->nullable();
84
            $table->string('name');
85
            $table->timestamps();
86
        });
87
    }
88
89
    /**
90
     * Create test users and posts.
91
     */
92
    protected function seed()
93
    {
94
        $evsign  = EloquentUser::create(['name' => 'evsign', 'email' => '[email protected]']);
95
        $omranic = EloquentUser::create(['name' => 'omranic', 'email' => '[email protected]']);
96
97
        $evsign->posts()->saveMany([
98
            new EloquentPost(['name' => 'first post']),
99
            new EloquentPost(['name' => 'second post']),
100
        ]);
101
102
        $omranic->posts()->saveMany([
103
            new EloquentPost(['name' => 'third post']),
104
            new EloquentPost(['name' => 'fourth post']),
105
        ]);
106
    }
107
108
    /**
109
     * Get Schema Builder.
110
     *
111
     * @return \Illuminate\Database\Schema\Builder
112
     */
113
    protected function schema()
114
    {
115
        return Model::resolveConnection()->getSchemaBuilder();
116
    }
117
118
    /**
119
     * Tear down the database schema.
120
     *
121
     * @return void
122
     */
123
    public function tearDown()
124
    {
125
        $this->schema()->drop('users');
126
        $this->schema()->drop('posts');
127
        unset($this->container);
128
    }
129
130
    /**
131
     * @return EloquentUserRepository
132
     */
133
    protected function userRepository()
134
    {
135
        return (new EloquentUserRepository())
136
            ->setContainer($this->getContainer());
137
    }
138
139
    /**
140
     * @return EloquentPostRepository
141
     */
142
    protected function postRepository()
143
    {
144
        return (new EloquentPostRepository())
145
            ->setContainer(new \Illuminate\Container\Container());
146
    }
147
}
148