Completed
Push — master ( 73c817...aaf0a0 )
by Nasrul Hazim
04:32
created

TestCaseTrait::assertHasHelper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace DummyNamespace\Tests\Traits;
4
5
use Illuminate\Support\Facades\Schema;
6
7
trait TestCaseTrait
8
{
9
    use SeedTrait;
10
11
    /**
12
     * Setup the test environment.
13
     */
14
    public function setUp(): void
15
    {
16
        parent::setUp();
17
        // $this->loadFactories();
18
        // $this->cleanUp();
19
        // $this->publish();
20
        // $this->clearCaches();
21
        // $this->migrate();
22
    }
23
24
    /**
25
     * Do clean up on tear down.
26
     */
27
    protected function tearDown(): void
28
    {
29
        // $this->cleanUp();
30
        parent::tearDown();
31
    }
32
33
    /**
34
     * Define environment setup.
35
     *
36
     * @param \Illuminate\Foundation\Application $app
37
     */
38
    protected function getEnvironmentSetUp($app)
39
    {
40
        // Setup default database to use sqlite :memory:
41
        $app['config']->set('database.default', 'testbench');
42
        $app['config']->set('database.connections.testbench', [
43
            'driver'   => 'sqlite',
44
            'database' => ':memory:',
45
            'prefix'   => '',
46
        ]);
47
    }
48
49
    /**
50
     * Load Factories
51
     * 
52
     * @return void
53
     */
54
    public function loadFactories()
55
    {
56
        $this->withFactories(realpath(__DIR__ . '/../factories'));
0 ignored issues
show
Bug introduced by
It seems like withFactories() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
        $this->/** @scrutinizer ignore-call */ 
57
               withFactories(realpath(__DIR__ . '/../factories'));
Loading history...
57
    }
58
59
    /**
60
     * Run all migrations.
61
     */
62
    public function migrate()
63
    {
64
        $this->loadLaravelMigrations(['--database' => 'testbench']);
0 ignored issues
show
Bug introduced by
It seems like loadLaravelMigrations() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        $this->/** @scrutinizer ignore-call */ 
65
               loadLaravelMigrations(['--database' => 'testbench']);
Loading history...
65
        $this->artisan('migrate', ['--database' => 'testbench']);
0 ignored issues
show
Bug introduced by
It seems like artisan() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        $this->/** @scrutinizer ignore-call */ 
66
               artisan('migrate', ['--database' => 'testbench']);
Loading history...
66
    }
67
68
    /**
69
     * Do test clean up.
70
     */
71
    public function cleanUp()
72
    {
73
        $this->removeMigrationFiles();
74
        $this->removeSeederFiles();
75
        // $this->removeIfExist(config_path('config.php'));
76
    }
77
78
    /**
79
     * Clear all caches.
80
     */
81
    public function clearCaches()
82
    {
83
        $this->artisan('config:clear');
84
    }
85
86
    /**
87
     * Republish assets.
88
     */
89
    public function republish()
90
    {
91
        $this->cleanUp();
92
        $this->clearCaches();
93
        $this->publish();
94
        $this->clearCaches();
95
    }
96
97
    /**
98
     * Remove all migration files if exist.
99
     */
100
    public function removeMigrationFiles()
101
    {
102
        collect(glob(database_path('migrations/*.php')))
103
            ->each(function ($path) {
104
                $this->removeIfExist($path);
105
            });
106
    }
107
108
    /**
109
     * Remove all seed files if exist.
110
     */
111
    public function removeSeederFiles()
112
    {
113
        collect(glob(database_path('seeds/*.php')))
114
            ->each(function ($path) {
115
                $this->removeIfExist($path);
116
            });
117
    }
118
119
    /**
120
     * Remove file if exists.
121
     *
122
     * @param string $path
123
     */
124
    public function removeIfExist($path)
125
    {
126
        if (file_exists($path)) {
127
            unlink($path);
128
        }
129
    }
130
131
    /**
132
     * Publish all package assets.
133
     */
134
    public function publish()
135
    {
136
        // $this->artisan('vendor:publish', [
137
        //     '--force' => true,
138
        //     '--tag'   => 'config',
139
        // ]);
140
    }
141
142
    /**
143
     * Truncate table.
144
     */
145
    public function truncateTable($table)
146
    {
147
        \DB::table($table)->truncate();
148
    }
149
150
    ////////////////////////////////////////////////////////
151
    ///////////////// Custom Assertions ////////////////////
152
    ////////////////////////////////////////////////////////
153
154
    /**
155
     * Assert the current database has table.
156
     *
157
     * @param string $table table name
158
     */
159
    protected function assertHasTable($table)
160
    {
161
        $this->assertTrue(Schema::hasTable($table));
0 ignored issues
show
Bug introduced by
It seems like assertTrue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

161
        $this->/** @scrutinizer ignore-call */ 
162
               assertTrue(Schema::hasTable($table));
Loading history...
162
    }
163
164
    /**
165
     * Assert the table has columns defined.
166
     *
167
     * @param string $table   table name
168
     * @param array  $columns list of columns
169
     */
170
    protected function assertTableHasColumns($table, $columns)
171
    {
172
        collect($columns)->each(function ($column) use ($table) {
173
            $this->assertTrue(Schema::hasColumn($table, $column));
174
        });
175
    }
176
177
    /**
178
     * Assert has helper.
179
     *
180
     * @param string $helper helper name
181
     */
182
    protected function assertHasHelper($helper)
183
    {
184
        $this->assertTrue(function_exists($helper));
185
    }
186
187
    /**
188
     * Assert has config.
189
     *
190
     * @param string $config config name
191
     */
192
    protected function assertHasConfig($config)
193
    {
194
        $this->assertFileExists(config_path($config . '.php'));
0 ignored issues
show
Bug introduced by
It seems like assertFileExists() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

194
        $this->/** @scrutinizer ignore-call */ 
195
               assertFileExists(config_path($config . '.php'));
Loading history...
195
    }
196
197
    /**
198
     * Assert has migration.
199
     *
200
     * @param string $migration migration name
201
     */
202
    protected function assertHasMigration($migration)
203
    {
204
        $this->assertHasClass($migration);
205
    }
206
207
    /**
208
     * Assert has class.
209
     *
210
     * @param string $class class name
211
     */
212
    protected function assertHasClass($class)
213
    {
214
        $this->assertTrue(class_exists($class));
215
    }
216
217
    /**
218
     * Assert has class method exist.
219
     *
220
     * @param string $object object
221
     * @param string $method method
222
     */
223
    protected function assertHasClassMethod($object, $method)
224
    {
225
        $this->assertTrue(method_exists($object, $method));
226
    }
227
}
228