Skip to content

Commit 2d54b65

Browse files
authored
Merge pull request #197 from wayofdev/feat/testing-helpers
2 parents 54f98b8 + f076f49 commit 2d54b65

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

src/Bridge/Laravel/Providers/CycleServiceProvider.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ public function boot(): void
4141

4242
public function register(): void
4343
{
44-
$this->mergeConfigFrom(
45-
__DIR__ . '/../../../../config/cycle.php',
46-
Registrator::CFG_KEY
47-
);
44+
// @phpstan-ignore-next-line
45+
if (! $this->app->configurationIsCached()) {
46+
$this->mergeConfigFrom(
47+
__DIR__ . '/../../../../config/cycle.php',
48+
Registrator::CFG_KEY
49+
);
50+
}
4851

4952
$registrators = [
5053
Registrators\RegisterConfigs::class,

src/Testing/Concerns/InteractsWithDatabase.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace WayOfDev\Cycle\Testing\Concerns;
66

77
use Cycle\Database\DatabaseProviderInterface;
8+
use Illuminate\Support\Facades\File;
89
use PHPUnit\Framework\Constraint\Constraint;
910
use PHPUnit\Framework\Constraint\LogicalNot as ReverseConstraint;
1011
use WayOfDev\Cycle\Testing\Constraints\CountInDatabase;
@@ -79,4 +80,12 @@ protected function assertDatabaseEmpty($table, $connection = null): static
7980

8081
return $this;
8182
}
83+
84+
protected function cleanupMigrations(string $pathGlob): void
85+
{
86+
$files = File::glob($pathGlob);
87+
foreach ($files as $file) {
88+
File::delete($file);
89+
}
90+
}
8291
}

tests/src/TestCase.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
use Faker\Factory as FakerFactory;
88
use Faker\Generator;
99
use Illuminate\Contracts\Console\Kernel;
10-
use Illuminate\Database\Eloquent\Factories\Factory;
1110
use Illuminate\Support\Facades\Artisan;
12-
use Illuminate\Support\Facades\File;
1311
use Orchestra\Testbench\TestCase as OrchestraTestCase;
1412
use Spatie\LaravelRay\RayServiceProvider;
1513
use WayOfDev\Cycle\Bridge\Laravel\Providers\CycleServiceProvider;
@@ -29,6 +27,8 @@ class TestCase extends OrchestraTestCase
2927
use InteractsWithDatabase;
3028
use RefreshDatabase;
3129

30+
protected ?string $migrationsPath = null;
31+
3232
final protected static function faker(string $locale = 'en_US'): Generator
3333
{
3434
/** @var array<string, Generator> $fakers */
@@ -45,27 +45,24 @@ protected function setUp(): void
4545
{
4646
parent::setUp();
4747

48-
$this->cleanupMigrations();
48+
$this->migrationsPath = __DIR__ . '/../app/database/migrations/cycle';
49+
$this->cleanupMigrations($this->migrationsPath . '/*.php');
4950
$this->refreshDatabase();
5051

51-
Factory::guessFactoryNamesUsing(
52-
static fn (string $modelName) => 'WayOfDev\\Laravel\\Cycle\\Database\\Factories\\' . class_basename($modelName) . 'Factory'
53-
);
54-
5552
if (app()->environment() === 'testing') {
5653
config()->set([
5754
'cycle.tokenizer.directories' => array_merge(
5855
config('cycle.tokenizer.directories'),
5956
[__DIR__ . '/../app/Entities']
6057
),
61-
'cycle.migrations.directory' => __DIR__ . '/../app/database/migrations/cycle',
58+
'cycle.migrations.directory' => $this->migrationsPath,
6259
]);
6360
}
6461
}
6562

6663
protected function tearDown(): void
6764
{
68-
$this->cleanupMigrations();
65+
$this->cleanupMigrations($this->migrationsPath . '/*.php');
6966
$this->refreshDatabase();
7067

7168
parent::tearDown();
@@ -105,14 +102,4 @@ protected function getPackageProviders($app): array
105102
RayServiceProvider::class,
106103
];
107104
}
108-
109-
protected function cleanupMigrations(): void
110-
{
111-
$path = __DIR__ . '/../app/database/migrations/cycle/*.php';
112-
113-
$files = File::glob($path);
114-
foreach ($files as $file) {
115-
File::delete($file);
116-
}
117-
}
118105
}

0 commit comments

Comments
 (0)