Last active
June 9, 2017 03:16
-
-
Save jasonmccallister/f4ec8f801e55a5fa52772529c422c484 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; | |
use App\Make; | |
use App\User; | |
use App\Year; | |
use App\Listing; | |
use App\UserType; | |
use App\MakeModel; | |
use App\ListingType; | |
use Tests\TestCase; | |
use Illuminate\Foundation\Testing\DatabaseMigrations; | |
use Illuminate\Foundation\Testing\DatabaseTransactions; | |
class BuyersCanSearchListingsTest extends TestCase | |
{ | |
use DatabaseMigrations, DatabaseTransactions; | |
/** @test */ | |
function buyers_can_search_for_listings_by_type() | |
{ | |
// Setup | |
$listing = $this->createMotorcycleListing(); | |
// search for listing | |
$response = $this->post('search', [ | |
'type' => $listing->type->id, | |
'keywords' => 'Harley Davidson Sportster', | |
'price_low' => 6000, | |
'price_high' => 12000, | |
]); | |
// see results do not contain other listing types | |
$response->assertStatus(200); | |
} | |
/** @test */ | |
function invalid_search_redirects_back() | |
{ | |
// test validation, should have redirect | |
$this->post('search', []) | |
->assertStatus(302) | |
->assertRedirect('/'); | |
} | |
protected function createMotorcycleListing() | |
{ | |
$userType = factory(UserType::class)->create([ | |
'name' => 'Seller' | |
]); | |
$user = factory(User::class)->create([ | |
'user_type_id' => $userType->id | |
]); | |
$year = factory(Year::class)->create([ | |
'year' => 2010, | |
]); | |
$make = factory(Make::class)->create([ | |
'name' => 'Harley Davidson' | |
]); | |
$model = factory(MakeModel::class)->create([ | |
'name' => 'Sportster 48', | |
'make_id' => $make->id | |
]); | |
$listingType = factory(ListingType::class)->create([ | |
'name' => 'Motorcycle' | |
]); | |
return factory(Listing::class)->create([ | |
'seller_id' => $user->id, | |
'description' => 'Custom pinstriped Sportster 48 with low mileage', | |
'year_id' => $year, | |
'make_id' => $make->id, | |
'make_model_id' => $model->id, | |
'listing_type_id' => $listingType->id, | |
'price' => 10250, | |
'color' => 'Black', | |
'transmission' => 'Automatic', | |
'body_type' => 'sportster', | |
'meta_data' => null, | |
]); | |
} | |
} |
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\Unit; | |
use App\Listing; | |
use App\ListingType; | |
use Tests\TestCase; | |
use Illuminate\Foundation\Testing\DatabaseMigrations; | |
use Illuminate\Foundation\Testing\DatabaseTransactions; | |
class ListingTest extends TestCase | |
{ | |
use DatabaseMigrations, DatabaseTransactions; | |
/** @test */ | |
function can_search_listing_by_the_listing_type() | |
{ | |
$validType = factory(ListingType::class)->create([ | |
'name' => 'Motorcycle' | |
]); | |
$invalidType = factory(ListingType::class)->create(); | |
factory(Listing::class)->create([ | |
'listing_type_id' => $validType->id | |
]); | |
$results = Listing::byType($validType); | |
$this->assertNotNull($results); | |
$this->assertCount(1, $results->get()); | |
$this->assertNotEquals($invalidType->id, $results->first()->id); | |
$this->assertEquals($validType->id, $results->first()->id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment