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 6549ae3 commit 376350f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

->set(SessionManagerInterface::class, SessionManager::class)
->args([
new Reference('session'),
new Reference('request_stack'),
])

->set(PsrClientConnection::class)
Expand Down
50 changes: 36 additions & 14 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 @@ -21,36 +23,37 @@ final class SessionManager implements SessionManagerInterface

private const SESSION_LASTFM_TOKEN = 'LASTFM_TOKEN';

/**
* @var Session
*/
private $session;
private RequestStack $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->getHttpSession()->get(static::SESSION_LASTFM_TOKEN);
}

public function getUsername(): ?string
{
return $this->session->get(static::SESSION_LASTFM_NAME);
return $this->getHttpSession()->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->getHttpSession();

$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->getHttpSession();

$session->remove(static::SESSION_LASTFM_NAME);
$session->remove(static::SESSION_LASTFM_TOKEN);
}

public function getSession(): ?SessionInterface
Expand All @@ -59,9 +62,28 @@ public function getSession(): ?SessionInterface
return null;
}

$session = $this->getHttpSession();

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 getHttpSession(): 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;
}
}
62 changes: 38 additions & 24 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);

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

$this->requestStack = new RequestStack();
$this->requestStack->push($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,7 +123,7 @@ public function testGetSession(): void
)
;

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

$lastfmSession = $manager->getSession();

Expand All @@ -119,12 +134,11 @@ 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());
}
Expand Down

0 comments on commit 376350f

Please sign in to comment.