Skip to content

Commit

Permalink
Replace session dependency with request_stack
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Dec 7, 2021
1 parent a17606f commit 7e33c5a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 39 deletions.
50 changes: 37 additions & 13 deletions src/Session/SessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Nucleos\LastFm\Session\Session as LastFmSession;
use Nucleos\LastFm\Session\SessionInterface;
use RuntimeException;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;

final class SessionManager implements SessionManagerInterface
Expand All @@ -22,46 +24,68 @@ final class SessionManager implements SessionManagerInterface
private const SESSION_LASTFM_TOKEN = 'LASTFM_TOKEN';

/**
* @var Session
* @var RequestStack
*/
private $session;
private $requestStack;

public function __construct(Session $session)
public function __construct(RequestStack $requestStack)
{
$this->session = $session;
$this->requestStack = $requestStack;
}

public function isAuthenticated(): bool
{
return (bool) $this->session->get(static::SESSION_LASTFM_TOKEN);
return (bool) $this->getSession()->get(static::SESSION_LASTFM_TOKEN);
}

public function getUsername(): ?string
{
return $this->session->get(static::SESSION_LASTFM_NAME);
return $this->getSession()->get(static::SESSION_LASTFM_NAME);
}

public function store(SessionInterface $lastFmSession): void
{
$this->session->set(static::SESSION_LASTFM_NAME, $lastFmSession->getName());
$this->session->set(static::SESSION_LASTFM_TOKEN, $lastFmSession->getKey());
$session = $this->getSession();

$session->set(static::SESSION_LASTFM_NAME, $lastFmSession->getName());
$session->set(static::SESSION_LASTFM_TOKEN, $lastFmSession->getKey());
}

public function clear(): void
{
$this->session->remove(static::SESSION_LASTFM_NAME);
$this->session->remove(static::SESSION_LASTFM_TOKEN);
$session = $this->getSession();
$session->remove(static::SESSION_LASTFM_NAME);
$session->remove(static::SESSION_LASTFM_TOKEN);
}

public function getSession(): ?SessionInterface
public function getRequestStack(): ?SessionInterface
{
if (!$this->isAuthenticated()) {
return null;
}

$session = $this->getSession();

return new LastFmSession(
$this->session->get(static::SESSION_LASTFM_NAME),
$this->session->get(static::SESSION_LASTFM_TOKEN)
$session->get(static::SESSION_LASTFM_NAME),
$session->get(static::SESSION_LASTFM_TOKEN)
);
}

private function getSession(): Session
{
$request = $this->requestStack->getMainRequest();

if (null === $request) {
throw new RuntimeException('Could not retrieve request.');
}

$session = $request->hasSession() ? $request->getSession() : null;

if (!$session instanceof Session) {
throw new RuntimeException('Could not retrieve session from request.');
}

return $session;
}
}
66 changes: 40 additions & 26 deletions tests/Session/SessionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,89 +13,104 @@

use Nucleos\LastFm\Session\Session as LastFmSession;
use Nucleos\LastFmBundle\Session\SessionManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;

final class SessionManagerTest extends TestCase
{
/**
* @var Session&MockObject
*/
private Session $session;

private RequestStack $requestStack;

protected function setUp(): void
{
$this->session = $this->createMock(Session::class);

$this->request = new Request();
$this->request->setSession($this->session);

$this->requestStack = new RequestStack();
$this->requestStack->push($this->request);
}

public function testIsAuthenticated(): void
{
$session = $this->createMock(Session::class);
$session->method('get')->with('LASTFM_TOKEN')
$this->session->method('get')->with('LASTFM_TOKEN')
->willReturn(true)
;

$manager = new SessionManager($session);
$manager = new SessionManager($this->requestStack);
static::assertTrue($manager->isAuthenticated());
}

public function testIsNotAuthenticated(): void
{
$session = $this->createMock(Session::class);
$session->method('get')->with('LASTFM_TOKEN')
$this->session->method('get')->with('LASTFM_TOKEN')
->willReturn(false)
;

$manager = new SessionManager($session);
$manager = new SessionManager($this->requestStack);
static::assertFalse($manager->isAuthenticated());
}

public function testGetUsername(): void
{
$session = $this->createMock(Session::class);
$session->method('get')->with('LASTFM_NAME')
$this->session->method('get')->with('LASTFM_NAME')
->willReturn('MyUser')
;

$manager = new SessionManager($session);
$manager = new SessionManager($this->requestStack);
static::assertSame('MyUser', $manager->getUsername());
}

public function testGetUsernameNotExist(): void
{
$session = $this->createMock(Session::class);
$session->method('get')->with('LASTFM_NAME')
$this->session = $this->createMock(Session::class);
$this->session->method('get')->with('LASTFM_NAME')
->willReturn(null)
;

$manager = new SessionManager($session);
$manager = new SessionManager($this->requestStack);
static::assertNull($manager->getUsername());
}

public function testStore(): void
{
$lastfmSession = new LastFmSession('YourName', 'YourToken');

$session = $this->createMock(Session::class);
$session->expects(static::exactly(2))->method('set')
$this->session->expects(static::exactly(2))->method('set')
->withConsecutive(
['LASTFM_NAME', 'YourName'],
['LASTFM_TOKEN', 'YourToken'],
)
;

$manager = new SessionManager($session);
$manager = new SessionManager($this->requestStack);
$manager->store($lastfmSession);
}

public function testClear(): void
{
$session = $this->createMock(Session::class);
$session->expects(static::exactly(2))->method('remove')
$this->session->expects(static::exactly(2))->method('remove')
->withConsecutive(
['LASTFM_NAME'],
['LASTFM_TOKEN'],
)
;

$manager = new SessionManager($session);
$manager = new SessionManager($this->requestStack);
$manager->clear();
}

public function testGetSession(): void
{
$session = $this->createMock(Session::class);
$session->expects(static::exactly(3))->method('get')
$this->session->expects(static::exactly(3))->method('get')
->withConsecutive(
['LASTFM_TOKEN'],
['LASTFM_NAME'],
Expand All @@ -108,9 +123,9 @@ public function testGetSession(): void
)
;

$manager = new SessionManager($session);
$manager = new SessionManager($this->requestStack);

$lastfmSession = $manager->getSession();
$lastfmSession = $manager->getRequestStack();

static::assertNotNull($lastfmSession);
static::assertSame('MyUser', $lastfmSession->getName());
Expand All @@ -119,13 +134,12 @@ public function testGetSession(): void

public function testGetSessionWithNoAuth(): void
{
$session = $this->createMock(Session::class);
$session->method('get')->with('LASTFM_TOKEN')
$this->session->method('get')->with('LASTFM_TOKEN')
->willReturn(null)
;

$manager = new SessionManager($session);
$manager = new SessionManager($this->requestStack);

static::assertNull($manager->getSession());
static::assertNull($manager->getRequestStack());
}
}

0 comments on commit 7e33c5a

Please sign in to comment.