Test Failed
Push — master ( 900563...486ca0 )
by archer
02:53
created

ApiAuthTest::test_get_appid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace ArcherZdip\LaravelApiAuth\Test;
5
6
7
use Faker\Factory;
8
use PHPUnit\Framework\TestCase;
9
use ArcherZdip\LaravelApiAuth\ApiAuth;
10
use Illuminate\Support\Facades\Artisan;
11
use ArcherZdip\LaravelApiAuth\Models\AppClient;
12
use Illuminate\Foundation\Testing\DatabaseTransactions;
13
14
class ApiAuthTest extends TestCase
15
{
16
    use DatabaseTransactions;
17
18
    /**
19
     * @var string $name
20
     */
21
    protected $name;
22
23
    /**
24
     * @var AppClient $appClient
25
     */
26
    protected $appClient;
27
28
    protected function setUp()
29
    {
30
        parent::setUp();
31
32
        $this->name = Factory::create()->md5;
33
34
        Artisan::call('apikey:generate', [
35
            'name' => $this->name
36
        ]);
37
38
        $this->appClient = AppClient::where('name', '=', $this->name)->first();
39
    }
40
41
    /**
42
     * test genereate token
43
     *
44
     * @throws \Exception
45
     */
46
    public function test_generate_token()
47
    {
48
        $token = ApiAuth::generateToken($this->appClient->appid, time());
49
50
        echo $token;
51
        $this->assertInternalType('string', $token);
52
    }
53
54
    /**
55
     * test get appid
56
     *
57
     * @throws \Exception
58
     */
59
    public function test_get_appid()
60
    {
61
        $appid = ApiAuth::getAppId($this->getGenerateToken());
62
63
        echo $appid;
64
65
        $this->assertInternalType('string', $appid);
66
    }
67
68
    /**
69
     * test token is valid
70
     *
71
     * @throws \Exception
72
     */
73
    public function test_token_is_valid()
74
    {
75
        $succToken = $this->getGenerateToken();
76
        $failToken = '123456';
77
78
        echo $succToken;
79
80
        $this->assertTrue(ApiAuth::isValid($succToken));
81
82
        $this->assertFalse(ApiAuth::isValid($failToken));
83
    }
84
85
    /**
86
     * Get token for test
87
     *
88
     * @return string
89
     * @throws \Exception
90
     */
91
    private function getGenerateToken()
92
    {
93
        return ApiAuth::generateToken($this->appClient->appid, time());
94
    }
95
}