Skip to content

Commit

Permalink
[8.x] Test Improvements (#39877)
Browse files Browse the repository at this point in the history
* Test Improvements

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone authored Dec 3, 2021
1 parent 1225088 commit 1f70040
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 83 deletions.
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

0 comments on commit 1f70040

Please sign in to comment.