cleaniquecoders /
packager
| 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 | * Load Factories. |
||||||
| 35 | * |
||||||
| 36 | * @return void |
||||||
| 37 | */ |
||||||
| 38 | public function loadFactories() |
||||||
| 39 | { |
||||||
| 40 | $this->withFactories(realpath(__DIR__ . '/../factories')); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 41 | } |
||||||
| 42 | |||||||
| 43 | /** |
||||||
| 44 | * Run all migrations. |
||||||
| 45 | */ |
||||||
| 46 | public function migrate() |
||||||
| 47 | { |
||||||
| 48 | $this->loadLaravelMigrations(['--database' => 'testbench']); |
||||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||||
| 49 | $this->artisan('migrate', ['--database' => 'testbench']); |
||||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||||
| 50 | } |
||||||
| 51 | |||||||
| 52 | /** |
||||||
| 53 | * Do test clean up. |
||||||
| 54 | */ |
||||||
| 55 | public function cleanUp() |
||||||
| 56 | { |
||||||
| 57 | $this->removeMigrationFiles(); |
||||||
| 58 | $this->removeSeederFiles(); |
||||||
| 59 | // $this->removeIfExist(config_path('config.php')); |
||||||
| 60 | } |
||||||
| 61 | |||||||
| 62 | /** |
||||||
| 63 | * Clear all caches. |
||||||
| 64 | */ |
||||||
| 65 | public function clearCaches() |
||||||
| 66 | { |
||||||
| 67 | $this->artisan('config:clear'); |
||||||
| 68 | } |
||||||
| 69 | |||||||
| 70 | /** |
||||||
| 71 | * Republish assets. |
||||||
| 72 | */ |
||||||
| 73 | public function republish() |
||||||
| 74 | { |
||||||
| 75 | $this->cleanUp(); |
||||||
| 76 | $this->clearCaches(); |
||||||
| 77 | $this->publish(); |
||||||
| 78 | $this->clearCaches(); |
||||||
| 79 | } |
||||||
| 80 | |||||||
| 81 | /** |
||||||
| 82 | * Remove all migration files if exist. |
||||||
| 83 | */ |
||||||
| 84 | public function removeMigrationFiles() |
||||||
| 85 | { |
||||||
| 86 | collect(glob(database_path('migrations/*.php'))) |
||||||
| 87 | ->each(function ($path) { |
||||||
| 88 | $this->removeIfExist($path); |
||||||
| 89 | }); |
||||||
| 90 | } |
||||||
| 91 | |||||||
| 92 | /** |
||||||
| 93 | * Remove all seed files if exist. |
||||||
| 94 | */ |
||||||
| 95 | public function removeSeederFiles() |
||||||
| 96 | { |
||||||
| 97 | collect(glob(database_path('seeds/*.php'))) |
||||||
| 98 | ->each(function ($path) { |
||||||
| 99 | $this->removeIfExist($path); |
||||||
| 100 | }); |
||||||
| 101 | } |
||||||
| 102 | |||||||
| 103 | /** |
||||||
| 104 | * Remove file if exists. |
||||||
| 105 | * |
||||||
| 106 | * @param string $path |
||||||
| 107 | */ |
||||||
| 108 | public function removeIfExist($path) |
||||||
| 109 | { |
||||||
| 110 | if (file_exists($path)) { |
||||||
| 111 | unlink($path); |
||||||
| 112 | } |
||||||
| 113 | } |
||||||
| 114 | |||||||
| 115 | /** |
||||||
| 116 | * Publish all package assets. |
||||||
| 117 | */ |
||||||
| 118 | public function publish() |
||||||
| 119 | { |
||||||
| 120 | // $this->artisan('vendor:publish', [ |
||||||
| 121 | // '--force' => true, |
||||||
| 122 | // '--tag' => 'config', |
||||||
| 123 | // ]); |
||||||
| 124 | } |
||||||
| 125 | |||||||
| 126 | /** |
||||||
| 127 | * Truncate table. |
||||||
| 128 | */ |
||||||
| 129 | public function truncateTable($table) |
||||||
| 130 | { |
||||||
| 131 | \DB::table($table)->truncate(); |
||||||
| 132 | } |
||||||
| 133 | |||||||
| 134 | /** |
||||||
| 135 | * Define environment setup. |
||||||
| 136 | * |
||||||
| 137 | * @param \Illuminate\Foundation\Application $app |
||||||
| 138 | */ |
||||||
| 139 | protected function getEnvironmentSetUp($app) |
||||||
| 140 | { |
||||||
| 141 | // Setup default database to use sqlite :memory: |
||||||
| 142 | $app['config']->set('database.default', 'testbench'); |
||||||
| 143 | $app['config']->set('database.connections.testbench', [ |
||||||
| 144 | 'driver' => 'sqlite', |
||||||
| 145 | 'database' => ':memory:', |
||||||
| 146 | 'prefix' => '', |
||||||
| 147 | ]); |
||||||
| 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
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
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
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
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 |