Skip to content

Instantly share code, notes, and snippets.

@roni-estein
Last active November 19, 2018 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roni-estein/796aea105d087ecea6d21e43f6863f3b to your computer and use it in GitHub Desktop.
Save roni-estein/796aea105d087ecea6d21e43f6863f3b to your computer and use it in GitHub Desktop.
A series of general helper macro's that apply to all tests
<?php
// To make this file work properly, you must create the App\Helpers\Model
// class, and have all models inherit from this class.
namespace Tests;
use App\Helpers\Model;
use PHPUnit\Framework\Assert;
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Foundation\Testing\TestResponse;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
// Determine in a view has been loaded or if a view is missing
// This will even drill down to view partials
use Viewable;
public function setUp()
{
parent::setUp();
TestResponse::macro('data', function ($key = null) {
if (!is_null($key)) {
return $this->original->getData()[$key];
}
return is_null($this->original->getData()) ? null : collect($this->original->getData());
});
BaseCollection::macro('assertEquals', function ($items) {
Assert::assertCount($items->count(), $this);
$this->zip($items)->each(function ($itemPair) {
Assert::assertTrue($itemPair[0]->is($itemPair[1]));
});
});
BaseCollection::macro('assertContains', function ($item) {
Assert::assertTrue($this->contains($item));
});
BaseCollection::macro('assertDoesNotContain', function ($item) {
Assert::assertFalse($this->contains($item));
});
Model::macro('tableHeaders', function(){
return array_keys($this->attributes);
});
$this->withoutExceptionHandling();
}
protected function signIn($user = null)
{
throw new \Exception('Use method basicSignIn(...parameters)');
}
protected function errorsArray($errorBag = 'default')
{
ddf(session()->get('errors')->getBag($errorBag));
}
protected function logoutUser($guard = null)
{
return $this->post(route('logout'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment