Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/SignupCodeTest.php
<?php | <?php | ||||
namespace Tests\Feature; | namespace Tests\Feature; | ||||
use App\SignupCode; | use App\SignupCode; | ||||
use Carbon\Carbon; | |||||
use Tests\TestCase; | use Tests\TestCase; | ||||
use Illuminate\Foundation\Testing\WithFaker; | use Illuminate\Foundation\Testing\WithFaker; | ||||
use Illuminate\Foundation\Testing\RefreshDatabase; | use Illuminate\Foundation\Testing\RefreshDatabase; | ||||
class SignupCodeTest extends TestCase | class SignupCodeTest extends TestCase | ||||
{ | { | ||||
/** | /** | ||||
* Test SignupCode creation | * Test SignupCode creation | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
public function testSignupCodeCreate() | public function testSignupCodeCreate() | ||||
{ | { | ||||
$data = [ | $data = [ | ||||
'data' => [ | 'data' => [ | ||||
'email' => 'User@email.org', | 'email' => 'User@email.org', | ||||
'name' => 'User Name', | 'name' => 'User Name', | ||||
] | ] | ||||
]; | ]; | ||||
$now = new \DateTime('now'); | $now = Carbon::now(); | ||||
$code = SignupCode::create($data); | $code = SignupCode::create($data); | ||||
$this->assertFalse($code->isExpired()); | $this->assertFalse($code->isExpired()); | ||||
$this->assertTrue(strlen($code->code) === SignupCode::CODE_LENGTH); | $this->assertTrue(strlen($code->code) === SignupCode::CODE_LENGTH); | ||||
$this->assertTrue( | $this->assertTrue( | ||||
strlen($code->short_code) === env( | strlen($code->short_code) === env( | ||||
'VERIFICATION_CODE_LENGTH', | 'VERIFICATION_CODE_LENGTH', | ||||
SignupCode::SHORTCODE_LENGTH | SignupCode::SHORTCODE_LENGTH | ||||
) | ) | ||||
); | ); | ||||
$this->assertSame($data['data'], $code->data); | $this->assertSame($data['data'], $code->data); | ||||
$this->assertInstanceOf(\DateTime::class, $code->expires_at); | $this->assertInstanceOf(Carbon::class, $code->expires_at); | ||||
$this->assertSame( | $this->assertSame( | ||||
env('SIGNUP_CODE_EXPIRY', SignupCode::CODE_EXP_HOURS), | env('SIGNUP_CODE_EXPIRY', SignupCode::CODE_EXP_HOURS), | ||||
$code->expires_at->diff($now)->h + 1 | $code->expires_at->diffInHours($now) + 1 | ||||
); | ); | ||||
$inst = SignupCode::find($code->code); | $inst = SignupCode::find($code->code); | ||||
$this->assertInstanceOf(SignupCode::class, $inst); | $this->assertInstanceOf(SignupCode::class, $inst); | ||||
$this->assertSame($inst->code, $code->code); | $this->assertSame($inst->code, $code->code); | ||||
} | } | ||||
} | } |