diff --git a/tests/Integration/Database/DatabaseTestCase.php b/tests/Integration/Database/DatabaseTestCase.php index 157d3a56853f..4b08becfff1a 100644 --- a/tests/Integration/Database/DatabaseTestCase.php +++ b/tests/Integration/Database/DatabaseTestCase.php @@ -16,6 +16,17 @@ abstract class DatabaseTestCase extends TestCase */ protected $driver; + protected function setUp(): void + { + $this->beforeApplicationDestroyed(function () { + foreach (array_keys($this->app['db']->getConnections()) as $name) { + $this->app['db']->purge($name); + } + }); + + parent::setUp(); + } + protected function getEnvironmentSetUp($app) { $connection = $app['config']->get('database.default'); diff --git a/tests/Integration/Database/EloquentModelStringCastingTest.php b/tests/Integration/Database/EloquentModelStringCastingTest.php index f0ff495ac820..7ba208f37498 100644 --- a/tests/Integration/Database/EloquentModelStringCastingTest.php +++ b/tests/Integration/Database/EloquentModelStringCastingTest.php @@ -2,37 +2,16 @@ namespace Illuminate\Tests\Integration\Database; -use Illuminate\Database\Capsule\Manager as DB; use Illuminate\Database\Eloquent\Model as Eloquent; use Illuminate\Database\Schema\Blueprint; -use PHPUnit\Framework\TestCase; +use Illuminate\Support\Facades\Schema; use stdClass; -class EloquentModelStringCastingTest extends TestCase +class EloquentModelStringCastingTest extends DatabaseTestCase { - protected function setUp(): void + protected function defineDatabaseMigrationsAfterDatabaseRefreshed() { - $db = new DB; - - $db->addConnection([ - 'driver' => 'sqlite', - 'database' => ':memory:', - ]); - - $db->bootEloquent(); - $db->setAsGlobal(); - - $this->createSchema(); - } - - /** - * Setup the database schema. - * - * @return void - */ - public function createSchema() - { - $this->schema()->create('casting_table', function (Blueprint $table) { + Schema::create('casting_table', function (Blueprint $table) { $table->increments('id'); $table->string('array_attributes'); $table->string('json_attributes'); @@ -41,16 +20,6 @@ public function createSchema() }); } - /** - * Tear down the database schema. - * - * @return void - */ - protected function tearDown(): void - { - $this->schema()->drop('casting_table'); - } - /** * Tests... */ @@ -91,26 +60,6 @@ public function testSavingCastedEmptyAttributesToDatabase() $this->assertSame([], $model->getOriginal('object_attributes')); $this->assertSame([], $model->getAttribute('object_attributes')); } - - /** - * Get a database connection instance. - * - * @return \Illuminate\Database\Connection - */ - protected function connection() - { - return Eloquent::getConnectionResolver()->connection(); - } - - /** - * Get a schema builder instance. - * - * @return \Illuminate\Database\Schema\Builder - */ - protected function schema() - { - return $this->connection()->getSchemaBuilder(); - } } /** diff --git a/tests/Integration/Database/EloquentPrunableTest.php b/tests/Integration/Database/EloquentPrunableTest.php index 20e7d5b2345d..1c11b4c457be 100644 --- a/tests/Integration/Database/EloquentPrunableTest.php +++ b/tests/Integration/Database/EloquentPrunableTest.php @@ -2,33 +2,18 @@ namespace Illuminate\Tests\Integration\Database; -use Illuminate\Container\Container; -use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Prunable; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Events\ModelsPruned; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Schema; use LogicException; -use Mockery as m; /** @group SkipMSSQL */ class EloquentPrunableTest extends DatabaseTestCase { - protected function setUp(): void - { - parent::setUp(); - - Container::setInstance($container = new Container); - - $container->singleton(Dispatcher::class, function () { - return m::mock(Dispatcher::class); - }); - - $container->alias(Dispatcher::class, 'events'); - } - protected function defineDatabaseMigrationsAfterDatabaseRefreshed() { collect([ @@ -58,10 +43,7 @@ public function testPrunableMethodMustBeImplemented() public function testPrunesRecords() { - app('events') - ->shouldReceive('dispatch') - ->times(2) - ->with(m::type(ModelsPruned::class)); + Event::fake(); collect(range(1, 5000))->map(function ($id) { return ['id' => $id]; @@ -73,14 +55,13 @@ public function testPrunesRecords() $this->assertEquals(1500, $count); $this->assertEquals(3500, PrunableTestModel::count()); + + Event::assertDispatched(ModelsPruned::class, 2); } public function testPrunesSoftDeletedRecords() { - app('events') - ->shouldReceive('dispatch') - ->times(3) - ->with(m::type(ModelsPruned::class)); + Event::fake(); collect(range(1, 5000))->map(function ($id) { return ['id' => $id, 'deleted_at' => now()]; @@ -93,14 +74,13 @@ public function testPrunesSoftDeletedRecords() $this->assertEquals(3000, $count); $this->assertEquals(0, PrunableSoftDeleteTestModel::count()); $this->assertEquals(2000, PrunableSoftDeleteTestModel::withTrashed()->count()); + + Event::assertDispatched(ModelsPruned::class, 3); } public function testPruneWithCustomPruneMethod() { - app('events') - ->shouldReceive('dispatch') - ->times(1) - ->with(m::type(ModelsPruned::class)); + Event::fake(); collect(range(1, 5000))->map(function ($id) { return ['id' => $id]; @@ -114,6 +94,8 @@ public function testPruneWithCustomPruneMethod() $this->assertTrue((bool) PrunableWithCustomPruneMethodTestModel::first()->pruned); $this->assertFalse((bool) PrunableWithCustomPruneMethodTestModel::orderBy('id', 'desc')->first()->pruned); $this->assertEquals(5000, PrunableWithCustomPruneMethodTestModel::count()); + + Event::assertDispatched(ModelsPruned::class, 1); } }