|
<?php |
|
|
|
namespace Tests\Feature; |
|
use Tests\TestCase; |
|
use Illuminate\Foundation\Testing\RefreshDatabase; |
|
use App\Models\Subscriber; |
|
use App\Mail\SubscriberJoined; |
|
use App\Waitlist; |
|
use App\Subscriber; |
|
use Illuminate\Support\Facades\Mail; |
|
|
|
|
|
class SubscriberTest extends TestCase |
|
{ |
|
//the test is used to ensure that the landing page loads correctly it returns response 200 and shows if a certain text appears. In my case I assert to see more than a text |
|
|
|
protected $index_route = '/'; |
|
protected $store_route = '/post' |
|
protected $subscribed_route = '/subscribed' |
|
|
|
protected function getResponse($data) |
|
{ |
|
return $this->jsonPost(route($this->store_route), $data); |
|
} |
|
|
|
public function test_it_load_information_on_the_landing_page() |
|
{ |
|
$response = $this->get(route($this->index_route)); |
|
|
|
$response->assetStatus(200); |
|
|
|
$response->assertSee('The marketplace'); |
|
|
|
$response->assertSee('Licensed agricultural'); |
|
} |
|
|
|
public function test_it_validate_email_is_required() |
|
{ |
|
$response = $this->getResponse([]); |
|
$this->assertJsonValidationErrors(['email']); |
|
} |
|
|
|
public function test_it_validate_email_is_unique() |
|
{ |
|
$registered_subscriber = factory(Subscriber::class)->create([ |
|
'email' => '[email protected]', |
|
]); |
|
$response = $this->getResponse(['email' => $registered_subscriber->email]); |
|
$this->assertJsonValidationErrors(['email']); |
|
} |
|
|
|
public function test_it_validate_email_is_valid() |
|
{ |
|
$response = $this->getResponse(['email' => 'somerandommail']); |
|
$this->assertJsonValidationErrors(['email']); |
|
} |
|
|
|
public function test_it_persist_subscriber_data_to_the_database() |
|
{ |
|
$response = $this->getResponse(['email' => '[email protected]']); |
|
$this->assertDatabaseHas('subscribers', [ |
|
'email' => '[email protected]' |
|
]); |
|
} |
|
|
|
public function test_it_send_subscriber_mail() |
|
{ |
|
Mail::fake(); |
|
|
|
$email = $this->faker->email; |
|
|
|
$subscriber = factory(Subscriber::class)->create([ |
|
'email' => $email, |
|
]); |
|
|
|
Mail::assertSent($subscriber, function ($email) { |
|
return $mail->hasTo($email); |
|
}); |
|
|
|
} |
|
|
|
public function test_it_get_redirected_to_subscribe_page_successfully() |
|
{ |
|
$response = $this->getResponse(['email' => '[email protected]']): |
|
$response->assertRedirect(route($this->subscribed_route)); |
|
$this->assertSee('Hooray! You\’re on the waitlist.'): |
|
} |
|
|
|
} |