Skip to content

Instantly share code, notes, and snippets.

View agm1984's full-sized avatar

Adam Mackintosh agm1984

View GitHub Profile
@agm1984
agm1984 / .babelrc
Created July 10, 2020 06:40
`.babelrc` settings for Laravel + Vue + Jest
{
"env": {
"test": {
"presets": [["@babel/preset-env", { "targets": { "node": "current" } }]]
}
}
}
@agm1984
agm1984 / .jest.config.js
Last active July 10, 2020 06:24
Demonstration of `.jest.config.js` for `vue-jest` and `jest`
module.exports = {
testRegex: 'resources/js/.*.spec.js$',
moduleFileExtensions: [
'js',
'json',
'vue',
],
transform: {
'^.+\\.vue$': 'vue-jest',
'^.+\\.js$': 'babel-jest',
@agm1984
agm1984 / LoginTest.php
Last active July 10, 2020 03:29
Demonstration of `resetAuth` function
<?php
namespace Tests\Auth;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class LoginTest extends TestCase
{
@agm1984
agm1984 / TestCase.php
Last active July 10, 2020 03:14
Demonstration of how to use a `resetAuth` function to fully reset auth state in-between Laravel unit tests. For theory, see: https://stackoverflow.com/a/57941133/6141025
<?php
use Illuminate\Auth\SessionGuard;
// ...
/**
* Resets AuthManager state by logging-out the user from all auth guards.
* This is used between unit tests to wipe cached auth state.
*
@agm1984
agm1984 / TestCase.php
Last active July 10, 2020 02:57
Demonstrates how to use a "select or create" pattern for Laravel unit testing via `$this->actingAs($user);`
<?php
namespace Tests;
use App\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Spatie\Permission\Models\Role;
abstract class TestCase extends BaseTestCase
@agm1984
agm1984 / TestCase.php
Created July 10, 2020 02:51
Demonstrates how to use a "select or create" pattern for Laravel unit testing via `$this->actingAs($user);`
<?php
namespace Tests;
use App\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Spatie\Permission\Models\Role;
abstract class TestCase extends BaseTestCase
@agm1984
agm1984 / TestCase.php
Created July 10, 2020 02:35
Example of how to use the `DatabaseTransactions` Trait in Laravel.
<?php
namespace Tests;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication, DatabaseTransactions;
@agm1984
agm1984 / SomeTest.php
Last active July 11, 2020 07:34
Quick demonstration of using `DatabaseTransactions` trait in Laravel unit tests
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class SomeTest extends TestCase
{
use DatabaseTransactions;
@agm1984
agm1984 / OAuthGitHubTest.php
Created July 10, 2020 01:48
This is not a full set of tests, but it should be enough to see one way to use Mockery. See this repo, where I originally found this code, for more information: https://github.com/cretueusebiu/laravel-vue-spa/blob/master/app/OAuthProvider.php
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\OauthProvider;
use App\User;
use Illuminate\Support\Str;
use Illuminate\Testing\TestResponse;
use Laravel\Socialite\Facades\Socialite;
@agm1984
agm1984 / phpunit.xml
Last active July 10, 2020 01:42
phpunit.xml for Laravel unit testing with database transactions. DB_CONNECTION and DB_DATABASE are omitted which implicitly instructs Laravel to use the database information in the currently-loaded .env file (note: we are testing against the current environment and the <php> section in this file overwrites specific env variables)
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">