Skip to content

Instantly share code, notes, and snippets.

Avatar

Adam Mackintosh agm1984

View GitHub Profile
@agm1984
agm1984 / SomeTest.php
Last active Jul 11, 2020
Quick demonstration of using `DatabaseTransactions` trait in Laravel unit tests
View SomeTest.php
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class SomeTest extends TestCase
{
use DatabaseTransactions;
@agm1984
agm1984 / .babelrc
Created Jul 10, 2020
`.babelrc` settings for Laravel + Vue + Jest
View .babelrc
{
"env": {
"test": {
"presets": [["@babel/preset-env", { "targets": { "node": "current" } }]]
}
}
}
@agm1984
agm1984 / .jest.config.js
Last active Jul 10, 2020
Demonstration of `.jest.config.js` for `vue-jest` and `jest`
View .jest.config.js
module.exports = {
testRegex: 'resources/js/.*.spec.js$',
moduleFileExtensions: [
'js',
'json',
'vue',
],
transform: {
'^.+\\.vue$': 'vue-jest',
'^.+\\.js$': 'babel-jest',
@agm1984
agm1984 / LoginTest.php
Last active Jul 10, 2020
Demonstration of `resetAuth` function
View LoginTest.php
<?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 Jul 10, 2020
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
View TestCase.php
<?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 Jul 10, 2020
Demonstrates how to use a "select or create" pattern for Laravel unit testing via `$this->actingAs($user);`
View TestCase.php
<?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 Jul 10, 2020
Demonstrates how to use a "select or create" pattern for Laravel unit testing via `$this->actingAs($user);`
View TestCase.php
<?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 Jul 10, 2020
Example of how to use the `DatabaseTransactions` Trait in Laravel.
View TestCase.php
<?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 / OAuthGitHubTest.php
Created Jul 10, 2020
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
View OAuthGitHubTest.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 Jul 10, 2020
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)
View phpunit.xml
<?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">