Skip to content

refactor: consolidate repetitive model instantiations across tests #3416

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

Open
wants to merge 2 commits into
base: 5.x
Choose a base branch
from
Open
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
59 changes: 24 additions & 35 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@

class ModelTest extends TestCase
{
public function tearDown(): void
protected function setUp(): void
{
parent::setUp();
Carbon::setTestNow();
}

protected function tearDown(): void
{
Carbon::setTestNow();
DB::connection('mongodb')->getCollection('users')->drop();
Expand Down Expand Up @@ -81,15 +87,22 @@ public function testQualifyColumn(): void
$this->assertEquals('users.name', $sqlUser->qualifyColumn('name'));
}

public function testInsert(): void
private function makeUser(): User
{
$user = new User();
$user = new User();
$user->name = 'John Doe';
$user->title = 'admin';
$user->age = 35;

$user->save();

return $user;
}

public function testInsert(): void
{
$user = $this->makeUser();

$this->assertTrue($user->exists);
$this->assertEquals(1, User::count());

Expand All @@ -108,11 +121,7 @@ public function testInsert(): void

public function testUpdate(): void
{
$user = new User();
$user->name = 'John Doe';
$user->title = 'admin';
$user->age = 35;
$user->save();
$user = $this->makeUser();

$raw = $user->getAttributes();
$this->assertInstanceOf(ObjectID::class, $raw['id']);
Expand Down Expand Up @@ -227,11 +236,7 @@ public function testManualIntId(): void

public function testDelete(): void
{
$user = new User();
$user->name = 'John Doe';
$user->title = 'admin';
$user->age = 35;
$user->save();
$user = $this->makeUser();

$this->assertTrue($user->exists);
$this->assertEquals(1, User::count());
Expand All @@ -243,11 +248,7 @@ public function testDelete(): void

public function testAll(): void
{
$user = new User();
$user->name = 'John Doe';
$user->title = 'admin';
$user->age = 35;
$user->save();
$user = $this->makeUser();

$user = new User();
$user->name = 'Jane Doe';
Expand All @@ -264,11 +265,7 @@ public function testAll(): void

public function testFind(): void
{
$user = new User();
$user->name = 'John Doe';
$user->title = 'admin';
$user->age = 35;
$user->save();
$user = $this->makeUser();

$check = User::find($user->id);
$this->assertInstanceOf(User::class, $check);
Expand Down Expand Up @@ -347,11 +344,7 @@ public function testCreate(): void

public function testDestroy(): void
{
$user = new User();
$user->name = 'John Doe';
$user->title = 'admin';
$user->age = 35;
$user->save();
$user = $this->makeUser();

User::destroy((string) $user->id);

Expand All @@ -360,11 +353,7 @@ public function testDestroy(): void

public function testTouch(): void
{
$user = new User();
$user->name = 'John Doe';
$user->title = 'admin';
$user->age = 35;
$user->save();
$user = $this->makeUser();

$old = $user->updated_at;
sleep(1);
Expand Down Expand Up @@ -1052,7 +1041,7 @@ public function testChunkById(): void
$this->assertEquals(['fork', 'spork', 'spoon'], $names);
}

public function testTruncateModel()
public function testTruncateModel(): void
{
User::create(['name' => 'John Doe']);

Expand All @@ -1061,7 +1050,7 @@ public function testTruncateModel()
$this->assertEquals(0, User::count());
}

public function testGuardedModel()
public function testGuardedModel(): void
{
$model = new Guarded();

Expand Down
Loading