-
-
Save adamwathan/513d476de6c5d6213c0cd9c62f4ac154 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Feature\Makers; | |
use App\User; | |
use App\Product; | |
use App\Purchase; | |
use Carbon\Carbon; | |
use Tests\TestCase; | |
use Illuminate\Database\Eloquent\Collection; | |
use Illuminate\Foundation\Testing\TestResponse; | |
use Illuminate\Foundation\Testing\DatabaseMigrations; | |
class ViewOrdersTest extends TestCase | |
{ | |
use DatabaseMigrations; | |
protected function setUp() | |
{ | |
parent::setUp(); | |
TestResponse::macro('data', function ($key) { | |
return $this->original->getData()[$key]; | |
}); | |
Collection::macro('eachSpread', function ($callback) { | |
return $this->each(function ($chunk) use ($callback) { | |
return $callback(...$chunk); | |
}); | |
}); | |
Collection::macro('assertEquals', function ($collection) { | |
$collection = new static($collection); | |
Assert::assertCount($collection->count(), $this); | |
$this->zip($collection)->eachSpread(function ($a, $b) { | |
Assert::assertTrue($a->is($b)); | |
}); | |
}); | |
Collection::macro('assertContains', function ($item) { | |
Assert::assertTrue($this->contains($item)); | |
}); | |
Collection::macro('assertNotContains', function ($item) { | |
Assert::assertFalse($this->contains($item)); | |
}); | |
} | |
/** @test */ | |
function guests_cannot_view_a_makers_orders() | |
{ | |
$response = $this->withExceptionHandling()->get(route('orders.index')); | |
$response->assertStatus(302); | |
$response->assertRedirect('/login'); | |
} | |
/** @test */ | |
function a_maker_can_only_view_a_list_of_their_own_orders() | |
{ | |
$user = factory(User::class)->create(); | |
$otherUser = factory(User::class)->create(); | |
$product = factory(Product::class)->states('publishable')->create(['user_id' => $user->id,]); | |
$otherUsersProduct = factory(Product::class)->states('publishable')->create(['user_id' => $otherUser->id,]); | |
$purchaseA = factory(Purchase::class)->create(['product_id' => $product->id]); | |
$purchaseB = factory(Purchase::class)->create(['product_id' => $product->id]); | |
$otherUsersPurchase = factory(Purchase::class)->create(['product_id' => $otherUsersProduct->id]); | |
$purchaseC = factory(Purchase::class)->create(['product_id' => $product->id]); | |
$response = $this->actingAs($user)->get(route('orders.index')); | |
$response->assertStatus(200); | |
$response->data('purchases')->assertContains($purchaseA); | |
$response->data('purchases')->assertContains($purchaseB); | |
$response->data('purchases')->assertContains($purchaseC); | |
$response->data('purchases')->assertNotContains($otherUsersPurchase); | |
} | |
/** @test */ | |
function orders_are_sorted_from_newest_to_oldest() | |
{ | |
$user = factory(User::class)->create(); | |
$product = factory(Product::class)->states('publishable')->create(['user_id' => $user->id,]); | |
$purchaseA = factory(Purchase::class)->create([ | |
'product_id' => $product->id, | |
'created_at' => Carbon::parse('-4 days'), | |
]); | |
$purchaseB = factory(Purchase::class)->create([ | |
'product_id' => $product->id, | |
'created_at' => Carbon::parse('-1 day'), | |
]); | |
$purchaseC = factory(Purchase::class)->create([ | |
'product_id' => $product->id, | |
'created_at' => Carbon::parse('-3 days'), | |
]); | |
$response = $this->actingAs($user)->get(route('orders.index')); | |
$response->assertStatus(200); | |
$response->data('purchases')->assertEquals([ | |
$purchaseB, | |
$purchaseC, | |
$purchaseA, | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment