Completed
Pull Request — develop (#76)
by
unknown
02:41
created

AbstractEloquentTests::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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