Last active
June 29, 2019 16:35
-
-
Save deleugpn/7a2adaee968c0a73b6c6edafb3ffbdd3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class RemoveUserTest extends DatabaseTestCase | |
{ | |
protected function setUp(): void | |
{ | |
parent::setUp(); | |
Schema::create('users', function (Blueprint $table) { | |
$table->increments('id'); | |
$table->string('name'); | |
$table->string('email'); | |
$table->timestamps(); | |
}); | |
} | |
public function test_remove_user() | |
{ | |
User::query()->create([ | |
'name' => 'Taylor Otwell', | |
'email' => '[email protected]', | |
]); | |
$service = new MyService(); | |
$service->remove(User::query()->first()); | |
$this->assertDatabaseMissing('users', [ | |
'email' => '[email protected]', | |
]); | |
} | |
} | |
class User extends Model | |
{ | |
protected $guarded = []; | |
protected $table = 'users'; | |
} | |
class MyService | |
{ | |
public function remove(User $user) | |
{ | |
$user->delete(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class RemoveUserTest extends DatabaseTestCase | |
{ | |
protected function setUp(): void | |
{ | |
parent::setUp(); | |
Schema::create('users', function (Blueprint $table) { | |
$table->increments('id'); | |
$table->string('name'); | |
$table->string('email'); | |
$table->timestamps(); | |
}); | |
} | |
public function test_remove_user() | |
{ | |
User::query()->create([ | |
'name' => 'Taylor Otwell', | |
'email' => '[email protected]', | |
]); | |
$service = new MyService(); | |
$service->remove(User::query()->first()); | |
$this->assertDatabaseMissing('users', [ | |
'email' => '[email protected]', | |
]); | |
} | |
public function test_remove_all_users() | |
{ | |
User::query()->create([ | |
'name' => 'Taylor Otwell', | |
'email' => '[email protected]', | |
]); | |
User::query()->create([ | |
'name' => 'Marco Deleu', | |
'email' => '[email protected]', | |
]); | |
$service = new MyService; | |
$service->remove(new User); | |
self::assertSame(0, User::query()->count()); | |
} | |
} | |
class User extends Model | |
{ | |
protected $guarded = []; | |
protected $table = 'users'; | |
} | |
class MyService | |
{ | |
public function remove(User $user) | |
{ | |
$user->newQuery()->delete(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Eloquent type-hint has effectively two meanings: A Repository-type class that can communicate with the database or a specific row. When sending an Eloquent object as a repository, the receiver can use Eloquent to do all sorts of database operation. However, when the intent is to specify a single instance of a record, the receiver can still "disobey" that intent. In the following snippet, the 2nd version was developed by a Jr. Developer that thought he could reuse an existing service without realizing that he would be changing the intended type of object the service was suppose to receive WITHOUT breaking any test.