Initial commit

This commit is contained in:
R. Eric Wheeler 2016-08-05 20:14:59 -07:00
parent 5eaec882da
commit d21a45c47b
6 changed files with 265 additions and 144 deletions

View File

@ -3,9 +3,10 @@
"description": "Mock for drupal variable functions variable_(set,get,del)", "description": "Mock for drupal variable functions variable_(set,get,del)",
"type": "library", "type": "library",
"require": { "require": {
"werx/config": "1.3", "php": ">=5.4",
"symfony/yaml": "^3.1", "hassankhan/config": "~0.10",
"hassankhan/config": "^0.10.0" "symfony/filesystem": "^3.1"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^5.5" "phpunit/phpunit": "^5.5"
@ -14,13 +15,19 @@
"authors": [ "authors": [
{ {
"name": "sikofitt", "name": "sikofitt",
"email": "sikofitt@gmail.com" "email": "sikofitt@gmail.com",
"role": "developer"
} }
], ],
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"Sikofitt\\Mocks\\Drupal\\":"src/Sikofitt/Mocks/Drupal/" "Sikofitt\\Mocks\\Drupal\\": "src/Sikofitt/Mocks/Drupal/"
}, },
"files": ["bootstrap.php"] "files": ["bootstrap.php"]
},
"autoload-dev": {
"psr-4": {
"Sikofitt\\Tests\\Mocks\\Drupal\\": "tests/"
}
} }
} }

114
composer.lock generated
View File

@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"hash": "4d4ebd354c6c8f007ca9ff6e10f15d6d", "hash": "4ca6c1efcbc049f58ae1ed1a3a592c11",
"content-hash": "23ae934d44ab3909ec35d411c68680d3", "content-hash": "c7d470d7f395e09723985f30dd2ecc5e",
"packages": [ "packages": [
{ {
"name": "hassankhan/config", "name": "hassankhan/config",
@ -65,17 +65,17 @@
"time": "2016-02-11 16:21:17" "time": "2016-02-11 16:21:17"
}, },
{ {
"name": "symfony/yaml", "name": "symfony/filesystem",
"version": "v3.1.3", "version": "v3.1.3",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/yaml.git", "url": "https://github.com/symfony/filesystem.git",
"reference": "1819adf2066880c7967df7180f4f662b6f0567ac" "reference": "bb29adceb552d202b6416ede373529338136e84f"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/yaml/zipball/1819adf2066880c7967df7180f4f662b6f0567ac", "url": "https://api.github.com/repos/symfony/filesystem/zipball/bb29adceb552d202b6416ede373529338136e84f",
"reference": "1819adf2066880c7967df7180f4f662b6f0567ac", "reference": "bb29adceb552d202b6416ede373529338136e84f",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -89,7 +89,7 @@
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"Symfony\\Component\\Yaml\\": "" "Symfony\\Component\\Filesystem\\": ""
}, },
"exclude-from-classmap": [ "exclude-from-classmap": [
"/Tests/" "/Tests/"
@ -109,50 +109,9 @@
"homepage": "https://symfony.com/contributors" "homepage": "https://symfony.com/contributors"
} }
], ],
"description": "Symfony Yaml Component", "description": "Symfony Filesystem Component",
"homepage": "https://symfony.com", "homepage": "https://symfony.com",
"time": "2016-07-17 14:02:08" "time": "2016-07-20 05:44:26"
},
{
"name": "werx/config",
"version": "1.3",
"source": {
"type": "git",
"url": "https://github.com/werx/config.git",
"reference": "70a8ea7d7b2b4f8358b1f03058b5f12b764dda6e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/werx/config/zipball/70a8ea7d7b2b4f8358b1f03058b5f12b764dda6e",
"reference": "70a8ea7d7b2b4f8358b1f03058b5f12b764dda6e",
"shasum": ""
},
"require": {
"php": ">= 5.4"
},
"require-dev": {
"phpunit/phpunit": "4.4.*",
"squizlabs/php_codesniffer": "1.*"
},
"type": "library",
"autoload": {
"psr-4": {
"werx\\Config\\": "src/",
"werx\\Config\\Providers\\": "src/Providers"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Josh Moody",
"email": "jgmoody@gmail.com"
}
],
"description": "Use environment-specific configuration files in your app.",
"time": "2015-12-13 16:25:11"
} }
], ],
"packages-dev": [ "packages-dev": [
@ -1354,6 +1313,55 @@
"homepage": "https://github.com/sebastianbergmann/version", "homepage": "https://github.com/sebastianbergmann/version",
"time": "2016-02-04 12:56:52" "time": "2016-02-04 12:56:52"
}, },
{
"name": "symfony/yaml",
"version": "v3.1.3",
"source": {
"type": "git",
"url": "https://github.com/symfony/yaml.git",
"reference": "1819adf2066880c7967df7180f4f662b6f0567ac"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/yaml/zipball/1819adf2066880c7967df7180f4f662b6f0567ac",
"reference": "1819adf2066880c7967df7180f4f662b6f0567ac",
"shasum": ""
},
"require": {
"php": ">=5.5.9"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.1-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Yaml\\": ""
},
"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 Yaml Component",
"homepage": "https://symfony.com",
"time": "2016-07-17 14:02:08"
},
{ {
"name": "webmozart/assert", "name": "webmozart/assert",
"version": "1.0.2", "version": "1.0.2",
@ -1409,6 +1417,8 @@
"stability-flags": [], "stability-flags": [],
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,
"platform": [], "platform": {
"php": ">=5.4"
},
"platform-dev": [] "platform-dev": []
} }

