Skip to content

Commit 744ed29

Browse files
authored
Merge pull request #11 from sun-asterisk/feature/unit-test
ref update coverage unit test
2 parents 1635a8a + 6dcc828 commit 744ed29

11 files changed

+670
-128
lines changed

src/Contracts/AuthSessionInterface.php

-8
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,4 @@ public function register(
3636
callable $callback = null,
3737
bool $setGuard = false
3838
): bool;
39-
40-
/**
41-
* [postForgotPassword]
42-
* @param string $email [The user's email for receive token.]
43-
* @param callable|null $callback [The callback function have the token & entity model.]
44-
* @return [bool]
45-
*/
46-
public function postForgotPassword(string $email, callable $callback = null): bool;
4739
}

src/Services/AuthSessionService.php

+1-39
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use SunAsterisk\Auth\Exceptions;
1414
use InvalidArgumentException;
1515

16-
final class AuthSessionService implements Contracts\AuthSessionInterface
16+
class AuthSessionService implements Contracts\AuthSessionInterface
1717
{
1818
/**
1919
* The repository implementation.
@@ -136,44 +136,6 @@ public function register(
136136
return true;
137137
}
138138

139-
/**
140-
* [postForgotPassword]
141-
* @param string $email [The user's email for receive token.]
142-
* @param callable|null $callback [The callback function have the token & entity model.]
143-
* @return [bool]
144-
*/
145-
public function postForgotPassword(string $email, callable $callback = null): bool
146-
{
147-
if (!in_array('email', $this->repository->getFillable())) {
148-
throw new Exceptions\AuthException('Model is have not the email attribute.');
149-
}
150-
// Validate Email
151-
Validator::make(['email' => $email], [
152-
'email' => ['required', 'email'],
153-
])->validate();
154-
// Check Email exists
155-
$item = $this->repository->findByAttribute(['email' => $email]);
156-
if (!$item) {
157-
throw ValidationException::withMessages([
158-
'email' => $this->getEmailInvalidMessage($email),
159-
]);
160-
}
161-
162-
// Generate Token
163-
$obj = [
164-
'id' => $item->id,
165-
'created_at' => Carbon::now()->timestamp,
166-
];
167-
168-
$token = Crypt::encryptString(json_encode($obj));
169-
170-
if (is_callable($callback)) {
171-
call_user_func_array($callback, [$token, $item]);
172-
}
173-
174-
return true;
175-
}
176-
177139
/**
178140
* Get a validator for an incoming login request.
179141
*
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace SunAsterisk\Auth\Tests\Exceptions;
4+
5+
use SunAsterisk\Auth\Tests\TestCase;
6+
use SunAsterisk\Auth\Exceptions\AuthException;
7+
8+
/**
9+
* @covers \SunAsterisk\Auth\Exceptions\AuthException
10+
*/
11+
final class AuthExceptionTest extends TestCase
12+
{
13+
public function test_protected()
14+
{
15+
$exception = new AuthException();
16+
$actual = $this->callAttributeProtectedOrPrivate($exception, 'code');
17+
18+
$this->assertEquals($actual, 40002);
19+
}
20+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace SunAsterisk\Auth\Tests\Exceptions;
4+
5+
use SunAsterisk\Auth\Tests\TestCase;
6+
use SunAsterisk\Auth\Exceptions\JWTException;
7+
8+
/**
9+
* @covers \SunAsterisk\Auth\Exceptions\JWTException
10+
*/
11+
final class JWTExceptionTest extends TestCase
12+
{
13+
public function test_protected()
14+
{
15+
$exception = new JWTException();
16+
$actual = $this->callAttributeProtectedOrPrivate($exception, 'code');
17+
18+
$this->assertEquals($actual, 40003);
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace SunAsterisk\Auth\Tests\Exceptions;
4+
5+
use SunAsterisk\Auth\Tests\TestCase;
6+
use SunAsterisk\Auth\Exceptions\UnauthorizedException;
7+
8+
/**
9+
* @covers \SunAsterisk\Auth\Exceptions\UnauthorizedException
10+
*/
11+
final class UnauthorizedExceptionTest extends TestCase
12+
{
13+
public function test_protected()
14+
{
15+
$exception = new UnauthorizedException();
16+
$actual = $this->callAttributeProtectedOrPrivate($exception, 'code');
17+
18+
$this->assertEquals($actual, 40001);
19+
}
20+
21+
public function test_message()
22+
{
23+
$exception = new UnauthorizedException();
24+
$actual = $this->callAttributeProtectedOrPrivate($exception, 'message');
25+
26+
$this->assertEquals($actual, 'UnauthorizedException.');
27+
}
28+
}

tests/Providers/StorageTest.php

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace SunAsterisk\Auth\Tests\Providers;
4+
5+
use SunAsterisk\Auth\Tests\TestCase;
6+
use SunAsterisk\Auth\Providers\Storage;
7+
use Illuminate\Contracts\Cache\Repository;
8+
use Mockery;
9+
10+
/**
11+
* @covers \SunAsterisk\Auth\Providers\Storage
12+
*/
13+
final class StorageTest extends TestCase
14+
{
15+
protected $cache;
16+
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
$this->cache = Mockery::mock(Repository::class)->makePartial();
21+
}
22+
23+
protected function tearDown(): void
24+
{
25+
parent::tearDown();
26+
Mockery::close();
27+
}
28+
29+
public function test_add()
30+
{
31+
$storage = new Storage($this->cache);
32+
$this->cache->shouldReceive('put')->with('key', 'value', 1)->once();
33+
$storage->add('key', 'value', 1);
34+
35+
$this->assertTrue(true);
36+
}
37+
38+
public function test_get()
39+
{
40+
$storage = new Storage($this->cache);
41+
$this->cache->shouldReceive('get')->with('key')->once()->andReturn(true);
42+
$actual = $storage->get('key');
43+
44+
$this->assertTrue($actual);
45+
}
46+
47+
public function test_has()
48+
{
49+
$storage = new Storage($this->cache);
50+
$this->cache->shouldReceive('has')->with('key')->once()->andReturn(true);
51+
$actual = $storage->has('key');
52+
53+
$this->assertTrue($actual);
54+
}
55+
56+
public function test_destroy()
57+
{
58+
$storage = new Storage($this->cache);
59+
$this->cache->shouldReceive('forget')->with('key')->once()->andReturn(true);
60+
$actual = $storage->destroy('key');
61+
62+
$this->assertTrue($actual);
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace SunAsterisk\Auth\Tests\Repositories;
4+
5+
use SunAsterisk\Auth\Tests\TestCase;
6+
use SunAsterisk\Auth\Repositories\EloquentRepository;
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\Builder;
9+
use Mockery;
10+
11+
/**
12+
* @covers \SunAsterisk\Auth\Repositories\EloquentRepository
13+
*/
14+
final class EloquentRepositoryTest extends TestCase
15+
{
16+
protected $model;
17+
18+
protected function setUp(): void
19+
{
20+
parent::setUp();
21+
$this->model = Mockery::mock(Model::class)->makePartial();
22+
}
23+
24+
protected function tearDown(): void
25+
{
26+
parent::tearDown();
27+
Mockery::close();
28+
}
29+
30+
public function test_provider_asks_model_to_find()
31+
{
32+
$eloquent = new EloquentRepository($this->model);
33+
$expected = (object) [];
34+
$this->model->shouldReceive('find')->with(1)->once()->andReturn($expected);
35+
$actual = $eloquent->find(1);
36+
37+
$this->assertEquals($actual, $expected);
38+
}
39+
40+
public function test_create()
41+
{
42+
$eloquent = new EloquentRepository($this->model);
43+
$expected = (object) [];
44+
$this->model->shouldReceive('create')->with([])->once()->andReturn($expected);
45+
$actual = $eloquent->create([]);
46+
47+
$this->assertEquals($actual, $expected);
48+
}
49+
50+
public function test_update_by_id()
51+
{
52+
$eloquent = new EloquentRepository($this->model);
53+
$this->model->shouldReceive('whereId')->with(1)->once()->andReturn($this->model);
54+
$this->model->shouldReceive('update')->with([])->once()->andReturn(true);
55+
$actual = $eloquent->updateById(1, []);
56+
57+
$this->assertTrue($actual);
58+
}
59+
60+
public function test_find_by_id()
61+
{
62+
$eloquent = new EloquentRepository($this->model);
63+
$expected = (object) [];
64+
$this->model->shouldReceive('find')->with(1)->once()->andReturn($expected);
65+
$actual = $eloquent->findById(1);
66+
67+
$this->assertEquals($actual, $expected);
68+
}
69+
70+
public function test_find_by_attribute()
71+
{
72+
$eloquent = new EloquentRepository($this->model);
73+
$expected = (object) [];
74+
$this->model->shouldReceive('where')->with([])->once()->andReturn($this->model);
75+
$this->model->shouldReceive('first')->once()->andReturn($expected);
76+
$actual = $eloquent->findByAttribute([]);
77+
78+
$this->assertEquals($actual, $expected);
79+
}
80+
81+
public function test_find_by_credentials()
82+
{
83+
$eloquent = new EloquentRepository($this->model);
84+
$expected = (object) [];
85+
$this->model->shouldReceive('where')->with(Mockery::on(function ($closure) {
86+
$builder = Mockery::mock(Builder::class)->makePartial();
87+
$builder->shouldReceive('orWhere')->with('key', 'value')->once()->andReturn($builder);
88+
$closure($builder);
89+
90+
return true;
91+
}))->once()->andReturn($this->model);
92+
$this->model->shouldReceive('where')->with([])->once()->andReturn($this->model);
93+
$this->model->shouldReceive('first')->once()->andReturn($expected);
94+
$actual = $eloquent->findByCredentials(['key' => 'value'], []);
95+
96+
$this->assertEquals($actual, $expected);
97+
}
98+
}

0 commit comments

Comments
 (0)