Adding register tests (#280)
* Adding register test, faking mails * Apply fixes from StyleCI (#279) [ci skip] [skip ci] Co-authored-by: Sergio Brighenti <SergiX44@users.noreply.github.com> * Reduce method complexity Co-authored-by: Sergio Brighenti <SergiX44@users.noreply.github.com>
This commit is contained in:
parent
c34671d700
commit
50c013af8b
|
@ -7,6 +7,11 @@ use InvalidArgumentException;
|
||||||
|
|
||||||
class Mail
|
class Mail
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private static $testing = false;
|
||||||
|
|
||||||
protected $fromMail = 'no-reply@example.com';
|
protected $fromMail = 'no-reply@example.com';
|
||||||
protected $fromName;
|
protected $fromName;
|
||||||
|
|
||||||
|
@ -26,6 +31,14 @@ class Mail
|
||||||
return new self();
|
return new self();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This will skip the email send
|
||||||
|
*/
|
||||||
|
public static function fake()
|
||||||
|
{
|
||||||
|
self::$testing = true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $mail
|
* @param $mail
|
||||||
* @param $name
|
* @param $name
|
||||||
|
@ -92,6 +105,22 @@ class Mail
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set headers before send
|
||||||
|
*/
|
||||||
|
protected function setHeaders()
|
||||||
|
{
|
||||||
|
if ($this->fromName === null) {
|
||||||
|
$this->addRequiredHeader("From: $this->fromMail");
|
||||||
|
} else {
|
||||||
|
$this->addRequiredHeader("From: $this->fromName <$this->fromMail>");
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->addRequiredHeader('X-Mailer: PHP/'.phpversion())
|
||||||
|
->addRequiredHeader('MIME-Version: 1.0')
|
||||||
|
->addRequiredHeader('Content-Type: text/html; charset=utf-8');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
|
@ -109,19 +138,15 @@ class Mail
|
||||||
throw new InvalidArgumentException('Message cannot be null.');
|
throw new InvalidArgumentException('Message cannot be null.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->fromName === null) {
|
$this->setHeaders();
|
||||||
$this->addRequiredHeader("From: $this->fromMail");
|
|
||||||
} else {
|
|
||||||
$this->addRequiredHeader("From: $this->fromName <$this->fromMail>");
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->addRequiredHeader('X-Mailer: PHP/'.phpversion());
|
|
||||||
$this->addRequiredHeader('MIME-Version: 1.0');
|
|
||||||
$this->addRequiredHeader('Content-Type: text/html; charset=utf-8');
|
|
||||||
|
|
||||||
$this->headers .= $this->additionalHeaders;
|
$this->headers .= $this->additionalHeaders;
|
||||||
$message = html_entity_decode($this->message);
|
$message = html_entity_decode($this->message);
|
||||||
|
|
||||||
|
if (self::$testing) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
return (int) mail($this->to, $this->subject, "<html><body>$message</body></html>", $this->headers);
|
return (int) mail($this->to, $this->subject, "<html><body>$message</body></html>", $this->headers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
71
tests/Feature/Auth/RegisterControllerTest.php
Normal file
71
tests/Feature/Auth/RegisterControllerTest.php
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace Tests\Feature\Auth;
|
||||||
|
|
||||||
|
use App\Web\Mail;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class RegisterControllerTest extends TestCase
|
||||||
|
{
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->updateSetting('register_enabled', 'on');
|
||||||
|
Mail::fake();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_loads_the_register_page()
|
||||||
|
{
|
||||||
|
$response = $this->get(route('register'));
|
||||||
|
|
||||||
|
$this->assertSame(200, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_give_404_if_registration_are_off()
|
||||||
|
{
|
||||||
|
$this->updateSetting('register_enabled', 'off');
|
||||||
|
$response = $this->get(route('register'));
|
||||||
|
|
||||||
|
$this->assertSame(404, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_redirect_to_home_if_logged_in()
|
||||||
|
{
|
||||||
|
$this->createAdminUser();
|
||||||
|
|
||||||
|
$response = $this->get(route('login.show'));
|
||||||
|
$form = $this->getCrawler($response)
|
||||||
|
->selectButton('Login')
|
||||||
|
->form([
|
||||||
|
'username' => 'admin@example.com',
|
||||||
|
'password' => 'admin',
|
||||||
|
], 'POST');
|
||||||
|
$this->submitForm($form);
|
||||||
|
|
||||||
|
$response = $this->get(route('register'));
|
||||||
|
$this->assertSame(302, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_register_a_new_user()
|
||||||
|
{
|
||||||
|
$response = $this->get(route('register'));
|
||||||
|
$form = $this->getCrawler($response)
|
||||||
|
->selectButton('Register')
|
||||||
|
->form([
|
||||||
|
'email' => 'mario@example.com',
|
||||||
|
'username' => 'Super Mario',
|
||||||
|
'password' => 'user',
|
||||||
|
], 'POST');
|
||||||
|
$response = $this->submitForm($form);
|
||||||
|
$this->assertSame(302, $response->getStatusCode());
|
||||||
|
$this->assertSame(route('login'), $response->getHeaderLine('Location'));
|
||||||
|
|
||||||
|
$result = $this->database()->query('SELECT * FROM users WHERE email = "mario@example.com"')->fetch();
|
||||||
|
$this->assertIsObject($result);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue