refactoring

This commit is contained in:
Milos Stojanovic
2019-06-14 13:34:21 +02:00
parent fa319eda36
commit b945526c85
8 changed files with 20 additions and 42 deletions

View File

@@ -12,7 +12,6 @@ before_script:
script: script:
- vendor/bin/phpunit --coverage-clover=coverage.xml - vendor/bin/phpunit --coverage-clover=coverage.xml
- composer phpstan
after_success: after_success:
- bash <(curl -s https://codecov.io/bash) - bash <(curl -s https://codecov.io/bash)

View File

@@ -65,7 +65,7 @@ Once everything is ready visit: ```http://localhost:8080```
### Run tests & static analysis ### Run tests & static analysis
``` ```
vendor/bin/phpunit vendor/bin/phpunit
composer phpstan vendor/bin/phpstan analyse ./backend --level=x
``` ```
### Deployment ### Deployment

View File

@@ -67,10 +67,6 @@ class AuthController
return $response->json($errors->firstOfAll(), 422); return $response->json($errors->firstOfAll(), 422);
} }
if ($auth->user() === null) {
return $response->json(['oldpassword' => 'Wrong password'], 422);
}
if (! $auth->authenticate($auth->user()->getUsername(), $request->input('oldpassword'))) { if (! $auth->authenticate($auth->user()->getUsername(), $request->input('oldpassword'))) {
return $response->json(['oldpassword' => 'Wrong password'], 422); return $response->json(['oldpassword' => 'Wrong password'], 422);
} }

View File

@@ -40,25 +40,21 @@ class SessionStorage implements Service, SessionStorageInterface
public function save() public function save()
{ {
return $this->getSession() !== null ? $this->getSession()->save() : false; $this->getSession()->save();
} }
public function set(string $key, $data) public function set(string $key, $data)
{ {
return $this->getSession() !== null ? $this->getSession()->set($key, $data) : false; return $this->getSession()->set($key, $data);
} }
public function get(string $key, $default = null) public function get(string $key, $default = null)
{ {
return $this->getSession() !== null ? $this->getSession()->get($key, $default) : $default; return $this->getSession() ? $this->getSession()->get($key, $default) : $default;
} }
public function invalidate() public function invalidate()
{ {
if ($this->getSession() === null) {
return;
}
if (! $this->getSession()->isStarted()) { if (! $this->getSession()->isStarted()) {
$this->getSession()->start(); $this->getSession()->start();
} }

View File

@@ -36,8 +36,5 @@
"symfony/var-dumper": "^4.2", "symfony/var-dumper": "^4.2",
"league/flysystem-memory": "^1.0", "league/flysystem-memory": "^1.0",
"phpstan/phpstan": "^0.11.8" "phpstan/phpstan": "^0.11.8"
},
"scripts": {
"phpstan": "phpstan analyse ./backend --level=7"
} }
} }

View File

@@ -10,15 +10,29 @@
namespace Tests\Unit\Auth; namespace Tests\Unit\Auth;
use Filegator\Kernel\Request;
use Filegator\Services\Auth\User; use Filegator\Services\Auth\User;
use Filegator\Services\Session\Adapters\SessionStorage;
use Tests\TestCase; use Tests\TestCase;
abstract class AuthTest extends TestCase abstract class AuthTest extends TestCase
{ {
public $auth; public $auth;
protected $session;
public function setUp(): void public function setUp(): void
{ {
$this->session = new SessionStorage(new Request());
$this->session->init([
'session_handler' => 'mockfilesession',
'available' => [
'mockfilesession' => function () {
return new \Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage();
},
],
]);
$this->setAuth(); $this->setAuth();
parent::setUp(); parent::setUp();

View File

@@ -10,9 +10,7 @@
namespace Tests\Unit\Auth; namespace Tests\Unit\Auth;
use Filegator\Kernel\Request;
use Filegator\Services\Auth\Adapters\Database; use Filegator\Services\Auth\Adapters\Database;
use Filegator\Services\Session\Adapters\SessionStorage;
/** /**
* @internal * @internal
@@ -23,17 +21,7 @@ class DatabaseAuthTest extends AuthTest
public function setAuth() public function setAuth()
{ {
$session = new SessionStorage(new Request()); $this->auth = new Database($this->session);
$session->init([
'session_handler' => 'mockfilesession',
'available' => [
'mockfilesession' => function () {
return new \Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage();
},
],
]);
$this->auth = new Database($session);
$this->auth->init([ $this->auth->init([
'driver' => 'pdo', 'driver' => 'pdo',
'dsn' => 'sqlite::memory:', 'dsn' => 'sqlite::memory:',

View File

@@ -10,9 +10,7 @@
namespace Tests\Unit\Auth; namespace Tests\Unit\Auth;
use Filegator\Kernel\Request;
use Filegator\Services\Auth\Adapters\JsonFile; use Filegator\Services\Auth\Adapters\JsonFile;
use Filegator\Services\Session\Adapters\SessionStorage;
/** /**
* @internal * @internal
@@ -32,17 +30,7 @@ class JsonFileTest extends AuthTest
@unlink($this->mock_file); @unlink($this->mock_file);
@touch($this->mock_file.'.blank'); @touch($this->mock_file.'.blank');
$session = new SessionStorage(new Request()); $this->auth = new JsonFile($this->session);
$session->init([
'session_handler' => 'mockfilesession',
'available' => [
'mockfilesession' => function () {
return new \Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage();
},
],
]);
$this->auth = new JsonFile($session);
$this->auth->init([ $this->auth->init([
'file' => $this->mock_file, 'file' => $this->mock_file,
]); ]);