Skip to content

Commit

Permalink
Added tests for the StorageFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
waltertamboer committed Apr 27, 2016
1 parent 5926e55 commit d31eeb7
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -29,6 +29,7 @@
"phpab/phpab": "dev-master"
},
"require-dev": {
"php-mock/php-mock": "^1.0",
"phpunit/phpunit" : "^4.7",
"scrutinizer/ocular": "^1.3",
"squizlabs/php_codesniffer": "^2.5",
Expand Down
139 changes: 139 additions & 0 deletions tests/PhpAbModuleTest/Service/StorageFactoryTest.php
@@ -0,0 +1,139 @@
<?php
/**
* This file is part of phpab/phpab-module. (https://github.com/phpab/phpab-module)
*
* @link https://github.com/phpab/phpab-module for the canonical source repository
* @copyright Copyright (c) 2015-2016 phpab. (https://github.com/phpab/)
* @license https://raw.githubusercontent.com/phpab/phpab-module/master/LICENSE MIT
*/

namespace PhpAbModule\Service;

use PhpAb\Storage\Cookie;
use PhpAb\Storage\Runtime;
use PhpAb\Storage\Session;
use phpmock\functions\FixedValueFunction;
use phpmock\Mock;
use phpmock\MockBuilder;
use PHPUnit_Framework_TestCase;
use Zend\ServiceManager\ServiceLocatorInterface;

class StorageFactoryTest extends PHPUnit_Framework_TestCase
{
public function testCreateServiceWithInvalidStorage()
{
// Arrange
$serviceLocator = $this->getMockForAbstractClass(ServiceLocatorInterface::class);
$serviceLocator->expects($this->once())->method('get')->with($this->equalTo('Config'))->willReturn([
'phpab' => [
'storage' => 'unknown',
'storage_options' => [
],
],
]);

// Assert
$this->setExpectedException('RuntimeException', 'Invalid storage provider set: unknown');

$service = new StorageFactory();

// Act
$storage = $service->createService($serviceLocator);

// Assert
$this->assertInstanceOf(Runtime::class, $storage);
}

public function testCreateServiceRuntime()
{
// Arrange
$serviceLocator = $this->getMockForAbstractClass(ServiceLocatorInterface::class);
$serviceLocator->expects($this->once())->method('get')->with($this->equalTo('Config'))->willReturn([
'phpab' => [
'storage' => 'runtime',
'storage_options' => [
],
],
]);

$service = new StorageFactory();

// Act
$storage = $service->createService($serviceLocator);

// Assert
$this->assertInstanceOf(Runtime::class, $storage);
}

public function testCreateServiceRuntimeWithoutStorageOptions()
{
// Arrange
$serviceLocator = $this->getMockForAbstractClass(ServiceLocatorInterface::class);
$serviceLocator->expects($this->once())->method('get')->with($this->equalTo('Config'))->willReturn([
'phpab' => [
'storage' => 'runtime',
],
]);

$service = new StorageFactory();

// Act
$storage = $service->createService($serviceLocator);

// Assert
$this->assertInstanceOf(Runtime::class, $storage);
}

public function testCreateServiceCookie()
{
// Arrange
$serviceLocator = $this->getMockForAbstractClass(ServiceLocatorInterface::class);
$serviceLocator->expects($this->once())->method('get')->with($this->equalTo('Config'))->willReturn([
'phpab' => [
'storage' => 'cookie',
'storage_options' => [
'name' => 'namespace',
'ttl' => 3600,
],
],
]);

$service = new StorageFactory();

// Act
$storage = $service->createService($serviceLocator);

// Assert
$this->assertInstanceOf(Cookie::class, $storage);
}

public function testCreateServiceSession()
{
// Arrange
$builder = new MockBuilder();
$builder->setNamespace('PhpAb\\Storage');
$builder->setName("session_status");
$builder->setFunctionProvider(new FixedValueFunction(PHP_SESSION_ACTIVE));

$sessionStatusMock = $builder->build();
$sessionStatusMock->enable();

$serviceLocator = $this->getMockForAbstractClass(ServiceLocatorInterface::class);
$serviceLocator->expects($this->once())->method('get')->with($this->equalTo('Config'))->willReturn([
'phpab' => [
'storage' => 'session',
'storage_options' => [
'name' => 'namespace',
],
],
]);

$service = new StorageFactory();

// Act
$storage = $service->createService($serviceLocator);

// Assert
$this->assertInstanceOf(Session::class, $storage);
}
}

0 comments on commit d31eeb7

Please sign in to comment.