diff --git a/README.md b/README.md index ac6470b..993b294 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,10 @@ Publish config using Artisan command: ```bash php artisan vendor:publish --tag=config --provider="FrittenKeeZ\Vouchers\VouchersServiceProvider" ``` +Publish migrations using Artisan command: +```bash +php artisan vendor:publish --tag=migrations --provider="FrittenKeeZ\Vouchers\VouchersServiceProvider" +``` Don't forget to run migrations: ```bash php artisan migrate diff --git a/composer.json b/composer.json index a395d9d..530bf2e 100644 --- a/composer.json +++ b/composer.json @@ -46,7 +46,7 @@ ], "psr-4": { "FrittenKeeZ\\Vouchers\\Tests\\": "tests", - "FrittenKeeZ\\Vouchers\\Tests\\Database\\Factories\\": "tests/database/factories" + "Database\\Factories\\FrittenKeeZ\\Vouchers\\Tests\\Models\\": "tests/database/factories" } }, "scripts": { diff --git a/migrations/2018_06_12_000000_create_voucher_tables.php b/publishes/migrations/2018_06_12_000000_create_voucher_tables.php similarity index 97% rename from migrations/2018_06_12_000000_create_voucher_tables.php rename to publishes/migrations/2018_06_12_000000_create_voucher_tables.php index 55f08ba..58fa7c0 100644 --- a/migrations/2018_06_12_000000_create_voucher_tables.php +++ b/publishes/migrations/2018_06_12_000000_create_voucher_tables.php @@ -7,7 +7,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateVoucherTables extends Migration +return new class extends Migration { /** * Run the migrations. @@ -68,4 +68,4 @@ public function down(): void Schema::dropIfExists(Config::table('redeemers')); Schema::dropIfExists(Config::table('vouchers')); } -} +}; diff --git a/publishes/migrations/2021_02_02_000000_add_owner_to_vouchers_table.php b/publishes/migrations/2021_02_02_000000_add_owner_to_vouchers_table.php deleted file mode 100644 index 730e9de..0000000 --- a/publishes/migrations/2021_02_02_000000_add_owner_to_vouchers_table.php +++ /dev/null @@ -1,31 +0,0 @@ -nullableMorphs('owner'); - }); - } - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - // We don't want to risk any data loss, so no reversal. - } -} diff --git a/publishes/migrations/2021_12_10_000000_add_primary_key_to_entities_table.php b/publishes/migrations/2021_12_10_000000_add_primary_key_to_entities_table.php deleted file mode 100644 index 22f724a..0000000 --- a/publishes/migrations/2021_12_10_000000_add_primary_key_to_entities_table.php +++ /dev/null @@ -1,31 +0,0 @@ -bigIncrements('id')->first(); - }); - } - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - // We don't want to risk any data loss, so no reversal. - } -} diff --git a/src/Console/Commands/MigrateCommand.php b/src/Console/Commands/MigrateCommand.php deleted file mode 100644 index 48265cf..0000000 --- a/src/Console/Commands/MigrateCommand.php +++ /dev/null @@ -1,180 +0,0 @@ -option('mode'); - $models = $this->getModels(); - - // Validate input. - $validator = Validator::make(compact('mode', 'models'), [ - 'mode' => ['required', 'in:auto,retain,delete'], - 'models' => ['required'], - ]); - - if ($validator->fails()) { - $this->info('Migration could not continue. See error messages below:'); - foreach ($validator->errors()->all() as $error) { - $this->error($error); - } - - return 1; - } - - // Output operation mode for good measure. - $this->info(sprintf('Database operation mode is set to: %s', $mode)); - - // Count number of vouchers available for migration. - $counts = collect($models)->mapWithKeys(function (string $class) { - return [$class => Voucher::withoutOwner()->withEntities($class)->count()]; - })->all(); - $rows = collect($counts)->map(function (int $count, string $class) { - return [$class, $count]; - })->values()->all(); - $this->table(['Model', 'Vouchers Count'], $rows); - - if (array_sum($counts) > 0 && $this->confirm('Do you wish to continue?', true)) { - foreach ($counts as $class => $count) { - $bar = $this->output->createProgressBar($count); - $bar->setFormat("%message%\n %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%"); - - $query = Voucher::withoutOwner()->withEntities($class); - $alias = (new $class())->getMorphClass(); - foreach ($query->cursor() as $voucher) { - $bar->setMessage(sprintf( - 'Migrating model %s - Voucher #%d', - $class, - $voucher->getKey() - )); - - DB::transaction(function () use ($voucher, $alias, $mode) { - $owner = $voucher->voucherEntities->first(function ($entity) use ($alias) { - return $entity->entity_type === $alias; - }); - // Set owner directly to prevent issues with deleted entities. - $voucher->owner_type = $owner->entity_type; - $voucher->owner_id = $owner->entity_id; - $voucher->save(); - - // Whether to delete the owner from entities. - if ($mode === 'delete' || $mode === 'auto' && $voucher->voucherEntities->count() === 1) { - $entities = $voucher->voucherEntities->reject(function ($entity) use ($owner) { - return $entity->entity_type === $owner->entity_type - && $entity->entity_id === $owner->entity_id; - }); - // Detach all existing entities. - $voucher->voucherEntities()->delete(); - if ($entities->isNotEmpty()) { - // Mark entities as not existing. - $entities->each(function ($entity) { - $entity->exists = false; - }); - // Re-attach remaining entities. - $voucher->voucherEntities()->saveMany($entities); - } - } - }); - - $bar->advance(); - } - - $bar->finish(); - $this->line(''); - } - } - - return 0; - } - - /** - * Get model classes. - */ - protected function getModels(): array - { - $models = $this->option('model'); - // Load models from folders if not directly specified. - if (empty($models)) { - $folders = $this->option('folder'); - if (empty($folders)) { - // Fallback to common folders. - $folders = ['app', 'app/Models']; - } - $this->info(sprintf( - 'Searching for models in folders: %s', - implode(', ', $folders) - )); - $models = collect($folders) - ->map(function (string $folder) { - $path = Str::startsWith($folder, '/') ? $folder : base_path($folder); - // Ensure no invalid paths are searched. - if (!is_dir($path)) { - return []; - } - - return collect(scandir($path)) - ->filter(function (string $file) { - // Remove any non PHP files. - return Str::endsWith($file, '.php'); - }) - ->map(function (string $file) use ($path) { - $class = basename($file, '.php'); - // Read first 200 bytes, should be enough to extract namespace. - $contents = file_get_contents($path . '/' . $file, false, null, 0, 200); - if (preg_match('/namespace\s+([^;]+);/i', $contents, $matches)) { - return $matches[1] . '\\' . $class; - } - - // Fallback to the class itself. - return '\\' . $class; - }) - ->values() - ->all() - ; - }) - ->flatten() - ->filter(function (string $class) { - $traits = class_uses_recursive($class); - - // Ensure only models using the `HasVouchers` trait are included. - return !empty($traits) && \in_array(HasVouchers::class, $traits); - }) - ->values() - ->all() - ; - } - - return $models; - } -} diff --git a/src/VouchersServiceProvider.php b/src/VouchersServiceProvider.php index 8812708..ddde7f6 100644 --- a/src/VouchersServiceProvider.php +++ b/src/VouchersServiceProvider.php @@ -4,7 +4,6 @@ namespace FrittenKeeZ\Vouchers; -use FrittenKeeZ\Vouchers\Console\Commands\MigrateCommand; use Illuminate\Support\ServiceProvider; class VouchersServiceProvider extends ServiceProvider @@ -22,13 +21,10 @@ class VouchersServiceProvider extends ServiceProvider public function boot(): void { $this->publishes([$this->getPublishConfigPath() => config_path('vouchers.php')], 'config'); - $this->publishes([$this->getPublishMigrationsPath() => database_path('migrations')], 'migrations'); - $this->loadMigrationsFrom(__DIR__ . '/../migrations'); - - if ($this->app->runningInConsole()) { - $this->commands([MigrateCommand::class]); - } + with(method_exists($this, 'publishesMigrations') ? 'publishesMigrations' : 'publishes', function ($method) { + $this->{$method}([$this->getPublishMigrationsPath() => database_path('migrations')], 'migrations'); + }); } /** @@ -38,9 +34,7 @@ public function register(): void { $this->mergeConfigFrom($this->getPublishConfigPath(), 'vouchers'); - $this->app->bind('vouchers', function () { - return new Vouchers(); - }); + $this->app->bind('vouchers', fn () => new Vouchers()); } /** diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 144137f..d58ec29 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -146,11 +146,7 @@ public function testDynamicallyOverriddenOptions(): void $this->assertSame($options['separator'], $config->getSeparator()); // Test 'without' calls. - $config - ->withoutPrefix() - ->withoutSuffix() - ->withoutSeparator() - ; + $config->withoutPrefix()->withoutSuffix()->withoutSeparator(); $this->assertSame('', $config->getPrefix()); $this->assertSame('', $config->getSuffix()); $this->assertSame('', $config->getSeparator()); @@ -212,11 +208,11 @@ public function testAdditionalOptions(): void ); // Test owner. - $owner = $this->factory(User::class)->make(); + $owner = User::factory()->make(); $this->assertSame($owner, $config->withOwner($owner)->getOwner()); // Test entities. - $entities = $this->factory(Color::class, 3)->make()->all(); + $entities = Color::factory()->count(3)->make()->all(); $this->assertSame($entities, $config->withEntities(...$entities)->getEntities()); } diff --git a/tests/HasVouchersTest.php b/tests/HasVouchersTest.php index d660c51..901655d 100644 --- a/tests/HasVouchersTest.php +++ b/tests/HasVouchersTest.php @@ -18,7 +18,7 @@ class HasVouchersTest extends TestCase */ public function testCreateVoucher(): void { - $user = $this->factory(User::class)->create(); + $user = User::factory()->create(); $voucher = $user->createVoucher(); // Check user voucher relation. @@ -31,8 +31,8 @@ public function testCreateVoucher(): void */ public function testCreateVoucherWithCallback(): void { - $user = $this->factory(User::class)->create(); - $color = $this->factory(Color::class)->create(); + $user = User::factory()->create(); + $color = Color::factory()->create(); $voucher = $user->createVoucher(function (Vouchers $vouchers) use ($color) { $vouchers->withEntities($color); }); @@ -48,8 +48,8 @@ public function testCreateVoucherWithCallback(): void */ public function testCreateVoucherWithAssociated(): void { - $user = $this->factory(User::class)->create(); - $other = $this->factory(User::class)->create(); + $user = User::factory()->create(); + $other = User::factory()->create(); $voucher = $user->createVoucher(function (Vouchers $vouchers) use ($other) { $vouchers->withEntities($other); }); @@ -68,7 +68,7 @@ public function testCreateVoucherWithAssociated(): void */ public function testCreateVouchers(): void { - $user = $this->factory(User::class)->create(); + $user = User::factory()->create(); $vouchers = $user->createVouchers(3); foreach ($vouchers as $index => $voucher) { @@ -83,8 +83,8 @@ public function testCreateVouchers(): void */ public function testCreateVouchersWithCallback(): void { - $user = $this->factory(User::class)->create(); - $color = $this->factory(Color::class)->create(); + $user = User::factory()->create(); + $color = Color::factory()->create(); $vouchers = $user->createVouchers(3, function (Vouchers $vouchers) use ($color) { $vouchers->withEntities($color); }); diff --git a/tests/MigrateCommandTest.php b/tests/MigrateCommandTest.php deleted file mode 100644 index 9955d94..0000000 --- a/tests/MigrateCommandTest.php +++ /dev/null @@ -1,174 +0,0 @@ -app->version(), '8.0', '<')) { - $this->markTestSkipped('Requires Laravel 8 to successfully complete these tests.'); - } - } - - /** - * Test arguments. - */ - public function testArguments(): void - { - $this->artisan('vouchers:migrate') - ->expectsOutput('Searching for models in folders: app, app/Models') - ->expectsOutput('Migration could not continue. See error messages below:') - ->expectsOutput('The models field is required.') - ->assertExitCode(1) - ; - - $this->artisan('vouchers:migrate', ['--mode' => 'fake', '--folder' => ['app/Models', 'app/Fakes']]) - ->expectsOutput('Searching for models in folders: app/Models, app/Fakes') - ->expectsOutput('Migration could not continue. See error messages below:') - ->expectsOutput('The selected mode is invalid.') - ->expectsOutput('The models field is required.') - ->assertExitCode(1) - ; - - $this->artisan('vouchers:migrate', ['--folder' => [__DIR__ . '/Models']]) - ->expectsOutput('Searching for models in folders: ' . __DIR__ . '/Models') - ->expectsOutput('Database operation mode is set to: auto') - ->expectsTable(['Model', 'Vouchers Count'], [[User::class, 0]]) - ->assertExitCode(0) - ; - - $this->artisan('vouchers:migrate', ['--mode' => 'retain', '--model' => [Color::class]]) - ->expectsOutput('Database operation mode is set to: retain') - ->expectsTable(['Model', 'Vouchers Count'], [[Color::class, 0]]) - ->assertExitCode(0) - ; - - $this->artisan('vouchers:migrate', ['--mode' => 'delete', '--model' => [User::class]]) - ->expectsOutput('Database operation mode is set to: delete') - ->expectsTable(['Model', 'Vouchers Count'], [[User::class, 0]]) - ->assertExitCode(0) - ; - } - - /** - * Test migration with auto mode. - */ - public function testMigrationModeAuto(): void - { - $user = $this->factory(User::class)->create(); - - // Create vouchers. - Vouchers::create(); - Vouchers::withOwner($user)->create(); - $v1 = Vouchers::withEntities($user)->create(); - $v2 = Vouchers::withEntities($user, ...$this->factory(Color::class, 2)->create())->create(); - - $this->assertSame(4, Voucher::count()); - $this->assertSame(3, Voucher::withoutOwner()->count()); - $this->assertSame(1, Voucher::withOwner($user)->count()); - $this->assertSame(2, $user->associatedVouchers()->count()); - - $this->artisan('vouchers:migrate', ['--model' => [User::class]]) - ->expectsOutput('Database operation mode is set to: auto') - ->expectsTable(['Model', 'Vouchers Count'], [[User::class, 2]]) - ->expectsConfirmation('Do you wish to continue?', 'yes') - ->assertExitCode(0) - ; - - // Refresh and check owners. - $v1->refresh(); - $v2->refresh(); - $this->assertTrue($user->is($v1->owner)); - $this->assertSame(0, $v1->voucherEntities->count()); - $this->assertTrue($user->is($v2->owner)); - $this->assertSame(3, $v2->voucherEntities->count()); - $this->assertSame(1, $user->associatedVouchers()->count()); - } - - /** - * Test migration with retain mode. - */ - public function testMigrationModeRetain(): void - { - $user = $this->factory(User::class)->create(); - - // Create vouchers. - Vouchers::create(); - Vouchers::withOwner($user)->create(); - $v1 = Vouchers::withEntities($user)->create(); - $v2 = Vouchers::withEntities($user, ...$this->factory(Color::class, 2)->create())->create(); - - $this->assertSame(4, Voucher::count()); - $this->assertSame(3, Voucher::withoutOwner()->count()); - $this->assertSame(1, Voucher::withOwner($user)->count()); - $this->assertSame(2, $user->associatedVouchers()->count()); - - $this->artisan('vouchers:migrate', ['--mode' => 'retain', '--model' => [User::class]]) - ->expectsOutput('Database operation mode is set to: retain') - ->expectsTable(['Model', 'Vouchers Count'], [[User::class, 2]]) - ->expectsConfirmation('Do you wish to continue?', 'yes') - ->assertExitCode(0) - ; - - // Refresh and check owners. - $v1->refresh(); - $v2->refresh(); - $this->assertTrue($user->is($v1->owner)); - $this->assertSame(1, $v1->voucherEntities->count()); - $this->assertTrue($user->is($v2->owner)); - $this->assertSame(3, $v2->voucherEntities->count()); - $this->assertSame(2, $user->associatedVouchers()->count()); - } - - /** - * Test migration with delete mode. - */ - public function testMigrationModeDelete(): void - { - $user = $this->factory(User::class)->create(); - - // Create vouchers. - Vouchers::create(); - Vouchers::withOwner($user)->create(); - $v1 = Vouchers::withEntities($user)->create(); - $v2 = Vouchers::withEntities($user, ...$this->factory(Color::class, 2)->create())->create(); - - $this->assertSame(4, Voucher::count()); - $this->assertSame(3, Voucher::withoutOwner()->count()); - $this->assertSame(1, Voucher::withOwner($user)->count()); - $this->assertSame(2, $user->associatedVouchers()->count()); - - $this->artisan('vouchers:migrate', ['--mode' => 'delete', '--model' => [User::class]]) - ->expectsOutput('Database operation mode is set to: delete') - ->expectsTable(['Model', 'Vouchers Count'], [[User::class, 2]]) - ->expectsConfirmation('Do you wish to continue?', 'yes') - ->assertExitCode(0) - ; - - // Refresh and check owners. - $v1->refresh(); - $v2->refresh(); - $this->assertTrue($user->is($v1->owner)); - $this->assertSame(0, $v1->voucherEntities->count()); - $this->assertTrue($user->is($v2->owner)); - $this->assertSame(2, $v2->voucherEntities->count()); - $this->assertSame(0, $user->associatedVouchers()->count()); - } -} diff --git a/tests/Models/Color.php b/tests/Models/Color.php index 01258c0..69a9106 100644 --- a/tests/Models/Color.php +++ b/tests/Models/Color.php @@ -4,10 +4,13 @@ namespace FrittenKeeZ\Vouchers\Tests\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Color extends Model { + use HasFactory; + /** * The attributes that are mass assignable. * diff --git a/tests/Models/User.php b/tests/Models/User.php index 631cd93..ed821ce 100644 --- a/tests/Models/User.php +++ b/tests/Models/User.php @@ -6,11 +6,13 @@ use FrittenKeeZ\Vouchers\Concerns\HasRedeemers; use FrittenKeeZ\Vouchers\Concerns\HasVouchers; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { + use HasFactory; use HasRedeemers; use HasVouchers; use Notifiable; diff --git a/tests/TestCase.php b/tests/TestCase.php index 5874415..02568a6 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -9,7 +9,6 @@ use FrittenKeeZ\Vouchers\Tests\Models\User; use FrittenKeeZ\Vouchers\VouchersServiceProvider; use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Support\Facades\App; use Orchestra\Testbench\TestCase as BaseTestCase; /** @@ -26,10 +25,8 @@ protected function setUp(): void $this->loadLaravelMigrations(); $this->artisan('migrate', ['--database' => 'testing']); + $this->loadMigrationsFrom(__DIR__ . '/../publishes/migrations'); $this->loadMigrationsFrom(__DIR__ . '/database/migrations'); - if ((float) App::version() < 8) { - $this->withFactories(__DIR__ . '/database/factories-legacy'); - } // Ensure everything works with morph map. Relation::morphMap([ @@ -57,29 +54,4 @@ protected function getPackageProviders($app): array { return [VouchersServiceProvider::class]; } - - /** - * Factory helper to handle both Laravel 8 and earlier versions. - * - * @return \Illuminate\Database\Eloquent\Factories\Factory|\Illuminate\Database\Eloquent\FactoryBuilder - */ - protected function factory(string $class, ?int $amount = null) - { - if (class_exists('Illuminate\\Database\\Eloquent\\Factory')) { - $factory = App::make('Illuminate\\Database\\Eloquent\\Factory'); - - if (isset($amount) && \is_int($amount)) { - return $factory->of($class)->times($amount); - } - - return $factory->of($class); - } - - $factory = 'FrittenKeeZ\\Vouchers\\Tests\\Database\\Factories\\' . class_basename($class) . 'Factory'; - if (isset($amount) && \is_int($amount)) { - return $factory::new()->count($amount); - } - - return $factory::new(); - } } diff --git a/tests/VoucherEntityScopesTest.php b/tests/VoucherEntityScopesTest.php index 0fc7b25..a0494d0 100644 --- a/tests/VoucherEntityScopesTest.php +++ b/tests/VoucherEntityScopesTest.php @@ -23,18 +23,17 @@ public function testEntityScopes(): void $vouchers = new Vouchers(); // Create user. - $user = $this->factory(User::class)->create(); + $user = User::factory()->create(); // Create vouchers. - $first = $vouchers->withEntities( - $user, - ...$this->factory(User::class, 2)->create(), - ...$this->factory(Color::class, 3)->create() - )->create(); - $second = $vouchers->withEntities( - ...$this->factory(User::class, 3)->create(), - ...$this->factory(Color::class, 6)->create() - )->create(); + $first = $vouchers + ->withEntities($user, ...User::factory()->count(2)->create(), ...Color::factory()->count(3)->create()) + ->create() + ; + $second = $vouchers + ->withEntities(...User::factory()->count(3)->create(), ...Color::factory()->count(6)->create()) + ->create() + ; $this->assertTrue(VoucherEntity::withEntity($user)->exists()); $this->assertSame(6, VoucherEntity::withEntityType(User::class)->count()); diff --git a/tests/VoucherScopesTest.php b/tests/VoucherScopesTest.php index e5b2106..d0bf9fc 100644 --- a/tests/VoucherScopesTest.php +++ b/tests/VoucherScopesTest.php @@ -186,11 +186,11 @@ public function testRedeemableScope(): void public function testEntitiesScope(): void { Vouchers::create(); - Vouchers::withEntities(...$this->factory(Color::class, 3)->create())->create(); - Vouchers::withEntities(...$this->factory(User::class, 3)->create())->create(); + Vouchers::withEntities(...Color::factory()->count(3)->create())->create(); + Vouchers::withEntities(...User::factory()->count(3)->create())->create(); Vouchers::withEntities( - ...$this->factory(Color::class, 3)->create(), - ...$this->factory(User::class, 3)->create() + ...Color::factory()->count(3)->create(), + ...User::factory()->count(3)->create() )->create(); $this->assertSame(4, Voucher::count()); @@ -205,9 +205,9 @@ public function testEntitiesScope(): void public function testOwnerScopes(): void { // Create users. - $first = $this->factory(User::class)->create(); - $second = $this->factory(User::class)->create(); - $third = $this->factory(User::class)->create(); + $first = User::factory()->create(); + $second = User::factory()->create(); + $third = User::factory()->create(); // Create vouchers. Vouchers::create(2); diff --git a/tests/VoucherTest.php b/tests/VoucherTest.php index e664a8b..4d7a8f4 100644 --- a/tests/VoucherTest.php +++ b/tests/VoucherTest.php @@ -26,7 +26,7 @@ public function testRedeemingEvent(): void }); $vouchers = new Vouchers(); - $user = $this->factory(User::class)->create(); + $user = User::factory()->create(); $voucher = $vouchers->create(); $this->assertTrue($voucher->isRedeemable()); $this->assertFalse($vouchers->redeem($voucher->code, $user)); @@ -46,7 +46,7 @@ public function testRedeemedEvent(): void }); $vouchers = new Vouchers(); - $user = $this->factory(User::class)->create(); + $user = User::factory()->create(); $voucher = $vouchers->create(); $this->assertTrue($voucher->isRedeemable()); $this->assertTrue($vouchers->redeem($voucher->code, $user)); @@ -68,7 +68,7 @@ public function testShouldMarkRedeemedEvent(): void }); $vouchers = new Vouchers(); - $user = $this->factory(User::class)->create(); + $user = User::factory()->create(); $voucher = $vouchers->create(); $this->assertTrue($voucher->isRedeemable()); $this->assertTrue($vouchers->redeem($voucher->code, $user)); @@ -89,8 +89,8 @@ public function testRedeemingByOwningUserOnlyEvent(): void }); $vouchers = new Vouchers(); - $user = $this->factory(User::class)->create(); - $other = $this->factory(User::class)->create(); + $user = User::factory()->create(); + $other = User::factory()->create(); $voucher = $vouchers->withOwner($user)->create(); $this->assertTrue($voucher->isRedeemable()); $this->assertFalse($vouchers->redeem($voucher->code, $other)); diff --git a/tests/VouchersTest.php b/tests/VouchersTest.php index c81f4af..952dd6c 100644 --- a/tests/VouchersTest.php +++ b/tests/VouchersTest.php @@ -100,8 +100,8 @@ public function testVoucherCreation(): void $now = Carbon::now(); $start_time = $now->copy()->add(CarbonInterval::create('P1D')); $expire_time = $now->copy()->add(CarbonInterval::create('P30D')); - $user = $this->factory(User::class)->create(); - $users = $this->factory(User::class, 3)->create(); + $user = User::factory()->create(); + $users = User::factory()->count(3)->create(); $voucher = $vouchers ->withMetadata($metadata) ->withStartTime($start_time) @@ -112,14 +112,8 @@ public function testVoucherCreation(): void ; $this->assertInstanceOf(Voucher::class, $voucher); $this->assertSame($metadata, $voucher->metadata); - $this->assertSame( - $start_time->toDateTimeString(), - $voucher->starts_at->toDateTimeString() - ); - $this->assertSame( - $expire_time->toDateTimeString(), - $voucher->expires_at->toDateTimeString() - ); + $this->assertSame($start_time->toDateTimeString(), $voucher->starts_at->toDateTimeString()); + $this->assertSame($expire_time->toDateTimeString(), $voucher->expires_at->toDateTimeString()); $this->assertTrue($user->is($voucher->owner)); foreach ($voucher->getEntities() as $index => $entity) { $this->assertTrue($users[$index]->is($entity)); @@ -143,7 +137,7 @@ public function testVoucherCreation(): void public function testVoucherRedemption(): void { $vouchers = new Vouchers(); - $user = $this->factory(User::class)->create(); + $user = User::factory()->create(); $voucher = $vouchers->withOwner($user)->create(); // Check user voucher relation. @@ -153,9 +147,11 @@ public function testVoucherRedemption(): void // Check voucher states. $this->assertTrue($voucher->isRedeemable()); $this->assertTrue($vouchers->redeemable($voucher->code)); - $this->assertFalse($vouchers->redeemable($voucher->code, function (Voucher $voucher) { - return $voucher->hasPrefix('thisprefixdoesnotexist'); - })); + $this->assertFalse( + $vouchers->redeemable($voucher->code, function (Voucher $voucher) { + return $voucher->hasPrefix('thisprefixdoesnotexist'); + }) + ); $this->assertEmpty($voucher->redeemers); $this->assertEmpty($voucher->getEntities()); $metadata = ['foo' => 'bar', 'baz' => 'boom']; @@ -179,7 +175,7 @@ public function testVoucherRedemption(): void public function testVoucherNotFoundException(): void { $vouchers = new Vouchers(); - $user = $this->factory(User::class)->create(); + $user = User::factory()->create(); $this->expectException(VoucherNotFoundException::class); $vouchers->redeem('idonotexist', $user); @@ -192,7 +188,7 @@ public function testVoucherAlreadyRedeemedException(): void { $vouchers = new Vouchers(); $voucher = $vouchers->create(); - $user = $this->factory(User::class)->create(); + $user = User::factory()->create(); $this->assertTrue($vouchers->redeem($voucher->code, $user)); $this->expectException(VoucherAlreadyRedeemedException::class); @@ -249,9 +245,11 @@ protected function generateCodeValidationRegex( string $separator ): string { $match = preg_quote($characters, '/'); - $inner = preg_replace_callback('/(?:\\\\\*)+/', function (array $matches) use ($match) { - return sprintf('[%s]{%d}', $match, mb_strlen($matches[0]) / 2); - }, preg_quote($mask, '/')); + $inner = preg_replace_callback( + "/(?:\\\\\*)+/", + fn (array $matches) => sprintf('[%s]{%d}', $match, mb_strlen($matches[0]) / 2), + preg_quote($mask, '/') + ); return sprintf( '/%s%s%s/', diff --git a/tests/database/factories-legacy/ColorFactory.php b/tests/database/factories-legacy/ColorFactory.php deleted file mode 100644 index 23e6a11..0000000 --- a/tests/database/factories-legacy/ColorFactory.php +++ /dev/null @@ -1,23 +0,0 @@ -define(Color::class, function (Faker $faker) { - return [ - 'name' => $faker->colorName, - ]; -}); diff --git a/tests/database/factories-legacy/UserFactory.php b/tests/database/factories-legacy/UserFactory.php deleted file mode 100644 index 98ce4d8..0000000 --- a/tests/database/factories-legacy/UserFactory.php +++ /dev/null @@ -1,27 +0,0 @@ -define(User::class, function (Faker $faker) { - return [ - 'name' => $faker->name, - 'email' => $faker->unique()->safeEmail, - 'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret - 'remember_token' => Str::random(10), - ]; -}); diff --git a/tests/database/factories/ColorFactory.php b/tests/database/factories/ColorFactory.php index f848314..80f0671 100644 --- a/tests/database/factories/ColorFactory.php +++ b/tests/database/factories/ColorFactory.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace FrittenKeeZ\Vouchers\Tests\Database\Factories; +namespace Database\Factories\FrittenKeeZ\Vouchers\Tests\Models; use FrittenKeeZ\Vouchers\Tests\Models\Color; use Illuminate\Database\Eloquent\Factories\Factory; diff --git a/tests/database/factories/UserFactory.php b/tests/database/factories/UserFactory.php index 3c4518b..85dc1ab 100644 --- a/tests/database/factories/UserFactory.php +++ b/tests/database/factories/UserFactory.php @@ -2,10 +2,11 @@ declare(strict_types=1); -namespace FrittenKeeZ\Vouchers\Tests\Database\Factories; +namespace Database\Factories\FrittenKeeZ\Vouchers\Tests\Models; use FrittenKeeZ\Vouchers\Tests\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Support\Facades\Hash; use Illuminate\Support\Str; class UserFactory extends Factory @@ -26,7 +27,7 @@ public function definition(): array 'name' => $this->faker->name, 'email' => $this->faker->unique()->safeEmail, 'email_verified_at' => now(), - 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password + 'password' => Hash::make('password'), 'remember_token' => Str::random(10), ]; } diff --git a/tests/database/migrations/2019_12_19_000000_create_vouchers_test_tables.php b/tests/database/migrations/2019_12_19_000000_create_vouchers_test_tables.php index 49752c0..fb68e95 100644 --- a/tests/database/migrations/2019_12_19_000000_create_vouchers_test_tables.php +++ b/tests/database/migrations/2019_12_19_000000_create_vouchers_test_tables.php @@ -6,7 +6,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateVouchersTestTables extends Migration +return new class extends Migration { /** * Run the migrations. @@ -27,4 +27,4 @@ public function down(): void { Schema::dropIfExists('colors'); } -} +};