Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Test Improvements #39877

Merged
merged 4 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tests/Integration/Database/DatabaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
59 changes: 4 additions & 55 deletions tests/Integration/Database/EloquentModelStringCastingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -41,16 +20,6 @@ public function createSchema()
});
}

/**
* Tear down the database schema.
*
* @return void
*/
protected function tearDown(): void
{
$this->schema()->drop('casting_table');
}

/**
* Tests...
*/
Expand Down Expand Up @@ -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();
}
}

/**
Expand Down
38 changes: 10 additions & 28 deletions tests/Integration/Database/EloquentPrunableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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];
Expand All @@ -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()];
Expand All @@ -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];
Expand All @@ -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);
}
}

Expand Down