21
phpunit.xml.dist Normal file
View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/5.4/phpunit.xsd"
bootstrap="vendor/autoload.php"
backupGlobals="false"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true"
colors="true">
<testsuite>
<directory suffix="Test.php">tests</directory>
</testsuite>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>

View File

@ -16,10 +16,10 @@ namespace Sikofitt\Mocks\Drupal;
use Noodlehaus\AbstractConfig; use Noodlehaus\AbstractConfig;
use Noodlehaus\Config; use Noodlehaus\Config;
use Noodlehaus\ConfigInterface; use Noodlehaus\Exception\EmptyDirectoryException;
use SebastianBergmann\CodeCoverage\Report\Html\File;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
use werx\Config\Container;
use werx\Config\Providers\JsonProvider;
/** /**
@ -27,10 +27,114 @@ use werx\Config\Providers\JsonProvider;
* *
* @package Sikofitt\Mocks\Drupal * @package Sikofitt\Mocks\Drupal
*/ */
class Variables extends AbstractConfig { class Variables extends AbstractConfig
{
const VARIABLE_TEMP_DIR = '/sikofitt/mocks/drupal';
const VARIABLE_NAMESPACE = '/sikofitt/mocks/drupal';
public function __construct() { public function tempDirectoryExists()
{
return file_exists(sys_get_temp_dir() . self::VARIABLE_TEMP_DIR);
}
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()
{
if (!$this->tempDirectoryExists()) {
$this->createTempDirectory();
}
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]);
}
$this->writeConfig();
}
private function resetConfig()
{
$config = new Config(CONFIG_PATH);
$this->setData($config->all());
} }
/** /**
@ -38,78 +142,33 @@ class Variables extends AbstractConfig {
* *
* @return $this * @return $this
*/ */
public function setData($data) { public function setData($data)
{
$this->data = $data; $this->data = $data;
return $this; return $this;
} }
/** private function is_serialized($data)
* @return array|null
*/
public function getData() {
return $this->data;
}
private function writeConfig($values)
{ {
file_put_contents(CONFIG_PATH . '/config.json', $values);
}
private function getConfig()
{
$config = new Config(CONFIG_PATH . '/config.json');
$this->setData($config->all());
}
public function variable_set($variableName, $variableValue)
{
$this->getConfig();
$this->set($variableName, serialize($variableValue));
$this->writeConfig(json_encode($this->all(), JSON_PRETTY_PRINT));
}
public function variable_get($variableName, $default = null)
{
$this->getConfig();
$variableValue = $this->get($variableName, $default);
return unserialize($variableValue);
}
public function variable_del($variableName)
{
$this->getConfig();
if($this->offsetExists($variableName))
{
unset($this->data[$variableName]);
}
$this->writeConfig(json_encode($this->all(), JSON_PRETTY_PRINT));
}
private function is_serialized( $data ) {
// if it isn't a string, it isn't serialized // if it isn't a string, it isn't serialized
if ( !is_string( $data ) ) if (!is_string($data))
return false; return false;
$data = trim( $data ); $data = trim($data);
if ( 'N;' == $data ) if ('N;' == $data)
return true; return true;
if ( !preg_match( '/^([adObis]):/', $data, $badions ) ) if (!preg_match('/^([adObis]):/', $data, $badions))
return false; return false;
switch ( $badions[1] ) { switch ($badions[1]) {
case 'a' : case 'a' :
case 'O' : case 'O' :
case 's' : case 's' :
if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) ) if (preg_match("/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data))
return true; return true;
break; break;
case 'b' : case 'b' :
case 'i' : case 'i' :
case 'd' : case 'd' :
if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) ) if (preg_match("/^{$badions[1]}:[0-9.E-]+;\$/", $data))
return true; return true;
break; break;
} }

View File

24
tests/VariablesTest.php Normal file
View File

@ -0,0 +1,24 @@
<?php
/**
* Created by PhpStorm.
* User: Eric
* Date: 08/05/2016
* Time: 5:47 PM
*/
namespace Sikofitt\Tests\Mocks\Drupal;
class VariablesTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
}
public function testVariableGet()
{
variable_set('test', 'variable');
$this->assertTrue(true);
}
}