Skip to content

Instantly share code, notes, and snippets.

@reinink
Created March 21, 2019 00:57
Show Gist options
  • Select an option

  • Save reinink/1b2cd63f17951e40a1d9cac2b1777f16 to your computer and use it in GitHub Desktop.

Select an option

Save reinink/1b2cd63f17951e40a1d9cac2b1777f16 to your computer and use it in GitHub Desktop.
<?php
use PHPUnit\Framework\Assert;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Testing\TestResponse;
TestResponse::macro('data', function ($key) {
return $this->original->getData()[$key];
});
TestResponse::macro('assertUnauthorized', function () {
$this->assertStatus(403);
return $this;
});
TestResponse::macro('assertNotFound', function () {
$this->assertStatus(404);
return $this;
});
TestResponse::macro('assertValidationErrors', function () {
$this->assertStatus(422);
return $this;
});
TestResponse::macro('assertViewContains', function ($key, $value) {
$this->data($key)->assertContains($value);
return $this;
});
TestResponse::macro('assertViewNotContains', function ($key, $value) {
$this->data($key)->assertNotContains($value);
return $this;
});
TestResponse::macro('assertViewData', function ($callback) {
Assert::assertTrue($callback((object) $this->original->getData()));
return $this;
});
Collection::macro('assertContains', function ($value) {
Assert::assertTrue($this->contains($value), 'Failed asserting that the collection contains the specified value.');
return $this;
});
Collection::macro('assertNotContains', function ($value) {
Assert::assertFalse($this->contains($value), 'Failed asserting that the collection does not contain the specified value.');
return $this;
});
Collection::macro('assertEquals', function ($items) {
Assert::assertEquals(count($this), count($items));
$this->zip($items)->each(function ($pair) {
list($a, $b) = $pair;
Assert::assertTrue($a->is($b));
});
return $this;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment