diff --git a/composer.json b/composer.json index cedacf2..599e12e 100644 --- a/composer.json +++ b/composer.json @@ -3,9 +3,10 @@ "description": "Mock for drupal variable functions variable_(set,get,del)", "type": "library", "require": { - "werx/config": "1.3", - "symfony/yaml": "^3.1", - "hassankhan/config": "^0.10.0" + "php": ">=5.4", + "hassankhan/config": "~0.10", + "symfony/filesystem": "^3.1" + }, "require-dev": { "phpunit/phpunit": "^5.5" @@ -14,13 +15,19 @@ "authors": [ { "name": "sikofitt", - "email": "sikofitt@gmail.com" + "email": "sikofitt@gmail.com", + "role": "developer" } ], "autoload": { "psr-4": { - "Sikofitt\\Mocks\\Drupal\\":"src/Sikofitt/Mocks/Drupal/" + "Sikofitt\\Mocks\\Drupal\\": "src/Sikofitt/Mocks/Drupal/" }, "files": ["bootstrap.php"] + }, + "autoload-dev": { + "psr-4": { + "Sikofitt\\Tests\\Mocks\\Drupal\\": "tests/" + } } } diff --git a/composer.lock b/composer.lock index f54c071..6692373 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "4d4ebd354c6c8f007ca9ff6e10f15d6d", - "content-hash": "23ae934d44ab3909ec35d411c68680d3", + "hash": "4ca6c1efcbc049f58ae1ed1a3a592c11", + "content-hash": "c7d470d7f395e09723985f30dd2ecc5e", "packages": [ { "name": "hassankhan/config", @@ -65,17 +65,17 @@ "time": "2016-02-11 16:21:17" }, { - "name": "symfony/yaml", + "name": "symfony/filesystem", "version": "v3.1.3", "source": { "type": "git", - "url": "https://github.com/symfony/yaml.git", - "reference": "1819adf2066880c7967df7180f4f662b6f0567ac" + "url": "https://github.com/symfony/filesystem.git", + "reference": "bb29adceb552d202b6416ede373529338136e84f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/1819adf2066880c7967df7180f4f662b6f0567ac", - "reference": "1819adf2066880c7967df7180f4f662b6f0567ac", + "url": "https://api.github.com/repos/symfony/filesystem/zipball/bb29adceb552d202b6416ede373529338136e84f", + "reference": "bb29adceb552d202b6416ede373529338136e84f", "shasum": "" }, "require": { @@ -89,7 +89,7 @@ }, "autoload": { "psr-4": { - "Symfony\\Component\\Yaml\\": "" + "Symfony\\Component\\Filesystem\\": "" }, "exclude-from-classmap": [ "/Tests/" @@ -109,50 +109,9 @@ "homepage": "https://symfony.com/contributors" } ], - "description": "Symfony Yaml Component", + "description": "Symfony Filesystem Component", "homepage": "https://symfony.com", - "time": "2016-07-17 14:02:08" - }, - { - "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" + "time": "2016-07-20 05:44:26" } ], "packages-dev": [ @@ -1354,6 +1313,55 @@ "homepage": "https://github.com/sebastianbergmann/version", "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", "version": "1.0.2", @@ -1409,6 +1417,8 @@ "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, - "platform": [], + "platform": { + "php": ">=5.4" + }, "platform-dev": [] } diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..6199165 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,21 @@ + + + + tests + + + + + src + + + diff --git a/src/Sikofitt/Mocks/Drupal/Variables.php b/src/Sikofitt/Mocks/Drupal/Variables.php index 4d5f963..6347c7f 100644 --- a/src/Sikofitt/Mocks/Drupal/Variables.php +++ b/src/Sikofitt/Mocks/Drupal/Variables.php @@ -16,10 +16,10 @@ namespace Sikofitt\Mocks\Drupal; use Noodlehaus\AbstractConfig; 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 werx\Config\Container; -use werx\Config\Providers\JsonProvider; /** @@ -27,92 +27,151 @@ use werx\Config\Providers\JsonProvider; * * @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() { - - } - - /** - * @param $data - * - * @return $this - */ - public function setData($data) { - $this->data = $data; - return $this; - } - - /** - * @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)) + public function tempDirectoryExists() { - 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 ( !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; + 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()); + } + + /** + * @param $data + * + * @return $this + */ + public function setData($data) + { + $this->data = $data; + return $this; + } + + private function is_serialized($data) + { + // 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; + } + return false; } - return false; - } } \ No newline at end of file diff --git a/tests/VariableSetTest.php b/tests/VariableSetTest.php new file mode 100644 index 0000000..e69de29 diff --git a/tests/VariablesTest.php b/tests/VariablesTest.php new file mode 100644 index 0000000..a692986 --- /dev/null +++ b/tests/VariablesTest.php @@ -0,0 +1,24 @@ +assertTrue(true); + } +}