Git Product home page Git Product logo

testassist's Introduction

TestAssist

Latest Version on Packagist Total Downloads StyleCI

This package contains some nifty helpers and assertions that are made with Laravel in mind. The majority of functions are found in other packages, tutorials or repositories and credit is given for each function.

Installation

Via Composer

$ composer require jeroen-g/testassist

Usage

All helpers are designed as traits and organised in components focused on browser, console and filesystem tests. If you want to have all helpers, simply use the general Assistants that will include all others for you.

use JeroenG\TestAssist\Assistants;

class MyTest {
    use Assistants;

    // ...
}

General functions

AssertIsCalled

use JeroenG\TestAssist\Assistants

This function accepts two parameters, the first is the classname that needs to get called, the second is a callback during which the class should be called. And example would be an event listener:

public function test_this()
{
    $this->assertIsCalled(UserHasSignedUp::class, function() {
        User::create([....]);
    });
}

Browser (Laravel Dusk)

ClearCookiesBetweenTests

use JeroenG\TestAssist\Browser\ClearCookiesBetweenTests

Using this trait will make sure that between each test the user is logged out and all (session) cookies are deleted. The best way to use this is to alter your TestCase's tearDown():

use JeroenG\TestAssist\Browser\ClearCookiesBetweenTests;

protected function tearDown()
{
    parent::tearDown();
    $this->clearCookies();
}

Console

OutputAssertions

use JeroenG\TestAssist\Console\OutputAssertions

Contains two functions: seeInConsoleOutput($text) and 'doNotSeeInConsoleOutput($text)` to check whether or not the (un)expected text appears in the console output.

ExceptionHandling

use JeroenG\TestAssist\Console\ExceptionHandling

Use disableExceptionHandling() to get the entire stacktrace instead of a error 500-like message. Use withExceptionHandling() to reactivate Laravel's error rendering.

Database

FabricateModels

use JeroenG\TestAssist\Database\FabricateModels

Containts two functions as shortcuts to Laravel's factory method to create/make model instances. The shortest use would be as follows:

$member = $this->create(User::class);
$member = $this->make(User::class);

DataAssertions

use JeroenG\TestAssist\Database\DataAssertions

Containts two methods to assert if an Eloquent model is inserted into the database (technically an alias for assertDatabaseHas()) or to assert that its row is updated or deleted.

$this->assertCreated('users', $member);
$this->assertUpdated('users', $member);
$this->assertDeleted('users', $member);

SQLiteRegex

use JeroenG\TestAssist\Database\SQLiteRegex

Add the function below to your SetUpBeforeClass to give SQLite the ability to use REGEXP statements. More information here and credits should go here.

$this->AddRegexToSQLite();

SeedDatabase

use JeroenG\TestAssist\Database\SeedDatabase

Runs your DatabaseSeeder class if you use the trait. In your testcase's setUp method you should run this:

// At the top of your class:
use JeroenG\TestAssist\Assistants;

// in setUp():
$this->runSeeder();

If you do not use the whole set of assistants (see top of usage section), place the following code in your setUp() method to run it only once:

// If Concerns\SeedDatabase is used, go seed the database, but only once per class.
if (isset(static::$seedDatabase) && static::$seedDatabase != false) {
    $this->seedDatabase();
    static::$seedDatabase = false;
}

Filesystem

ManageFilesystem

use JeroenG\TestAssist\Filesystem\ManageFilesystem

As the moment this trait has only the function removeDir($path) to nuke the directory and everything inside of it.

FileAssertions

use JeroenG\TestAssist\Filesystem\FileAssertions

There are several assertions present to work with the (non)existence of files and directories.

function test_filesystem() {
    $this->assertFileExistsOnDisk($filename); // Optionally pass the storage disk (defualt: local)
    $this->assertFileNotExistsOnDisk($filename);
    $this->assertFileExistsInZip($zipPath, $filename);
    $this->assertFileDoesntExistsInZip($zipPath, $filename);
}

The underlying functions such as pathExists() are of course also available for use.

Change log

Please see the changelog for more information on what has changed recently.

Contributing

Please see contributing.md for details and a todolist.

Credits

License

EUPL-1.1. Please see the license file for more information.

testassist's People

Stargazers

 avatar

Watchers

 avatar  avatar

testassist's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.