summaryrefslogtreecommitdiff
path: root/tests/Unit
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-09-15 17:24:59 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-09-16 14:56:07 +0200
commit104e4f4c437376eb739dd3ef2de603855947a557 (patch)
treeb28f33e6d2fbb2aad5af074d2eb1d2e02148c6cd /tests/Unit
parentedeab5e75ffa02b075c151ca03ea1038f61e4396 (diff)
Session: Added Symfony PDO backend
Diffstat (limited to 'tests/Unit')
-rw-r--r--tests/Unit/Database/DatabaseServiceProviderTest.php6
-rw-r--r--tests/Unit/Http/SessionServiceProviderTest.php75
-rw-r--r--tests/Unit/Models/BaseModelTest.php24
-rw-r--r--tests/Unit/Models/Stub/BaseModelImplementation.php12
4 files changed, 110 insertions, 7 deletions
diff --git a/tests/Unit/Database/DatabaseServiceProviderTest.php b/tests/Unit/Database/DatabaseServiceProviderTest.php
index 0f259036..7dae065f 100644
--- a/tests/Unit/Database/DatabaseServiceProviderTest.php
+++ b/tests/Unit/Database/DatabaseServiceProviderTest.php
@@ -2,6 +2,7 @@
namespace Engelsystem\Test\Unit\Database;
+use Engelsystem\Application;
use Engelsystem\Config\Config;
use Engelsystem\Database\Database;
use Engelsystem\Database\DatabaseServiceProvider;
@@ -10,6 +11,7 @@ use Engelsystem\Test\Unit\ServiceProviderTest;
use Exception;
use Illuminate\Database\Capsule\Manager as CapsuleManager;
use Illuminate\Database\Connection;
+use PDO;
use PDOException;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
@@ -117,12 +119,12 @@ class DatabaseServiceProviderTest extends ServiceProviderTest
$this->setExpects($connection, 'useDefaultSchemaGrammar');
$connection->expects($this->once())
->method('getPdo')
- ->willReturnCallback(function () use ($getPdoThrowException) {
+ ->willReturnCallback(function () use ($getPdoThrowException, $pdo) {
if ($getPdoThrowException) {
throw new PDOException();
}
- return '';
+ return $pdo;
});
$this->setExpects($dbManager, 'getConnection', [], $connection, $this->atLeastOnce());
diff --git a/tests/Unit/Http/SessionServiceProviderTest.php b/tests/Unit/Http/SessionServiceProviderTest.php
index d0125bc2..5e4575b3 100644
--- a/tests/Unit/Http/SessionServiceProviderTest.php
+++ b/tests/Unit/Http/SessionServiceProviderTest.php
@@ -2,11 +2,14 @@
namespace Engelsystem\Test\Unit\Http;
+use Engelsystem\Config\Config;
use Engelsystem\Http\Request;
use Engelsystem\Http\SessionServiceProvider;
use Engelsystem\Test\Unit\ServiceProviderTest;
+use PDO;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
use Symfony\Component\HttpFoundation\Session\Session;
+use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface as StorageInterface;
@@ -23,6 +26,9 @@ class SessionServiceProviderTest extends ServiceProviderTest
$sessionStorage = $this->getMockForAbstractClass(StorageInterface::class);
$sessionStorage2 = $this->getMockForAbstractClass(StorageInterface::class);
+ $pdoSessionHandler = $this->getMockBuilder(PdoSessionHandler::class)
+ ->disableOriginalConstructor()
+ ->getMock();
$session = $this->getSessionMock();
$request = $this->getRequestMock();
@@ -32,22 +38,54 @@ class SessionServiceProviderTest extends ServiceProviderTest
->setConstructorArgs([$app])
->setMethods(['isCli'])
->getMock();
- $serviceProvider->expects($this->exactly(2))
+
+ /** @var Config|MockObject $config */
+ $config = $this->createMock(Config::class);
+ /** @var PDO|MockObject $pdo */
+ $pdo = $this->getMockBuilder(PDO::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $serviceProvider->expects($this->exactly(3))
->method('isCli')
- ->willReturnOnConsecutiveCalls(true, false);
+ ->willReturnOnConsecutiveCalls(true, false, false);
- $app->expects($this->exactly(4))
+ $app->expects($this->exactly(7))
->method('make')
->withConsecutive(
[MockArraySessionStorage::class],
[Session::class],
- [NativeSessionStorage::class, ['options' => ['cookie_httponly' => true]]],
+ [
+ NativeSessionStorage::class,
+ ['options' => ['cookie_httponly' => true, 'name' => 'session'], 'handler' => null]
+ ],
+ [Session::class],
+ [
+ PdoSessionHandler::class,
+ [
+ 'pdoOrDsn' => $pdo,
+ 'options' => [
+ 'db_table' => 'sessions',
+ 'db_id_col' => 'id',
+ 'db_data_col' => 'payload',
+ 'db_lifetime_col' => 'lifetime',
+ 'db_time_col' => 'last_activity',
+ ],
+ ]
+ ],
+ [
+ NativeSessionStorage::class,
+ ['options' => ['cookie_httponly' => true, 'name' => 'foobar'], 'handler' => $pdoSessionHandler]
+ ],
[Session::class]
)
->willReturnOnConsecutiveCalls(
$sessionStorage,
$session,
$sessionStorage2,
+ $session,
+ $pdoSessionHandler,
+ $sessionStorage2,
$session
);
$app->expects($this->atLeastOnce())
@@ -58,13 +96,40 @@ class SessionServiceProviderTest extends ServiceProviderTest
['session', $session]
);
+ $app->expects($this->exactly(6))
+ ->method('get')
+ ->withConsecutive(
+ ['request'],
+ ['config'],
+ ['request'],
+ ['config'],
+ ['db.pdo'],
+ ['request']
+ )
+ ->willReturnOnConsecutiveCalls(
+ $request,
+ $config,
+ $request,
+ $config,
+ $pdo,
+ $request
+ );
+
+ $config->expects($this->exactly(2))
+ ->method('get')
+ ->with('session')
+ ->willReturnOnConsecutiveCalls(
+ ['driver' => 'native', 'name' => 'session'],
+ ['driver' => 'pdo', 'name' => 'foobar']
+ );
+
$this->setExpects($app, 'bind', [StorageInterface::class, 'session.storage'], null, $this->atLeastOnce());
- $this->setExpects($app, 'get', ['request'], $request, $this->atLeastOnce());
$this->setExpects($request, 'setSession', [$session], null, $this->atLeastOnce());
$this->setExpects($session, 'start', null, null, $this->atLeastOnce());
$serviceProvider->register();
$serviceProvider->register();
+ $serviceProvider->register();
}
/**
diff --git a/tests/Unit/Models/BaseModelTest.php b/tests/Unit/Models/BaseModelTest.php
index 52cb8c7b..9af55fa1 100644
--- a/tests/Unit/Models/BaseModelTest.php
+++ b/tests/Unit/Models/BaseModelTest.php
@@ -3,6 +3,8 @@
namespace Engelsystem\Test\Unit\Models;
use Engelsystem\Test\Unit\Models\Stub\BaseModelImplementation;
+use Illuminate\Database\Eloquent\Builder as QueryBuilder;
+use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class BaseModelTest extends TestCase
@@ -19,4 +21,26 @@ class BaseModelTest extends TestCase
$this->assertEquals('bar', $newModel->foo);
$this->assertEquals(1, $newModel->saveCount);
}
+
+ /**
+ * @covers \Engelsystem\Models\BaseModel::find
+ */
+ public function testFind()
+ {
+ /** @var QueryBuilder|MockObject $queryBuilder */
+ $queryBuilder = $this->createMock(QueryBuilder::class);
+ BaseModelImplementation::$queryBuilder = $queryBuilder;
+
+ $anotherModel = new BaseModelImplementation();
+
+ $queryBuilder->expects($this->once())
+ ->method('find')
+ ->with(1337, ['foo', 'bar'])
+ ->willReturn($anotherModel);
+
+ $model = new BaseModelImplementation();
+ $newModel = $model->find(1337, ['foo', 'bar']);
+
+ $this->assertEquals($anotherModel, $newModel);
+ }
}
diff --git a/tests/Unit/Models/Stub/BaseModelImplementation.php b/tests/Unit/Models/Stub/BaseModelImplementation.php
index 4aa1ef0b..70643b03 100644
--- a/tests/Unit/Models/Stub/BaseModelImplementation.php
+++ b/tests/Unit/Models/Stub/BaseModelImplementation.php
@@ -3,6 +3,7 @@
namespace Engelsystem\Test\Unit\Models\Stub;
use Engelsystem\Models\BaseModel;
+use Illuminate\Database\Eloquent\Builder as QueryBuilder;
/**
* @property string foo
@@ -15,6 +16,9 @@ class BaseModelImplementation extends BaseModel
/** @var int */
public $saveCount = 0;
+ /** @var QueryBuilder */
+ public static $queryBuilder = null;
+
/**
* @param array $options
* @return bool
@@ -24,4 +28,12 @@ class BaseModelImplementation extends BaseModel
$this->saveCount++;
return true;
}
+
+ /**
+ * @return QueryBuilder
+ */
+ public static function query()
+ {
+ return self::$queryBuilder;
+ }
}