Skip to content

Instantly share code, notes, and snippets.

View devmsh's full-sized avatar
👨‍💻
Available for new projects

Mohammed S Shurrab devmsh

👨‍💻
Available for new projects
View GitHub Profile
@devmsh
devmsh / 8.php
Created February 16, 2020 19:49
Write better Laravel tests
<?php
class LoanTest extends TestCase{
public function test_can_log_a_loan()
{
$user = factory(User::class)->create();
$wallet = factory(Wallet::class)->create([
'user_id' => $user->id
]);
@devmsh
devmsh / 7.php
Last active February 16, 2020 19:36
Write better Laravel tests 2
<?php
class LoanTest extends TestCase
{
use DatabaseMigrations;
public function test_can_log_a_loan()
{
$user = factory(User::class)->create();
$wallet = factory(Wallet::class)->create([
@devmsh
devmsh / 6.php
Created February 10, 2020 08:09
Write better Laravel tests 6
<?php
/**
* @depends test_user_have_a_copy_of_the_default_categories
*/
public function test_users_can_get_their_categories_list()
{
factory(Category::class, 5)->create();
$this->passportAs($user = factory(User::class)->create())
@devmsh
devmsh / 5.php
Created February 10, 2020 08:06
Write better Laravel tests
<?php
// Option 1, write a simple comment
public function test_users_can_get_their_categories_list()
{
factory(Category::class, 5)->create();
// once the user created, we will have a copy of the default categories
$user = factory(User::class)->create();
// ...
@devmsh
devmsh / 4.php
Created February 10, 2020 08:05
Write better Laravel tests 4
<?php
// add this method to your TestCase
public function passportAs($user, $scopes = [], $guard = 'api')
{
Passport::actingAs($user, $scopes, $guard);
return $this;
}
@devmsh
devmsh / 3.php
Created February 10, 2020 08:04
Write better Laravel tests 3
<?php
// add this macro to your service provider
TestResponse::macro('assertJsonPaths', function ($path, $expected) {
foreach ($this->json($path) as $real) {
PHPUnit::assertEquals($expected, $real);
}
return $this;
});
@devmsh
devmsh / 2.php
Created February 10, 2020 08:04
Write better Laravel tests 2
<?php
public function test_users_can_get_their_categories_list()
{
// ...
$response = $this->get('api/categories')
->assertSuccessful()
->dump()
->assertJsonCount(5)
@devmsh
devmsh / 1.php
Created February 10, 2020 08:01
Write better Laravel tests 1
<?php
public function test_users_can_get_their_categories_list()
{
factory(Category::class, 5)->create();
$user = factory(User::class)->create();
Passport::actingAs($user);
$response = $this->get('api/categories');
@devmsh
devmsh / 1.php
Last active February 10, 2020 07:45
Write better Laravel tests
<?php
public function test_users_can_get_their_categories_list()
{
factory(Category::class, 5)->create();
$user = factory(User::class)->create();
Passport::actingAs($user);
$response = $this->get('api/categories');
<?php
class response {
public $response = [];
function __construct(){
$this->addNormalResponse();
return $this->response;
}