Added tests

This commit is contained in:
R. Eric Wheeler 2016-08-08 07:22:38 -07:00
parent d21a45c47b
commit 9651816516
9 changed files with 245 additions and 256 deletions

23
.travis.yml Normal file
View File

@ -0,0 +1,23 @@
language: php
php:
- '5.3'
- '5.4'
- '5.5'
- '5.6'
- '7.0'
- hhvm
- nightly
matrix:
allow_failures:
- php: '5.3'
- php: '5.4'
- php: '5.5'
- php: hhvm
- php: nightly
before_script:
- composer install --dev
script:
- mkdir -p build/logs
- php vendor/bin/phpunit -c phpunit.xml.dist

View File

@ -14,8 +14,6 @@
use Sikofitt\Mocks\Drupal\Variables;
define('CONFIG_PATH', __DIR__ . '/storage');
if(!function_exists('variable_get'))
{

View File

@ -1,15 +1,14 @@
{
"name": "sikofitt/mocks-drupal-variables",
"description": "Mock for drupal variable functions variable_(set,get,del)",
"keywords": ["drupal", "mock", "config"],
"type": "library",
"require": {
"php": ">=5.4",
"hassankhan/config": "~0.10",
"symfony/filesystem": "^3.1"
"php": ">=5.4"
},
"require-dev": {
"phpunit/phpunit": "^5.5"
"phpunit/phpunit": "^5.5",
"phpunit/php-code-coverage": "4.0.*"
},
"license": "GPL-3.0",
"authors": [

113
composer.lock generated
View File

@ -4,116 +4,9 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "4ca6c1efcbc049f58ae1ed1a3a592c11",
"content-hash": "c7d470d7f395e09723985f30dd2ecc5e",
"packages": [
{
"name": "hassankhan/config",
"version": "0.10.0",
"source": {
"type": "git",
"url": "https://github.com/hassankhan/config.git",
"reference": "06ac500348af033f1a2e44dc357ca86282626d4a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/hassankhan/config/zipball/06ac500348af033f1a2e44dc357ca86282626d4a",
"reference": "06ac500348af033f1a2e44dc357ca86282626d4a",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"scrutinizer/ocular": "~1.1",
"squizlabs/php_codesniffer": "~2.2"
},
"suggest": {
"symfony/yaml": "~2.5"
},
"type": "library",
"autoload": {
"psr-4": {
"Noodlehaus\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Hassan Khan",
"homepage": "http://hassankhan.me/",
"role": "Developer"
}
],
"description": "Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files",
"homepage": "http://hassankhan.me/config/",
"keywords": [
"config",
"configuration",
"ini",
"json",
"microphp",
"unframework",
"xml",
"yaml",
"yml"
],
"time": "2016-02-11 16:21:17"
},
{
"name": "symfony/filesystem",
"version": "v3.1.3",
"source": {
"type": "git",
"url": "https://github.com/symfony/filesystem.git",
"reference": "bb29adceb552d202b6416ede373529338136e84f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/filesystem/zipball/bb29adceb552d202b6416ede373529338136e84f",
"reference": "bb29adceb552d202b6416ede373529338136e84f",
"shasum": ""
},
"require": {
"php": ">=5.5.9"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.1-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Filesystem\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Filesystem Component",
"homepage": "https://symfony.com",
"time": "2016-07-20 05:44:26"
}
],
"hash": "34225ddc86ef42457ccd546bb137f3c3",
"content-hash": "be291abae27306620a5badf0f0af23c3",
"packages": [],
"packages-dev": [
{
"name": "doctrine/instantiator",

View File

@ -7,15 +7,21 @@
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutTodoAnnotatedTests="true"
stopOnFailure="false"
syntaxCheck="false"
verbose="true"
colors="true">
<testsuite>
<directory suffix="Test.php">tests</directory>
<testsuite name="Drupal mock variables, variable_* tests">
<directory suffix="Test.php">./tests</directory>
</testsuite>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml" />
</logging>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>

View File

@ -14,56 +14,33 @@
namespace Sikofitt\Mocks\Drupal;
use Noodlehaus\AbstractConfig;
use Noodlehaus\Config;
use Noodlehaus\Exception\EmptyDirectoryException;
use SebastianBergmann\CodeCoverage\Report\Html\File;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;
/**
* Class Variables
*
* @package Sikofitt\Mocks\Drupal
*/
class Variables extends AbstractConfig
class Variables extends \ArrayObject
{
const VARIABLE_TEMP_DIR = '/sikofitt/mocks/drupal';
const VARIABLE_NAMESPACE = '/sikofitt/mocks/drupal';
public function tempDirectoryExists()
{
/**
*
*/
const VARIABLE_NAMESPACE = '/sikofitt/mocks/drupal/';
return file_exists(sys_get_temp_dir() . self::VARIABLE_TEMP_DIR);
/**
*
*/
const VARIABLE_TEMP_FILE_NAME = 'config.tmp';
}
/**
* @var
*/
private $config;
private function tempFileExists()
{
return file_exists(sys_get_temp_dir() . self::VARIABLE_TEMP_DIR . '/config.yml');
}
private function createTempDirectory()
{
mkdir(sys_get_temp_dir() . '/sikofitt/mocks/drupal', 0775, true);
}
private function touchConfigFile()
{
touch(sys_get_temp_dir() . self::VARIABLE_TEMP_DIR . '/config.yml');
}
private function parseConfig()
{
if (null === $config = Yaml::parse(file_get_contents(sys_get_temp_dir() . self::VARIABLE_TEMP_DIR . '/config.yml'))) {
$config = [];
}
return $config;
}
public function __construct()
/**
* Variables constructor.
*/
public function __construct($data = null)
{
if (!$this->tempDirectoryExists()) {
$this->createTempDirectory();
@ -71,107 +48,123 @@ class Variables extends AbstractConfig
if (!$this->tempFileExists()) {
$this->touchConfigFile();
}
$config = $this->parseConfig();
parent::__construct($config);
}
/**
* @return array|null
*/
public function getData()
{
return $this->data;
}
public function variable_set($variableName, $variableValue)
{
$this->set($variableName, serialize($variableValue));
$this->writeConfig();
}
private function cleanTempDirectory()
{
$tempDirectory = new Filesystem();
$tempDirectory->remove(sys_get_temp_dir() . '/sikofitt');
return false === $tempDirectory->exists(sys_get_temp_dir() . '/sikofitt');
}
private function makeTempDirectory()
{
$tempDirectory = new Filesystem();
$tempDirectory->mkdir(sys_get_temp_dir() . self::VARIABLE_NAMESPACE);
return $tempDirectory->exists(sys_get_temp_dir() . self::VARIABLE_TEMP_DIR);
}
private function writeConfig()
{
$yaml = Yaml::dump($this->all());
file_put_contents(sys_get_temp_dir() . self::VARIABLE_TEMP_DIR . '/config.yml', $yaml);
}
public function variable_get($variableName, $default = null)
{
$variableValue = $this->get($variableName, $default);
return unserialize($variableValue);
}
public function variable_del($variableName)
{
if ($this->offsetExists($variableName)) {
unset($this->data[$variableName]);
if($data !== null && (is_array($data) || is_object($data)))
{
$this->exchangeArray($data);
$this->writeConfig();
}
$this->writeConfig();
$this->parseConfig();
parent::__construct($this->config);
}
private function resetConfig()
/**
* @param $variableName
* @param null $default
*
* @return mixed|null
*/
public function variable_get($variableName, $default = null)
{
if ($this->offsetExists($variableName)) {
return unserialize($this->offsetGet($variableName));
} else {
return $default;
}
}
/**
* @param $variableName
* @param $variableValue
*
* @return null
*/
public function variable_set($variableName, $variableValue)
{
$this->offsetSet($variableName, serialize($variableValue));
$this->writeConfig();
return null;
}
/**
* @param $variableName
*
* @return null
*/
public function variable_del($variableName)
{
if ($this->offsetExists($variableName)) {
$this->offsetUnset($variableName);
$this->writeConfig();
}
return null;
}
/**
* @return bool
*/
public function tempDirectoryExists()
{
$config = new Config(CONFIG_PATH);
$this->setData($config->all());
return file_exists(sys_get_temp_dir().self::VARIABLE_NAMESPACE);
}
/**
* @param $data
*
* @return $this
*/
public function setData($data)
/**
* @return bool
*/
private function tempFileExists()
{
$this->data = $data;
return $this;
return file_exists(
sys_get_temp_dir().self::VARIABLE_NAMESPACE.self::VARIABLE_TEMP_FILE_NAME);
}
private function is_serialized($data)
/**
* @return void
*/
private function createTempDirectory()
{
// if it isn't a string, it isn't serialized
if (!is_string($data))
return false;
$data = trim($data);
if ('N;' == $data)
return true;
if (!preg_match('/^([adObis]):/', $data, $badions))
return false;
switch ($badions[1]) {
case 'a' :
case 'O' :
case 's' :
if (preg_match("/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data))
return true;
break;
case 'b' :
case 'i' :
case 'd' :
if (preg_match("/^{$badions[1]}:[0-9.E-]+;\$/", $data))
return true;
break;
try {
mkdir(sys_get_temp_dir().self::VARIABLE_NAMESPACE, 0775, true);
} catch (\Exception $e) {
trigger_error($e->getMessage(), E_USER_ERROR);
}
return false;
}
}
/**
* @return void
*/
private function touchConfigFile()
{
try {
touch(sys_get_temp_dir().self::VARIABLE_NAMESPACE.self::VARIABLE_TEMP_FILE_NAME);
} catch (\Exception $e) {
trigger_error($e->getMessage(), E_USER_ERROR);
}
}
/**
* @return void
*/
private function parseConfig()
{
if (null === $this->config = json_decode(
file_get_contents(
sys_get_temp_dir().self::VARIABLE_NAMESPACE.self::VARIABLE_TEMP_FILE_NAME))
) {
$this->config = [];
}
}
/**
* @return void
*/
private function writeConfig()
{
$data = json_encode($this->getArrayCopy());
try {
file_put_contents(
sys_get_temp_dir().self::VARIABLE_NAMESPACE.self::VARIABLE_TEMP_FILE_NAME,
$data);
} catch (\Exception $e) {
trigger_error($e->getMessage(), E_USER_ERROR);
}
}
}

View File

View File

@ -3,22 +3,99 @@
* Created by PhpStorm.
* User: Eric
* Date: 08/05/2016
* Time: 5:47 PM
* Time: 5:47 PM.
*/
namespace Sikofitt\Tests\Mocks\Drupal;
use Sikofitt\Mocks\Drupal\Variables;
class VariablesTest extends \PHPUnit_Framework_TestCase
{
private $object;
private $keyedArrayData;
private $variables;
public function setUp()
{
$this->object = new \stdClass();
$this->object->objectVariableName1 = 'objectVariableValue1';
$this->object->objectVariableName2 = 'objectVariableValue2';
$this->object->objectVariableName3 = 'objectVariableValue3';
$this->keyedArrayData = array(
'subVariableName1' => 'subVariableValue1',
'subVariableName2' => 'subVariableValue2',
'subVariableName3' => 'subVariableValue3',
);
$testData = array(
'variableName1' => serialize('variableValue1'),
'variableName2' => serialize('variableValue2'),
'variableName3' => serialize(range(0, 20)),
'variableName4' => serialize($this->keyedArrayData),
'variableObjectName1' => serialize($this->object),
);
$this->variables = new Variables($testData);
}
/**
* @covers Variables::variable_get
*/
public function testVariableGet()
{
variable_set('test', 'variable');
$this->assertTrue(true);
$this->assertSame('variableValue1', variable_get('variableName1'));
$this->assertSame('variableValue2', variable_get('variableName2'));
$this->assertSame(range(0, 20), variable_get('variableName3'));
$this->assertNotSame(range(1, 20), variable_get('variableName3'));
$this->assertSame($this->keyedArrayData, variable_get('variableName4'));
$this->assertArrayHasKey('subVariableName1', variable_get('variableName4'));
$this->assertArraySubset($this->keyedArrayData, variable_get('variableName4'));
$this->assertEquals($this->object, variable_get('variableObjectName1'));
$this->assertNotSame('variableValue1', []);
$this->assertSame(0, variable_get('thisVariableDoesntExist', 0));
$this->assertNull(variable_get('thisVariableDoesntExist'));
}
/**
* @covers Variables::variable_set
*/
public function testVariableSet()
{
$this->assertNull(variable_set('variableName5', 'variableValue5'));
variable_set('variableName6', 'variableValue6');
$this->assertSame('variableValue6', variable_get('variableName6'));
variable_set('variableName7', $this->object);
$this->assertEquals($this->object, variable_get('variableName7'));
}
/**
* @covers Variables::variable_del
*/
public function testVariableDel()
{
$this->assertNull(variable_del('variableName7'));
$this->assertNull(variable_get('variableName7'));
variable_del('variableName6');
$this->assertSame(0, variable_get('variableName6', 0));
$this->assertSame('variableValue1', variable_get('variableName1'));
variable_del('variableName1');
variable_set('variableName1', 'variableDelTest1');
$this->assertSame('variableDelTest1', variable_get('variableName1'));
}
/**
* @covers Variables::createTempDirectory
* @covers Variables::touchConfigFile
* @covers Variables::parseConfig
* @covers Variables::writeConfig
*/
public function testTempFile()
{
$this->assertFileExists(sys_get_temp_dir().Variables::VARIABLE_NAMESPACE.Variables::VARIABLE_TEMP_FILE_NAME);
$this->assertFileEquals(sys_get_temp_dir().'/sikofitt/mocks/drupal/config.tmp', sys_get_temp_dir().Variables::VARIABLE_NAMESPACE.Variables::VARIABLE_TEMP_FILE_NAME);
$this->assertJsonStringEqualsJsonFile(
sys_get_temp_dir().Variables::VARIABLE_NAMESPACE.Variables::VARIABLE_TEMP_FILE_NAME,
json_encode($this->variables->getArrayCopy())
);
}
}