1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Christian Gripp <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Core23\LastFmBundle\Tests\Session; |
11
|
|
|
|
12
|
|
|
use Core23\LastFm\Connection\Session as LastFmSession; |
13
|
|
|
use Core23\LastFm\Connection\SessionInterface; |
14
|
|
|
use Core23\LastFmBundle\Session\SessionManager; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
17
|
|
|
|
18
|
|
|
class SessionManagerTest extends TestCase |
19
|
|
|
{ |
20
|
|
View Code Duplication |
public function testIsAuthenticated(): void |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$session = $this->prophesize(Session::class); |
23
|
|
|
$session->get('_CORE23_LASTFM_TOKEN') |
24
|
|
|
->willReturn(true) |
25
|
|
|
; |
26
|
|
|
|
27
|
|
|
$manager = new SessionManager($session->reveal()); |
28
|
|
|
$this->assertTrue($manager->isAuthenticated()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
public function testIsNotAuthenticated(): void |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$session = $this->prophesize(Session::class); |
34
|
|
|
$session->get('_CORE23_LASTFM_TOKEN') |
35
|
|
|
->willReturn(false) |
36
|
|
|
; |
37
|
|
|
|
38
|
|
|
$manager = new SessionManager($session->reveal()); |
39
|
|
|
$this->assertFalse($manager->isAuthenticated()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
View Code Duplication |
public function testGetUsername(): void |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$session = $this->prophesize(Session::class); |
45
|
|
|
$session->get('_CORE23_LASTFM_NAME') |
46
|
|
|
->willReturn('MyUser') |
47
|
|
|
; |
48
|
|
|
|
49
|
|
|
$manager = new SessionManager($session->reveal()); |
50
|
|
|
$this->assertSame('MyUser', $manager->getUsername()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
public function testGetUsernameNotExist(): void |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$session = $this->prophesize(Session::class); |
56
|
|
|
$session->get('_CORE23_LASTFM_NAME') |
57
|
|
|
->willReturn(null) |
58
|
|
|
; |
59
|
|
|
|
60
|
|
|
$manager = new SessionManager($session->reveal()); |
61
|
|
|
$this->assertNull($manager->getUsername()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testStore(): void |
65
|
|
|
{ |
66
|
|
|
$lastfmSession = new LastFmSession('YourName', 'YourToken'); |
67
|
|
|
|
68
|
|
|
$session = $this->prophesize(Session::class); |
69
|
|
|
$session->set('_CORE23_LASTFM_NAME', 'YourName')->shouldBeCalled(); |
70
|
|
|
$session->set('_CORE23_LASTFM_TOKEN', 'YourToken')->shouldBeCalled(); |
71
|
|
|
|
72
|
|
|
$manager = new SessionManager($session->reveal()); |
73
|
|
|
$manager->store($lastfmSession); |
74
|
|
|
|
75
|
|
|
$this->assertTrue(true); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
public function testClear(): void |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$session = $this->prophesize(Session::class); |
81
|
|
|
$session->remove('_CORE23_LASTFM_TOKEN')->shouldBeCalled(); |
82
|
|
|
$session->remove('_CORE23_LASTFM_NAME')->shouldBeCalled(); |
83
|
|
|
|
84
|
|
|
$manager = new SessionManager($session->reveal()); |
85
|
|
|
$manager->clear(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testGetSession(): void |
89
|
|
|
{ |
90
|
|
|
$session = $this->prophesize(Session::class); |
91
|
|
|
$session->get('_CORE23_LASTFM_NAME') |
92
|
|
|
->willReturn('MyUser') |
93
|
|
|
; |
94
|
|
|
$session->get('_CORE23_LASTFM_TOKEN') |
95
|
|
|
->willReturn('TheToken') |
96
|
|
|
; |
97
|
|
|
|
98
|
|
|
$manager = new SessionManager($session->reveal()); |
99
|
|
|
|
100
|
|
|
/** @var SessionInterface $lastfmSession */ |
101
|
|
|
$lastfmSession = $manager->getSession(); |
102
|
|
|
|
103
|
|
|
$this->assertNotNull($lastfmSession); |
104
|
|
|
$this->assertSame('MyUser', $lastfmSession->getName()); |
105
|
|
|
$this->assertSame('TheToken', $lastfmSession->getKey()); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.