summaryrefslogtreecommitdiff
path: root/tests/Unit/Controllers
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Unit/Controllers')
-rw-r--r--tests/Unit/Controllers/AuthControllerTest.php142
-rw-r--r--tests/Unit/Controllers/BaseControllerTest.php2
-rw-r--r--tests/Unit/Controllers/CreditsControllerTest.php16
-rw-r--r--tests/Unit/Controllers/Metrics/ControllerTest.php22
-rw-r--r--tests/Unit/Controllers/Metrics/MetricsEngineTest.php11
-rw-r--r--tests/Unit/Controllers/Stub/ControllerImplementation.php8
6 files changed, 173 insertions, 28 deletions
diff --git a/tests/Unit/Controllers/AuthControllerTest.php b/tests/Unit/Controllers/AuthControllerTest.php
index c5349cda..c3d9659c 100644
--- a/tests/Unit/Controllers/AuthControllerTest.php
+++ b/tests/Unit/Controllers/AuthControllerTest.php
@@ -3,40 +3,166 @@
namespace Engelsystem\Test\Unit\Controllers;
use Engelsystem\Controllers\AuthController;
+use Engelsystem\Helpers\Authenticator;
+use Engelsystem\Http\Exceptions\ValidationException;
+use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
use Engelsystem\Http\UrlGeneratorInterface;
+use Engelsystem\Http\Validation\Validator;
+use Engelsystem\Models\User\Settings;
+use Engelsystem\Models\User\User;
+use Engelsystem\Test\Unit\HasDatabase;
+use Illuminate\Support\Collection;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
+use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
+use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
class AuthControllerTest extends TestCase
{
+ use HasDatabase;
+
/**
* @covers \Engelsystem\Controllers\AuthController::__construct
- * @covers \Engelsystem\Controllers\AuthController::logout
+ * @covers \Engelsystem\Controllers\AuthController::login
+ * @covers \Engelsystem\Controllers\AuthController::showLogin
*/
- public function testLogout()
+ public function testLogin()
{
/** @var Response|MockObject $response */
$response = $this->createMock(Response::class);
/** @var SessionInterface|MockObject $session */
- $session = $this->getMockForAbstractClass(SessionInterface::class);
/** @var UrlGeneratorInterface|MockObject $url */
- $url = $this->getMockForAbstractClass(UrlGeneratorInterface::class);
+ /** @var Authenticator|MockObject $auth */
+ list(, $session, $url, $auth) = $this->getMocks();
$session->expects($this->once())
- ->method('invalidate');
+ ->method('get')
+ ->with('errors', [])
+ ->willReturn(['foo' => 'bar']);
+ $response->expects($this->once())
+ ->method('withView')
+ ->with('pages/login')
+ ->willReturn($response);
+
+ $controller = new AuthController($response, $session, $url, $auth);
+ $controller->login();
+ }
+
+ /**
+ * @covers \Engelsystem\Controllers\AuthController::postLogin
+ */
+ public function testPostLogin()
+ {
+ $this->initDatabase();
+ $request = new Request();
+ /** @var Response|MockObject $response */
+ $response = $this->createMock(Response::class);
+ /** @var UrlGeneratorInterface|MockObject $url */
+ /** @var Authenticator|MockObject $auth */
+ list(, , $url, $auth) = $this->getMocks();
+ $session = new Session(new MockArraySessionStorage());
+ /** @var Validator|MockObject $validator */
+ $validator = new Validator();
+
+ $user = new User([
+ 'name' => 'foo',
+ 'password' => '',
+ 'email' => '',
+ 'api_key' => '',
+ 'last_login_at' => null,
+ ]);
+ $user->forceFill(['id' => 42]);
+ $user->save();
+
+ $settings = new Settings(['language' => 'de_DE', 'theme' => '']);
+ $settings->user()
+ ->associate($user)
+ ->save();
+
+ $auth->expects($this->exactly(2))
+ ->method('authenticate')
+ ->with('foo', 'bar')
+ ->willReturnOnConsecutiveCalls(null, $user);
+
+ $response->expects($this->once())
+ ->method('withView')
+ ->with('pages/login', ['errors' => Collection::make(['auth.not-found']), 'show_password_recovery' => true])
+ ->willReturn($response);
$response->expects($this->once())
->method('redirectTo')
- ->with('https://foo.bar/');
+ ->with('news')
+ ->willReturn($response);
+
+ // No credentials
+ $controller = new AuthController($response, $session, $url, $auth);
+ $controller->setValidator($validator);
+ try {
+ $controller->postLogin($request);
+ $this->fail('Login without credentials possible');
+ } catch (ValidationException $e) {
+ }
+
+ // Missing password
+ $request = new Request([], ['login' => 'foo']);
+ try {
+ $controller->postLogin($request);
+ $this->fail('Login without password possible');
+ } catch (ValidationException $e) {
+ }
+
+ // No user found
+ $request = new Request([], ['login' => 'foo', 'password' => 'bar']);
+ $controller->postLogin($request);
+ $this->assertEquals([], $session->all());
+
+ // Authenticated user
+ $controller->postLogin($request);
+
+ $this->assertNotNull($user->last_login_at);
+ $this->assertEquals(['user_id' => 42, 'locale' => 'de_DE'], $session->all());
+ }
+
+ /**
+ * @covers \Engelsystem\Controllers\AuthController::logout
+ */
+ public function testLogout()
+ {
+ /** @var Response $response */
+ /** @var SessionInterface|MockObject $session */
+ /** @var UrlGeneratorInterface|MockObject $url */
+ /** @var Authenticator|MockObject $auth */
+ list($response, $session, $url, $auth) = $this->getMocks();
+
+ $session->expects($this->once())
+ ->method('invalidate');
$url->expects($this->once())
->method('to')
->with('/')
->willReturn('https://foo.bar/');
- $controller = new AuthController($response, $session, $url);
- $controller->logout();
+ $controller = new AuthController($response, $session, $url, $auth);
+ $return = $controller->logout();
+
+ $this->assertEquals(['https://foo.bar/'], $return->getHeader('location'));
+ }
+
+ /**
+ * @return array
+ */
+ protected function getMocks()
+ {
+ $response = new Response();
+ /** @var SessionInterface|MockObject $session */
+ $session = $this->getMockForAbstractClass(SessionInterface::class);
+ /** @var UrlGeneratorInterface|MockObject $url */
+ $url = $this->getMockForAbstractClass(UrlGeneratorInterface::class);
+ /** @var Authenticator|MockObject $auth */
+ $auth = $this->createMock(Authenticator::class);
+
+ return [$response, $session, $url, $auth];
}
}
diff --git a/tests/Unit/Controllers/BaseControllerTest.php b/tests/Unit/Controllers/BaseControllerTest.php
index 738b538f..2adc9dc7 100644
--- a/tests/Unit/Controllers/BaseControllerTest.php
+++ b/tests/Unit/Controllers/BaseControllerTest.php
@@ -21,5 +21,7 @@ class BaseControllerTest extends TestCase
'dolor',
],
], $controller->getPermissions());
+
+ $this->assertTrue(method_exists($controller, 'setValidator'));
}
}
diff --git a/tests/Unit/Controllers/CreditsControllerTest.php b/tests/Unit/Controllers/CreditsControllerTest.php
index 42ea4ea1..303bf60e 100644
--- a/tests/Unit/Controllers/CreditsControllerTest.php
+++ b/tests/Unit/Controllers/CreditsControllerTest.php
@@ -4,9 +4,10 @@ namespace Engelsystem\Test\Unit\Controllers;
use Engelsystem\Config\Config;
use Engelsystem\Controllers\CreditsController;
+use Engelsystem\Helpers\Version;
use Engelsystem\Http\Response;
+use Engelsystem\Test\Unit\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
-use PHPUnit\Framework\TestCase;
class CreditsControllerTest extends TestCase
{
@@ -19,12 +20,17 @@ class CreditsControllerTest extends TestCase
/** @var Response|MockObject $response */
$response = $this->createMock(Response::class);
$config = new Config(['foo' => 'bar', 'credits' => ['lor' => 'em']]);
+ /** @var Version|MockObject $version */
+ $version = $this->createMock(Version::class);
- $response->expects($this->once())
- ->method('withView')
- ->with('pages/credits.twig', ['credits' => ['lor' => 'em']]);
+ $this->setExpects(
+ $response,
+ 'withView',
+ ['pages/credits.twig', ['credits' => ['lor' => 'em'], 'version' => '42.1.0-test']]
+ );
+ $this->setExpects($version, 'getVersion', [], '42.1.0-test');
- $controller = new CreditsController($response, $config);
+ $controller = new CreditsController($response, $config, $version);
$controller->index();
}
}
diff --git a/tests/Unit/Controllers/Metrics/ControllerTest.php b/tests/Unit/Controllers/Metrics/ControllerTest.php
index 18daa96a..f203200c 100644
--- a/tests/Unit/Controllers/Metrics/ControllerTest.php
+++ b/tests/Unit/Controllers/Metrics/ControllerTest.php
@@ -6,6 +6,7 @@ use Engelsystem\Config\Config;
use Engelsystem\Controllers\Metrics\Controller;
use Engelsystem\Controllers\Metrics\MetricsEngine;
use Engelsystem\Controllers\Metrics\Stats;
+use Engelsystem\Helpers\Version;
use Engelsystem\Http\Exceptions\HttpForbidden;
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
@@ -28,7 +29,8 @@ class ControllerTest extends TestCase
/** @var MetricsEngine|MockObject $engine */
/** @var Stats|MockObject $stats */
/** @var Config $config */
- list($response, $request, $engine, $stats, $config) = $this->getMocks();
+ /** @var Version|MockObject $version */
+ list($response, $request, $engine, $stats, $config, $version) = $this->getMocks();
$request->server = new ServerBag();
$request->server->set('REQUEST_TIME_FLOAT', 0.0123456789);
@@ -37,6 +39,7 @@ class ControllerTest extends TestCase
->method('get')
->willReturnCallback(function ($path, $data) use ($response) {
$this->assertEquals('/metrics', $path);
+ $this->assertArrayHasKey('info', $data);
$this->assertArrayHasKey('users', $data);
$this->assertArrayHasKey('licenses', $data);
$this->assertArrayHasKey('users_working', $data);
@@ -122,7 +125,9 @@ class ControllerTest extends TestCase
'XL' => 'X Large',
]);
- $controller = new Controller($response, $engine, $config, $request, $stats);
+ $this->setExpects($version, 'getVersion', [], '0.42.42');
+
+ $controller = new Controller($response, $engine, $config, $request, $stats, $version);
$controller->metrics();
}
@@ -137,7 +142,8 @@ class ControllerTest extends TestCase
/** @var MetricsEngine|MockObject $engine */
/** @var Stats|MockObject $stats */
/** @var Config $config */
- list($response, $request, $engine, $stats, $config) = $this->getMocks();
+ /** @var Version|MockObject $version */
+ list($response, $request, $engine, $stats, $config, $version) = $this->getMocks();
$response->expects($this->once())
->method('withHeader')
@@ -168,7 +174,7 @@ class ControllerTest extends TestCase
$this->setExpects($stats, 'arrivedUsers', null, 10, $this->exactly(2));
$this->setExpects($stats, 'currentlyWorkingUsers', null, 5);
- $controller = new Controller($response, $engine, $config, $request, $stats);
+ $controller = new Controller($response, $engine, $config, $request, $stats, $version);
$controller->stats();
}
@@ -182,7 +188,8 @@ class ControllerTest extends TestCase
/** @var MetricsEngine|MockObject $engine */
/** @var Stats|MockObject $stats */
/** @var Config $config */
- list($response, $request, $engine, $stats, $config) = $this->getMocks();
+ /** @var Version|MockObject $version */
+ list($response, $request, $engine, $stats, $config, $version) = $this->getMocks();
$request->expects($this->once())
->method('get')
@@ -191,7 +198,7 @@ class ControllerTest extends TestCase
$config->set('api_key', 'fooBar!');
- $controller = new Controller($response, $engine, $config, $request, $stats);
+ $controller = new Controller($response, $engine, $config, $request, $stats, $version);
$this->expectException(HttpForbidden::class);
$this->expectExceptionMessage(json_encode(['error' => 'The api_key is invalid']));
@@ -212,7 +219,8 @@ class ControllerTest extends TestCase
/** @var Stats|MockObject $stats */
$stats = $this->createMock(Stats::class);
$config = new Config();
+ $version = $this->createMock(Version::class);
- return [$response, $request, $engine, $stats, $config];
+ return [$response, $request, $engine, $stats, $config, $version];
}
}
diff --git a/tests/Unit/Controllers/Metrics/MetricsEngineTest.php b/tests/Unit/Controllers/Metrics/MetricsEngineTest.php
index 38817b36..87a7dc88 100644
--- a/tests/Unit/Controllers/Metrics/MetricsEngineTest.php
+++ b/tests/Unit/Controllers/Metrics/MetricsEngineTest.php
@@ -66,4 +66,15 @@ class MetricsEngineTest extends TestCase
$this->assertFalse($engine->canRender('/metrics.foo'));
$this->assertTrue($engine->canRender('/metrics'));
}
+
+ /**
+ * @covers \Engelsystem\Controllers\Metrics\MetricsEngine::share
+ */
+ public function testShare()
+ {
+ $engine = new MetricsEngine();
+
+ $engine->share('foo', 42);
+ $this->assertEquals('', $engine->get('/metrics'));
+ }
}
diff --git a/tests/Unit/Controllers/Stub/ControllerImplementation.php b/tests/Unit/Controllers/Stub/ControllerImplementation.php
index 01d9f250..a8bf538c 100644
--- a/tests/Unit/Controllers/Stub/ControllerImplementation.php
+++ b/tests/Unit/Controllers/Stub/ControllerImplementation.php
@@ -14,12 +14,4 @@ class ControllerImplementation extends BaseController
'dolor',
],
];
-
- /**
- * @param array $permissions
- */
- public function setPermissions(array $permissions)
- {
- $this->permissions = $permissions;
- }
}