Skip to content

add diagnostic checks to require or suggest .env file #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@
</target>

<target name="phpcs">
<exec command="vendor/bin/phpcs --standard=PSR2 --extensions=php --severity=1 --colors -p src/ tests/"
<exec command="vendor/bin/phpcs --standard=PSR2 --extensions=php --severity=1 --colors -p config/ src/ tests/"
passthru="true"
output="/dev/stdout"
error="/dev/stdout"
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -21,16 +21,22 @@
"vlucas/phpdotenv": "^2.2",
"zendframework/zend-eventmanager": "^2",
"zendframework/zend-modulemanager": "^2",
"zendframework/zend-servicemanager": "^2"
"zendframework/zend-servicemanager": "^2",
"zendframework/zend-stdlib": "^2"
},
"require-dev": {
"phpunit/phpunit": "^5|^4",
"phing/phing": "^2",
"zendframework/zend-mvc": "^2",
"zendframework/zend-loader": "^2",
"jakub-onderka/php-parallel-lint": "^0.9",
"zendframework/zenddiagnostics": "^1.0",
"mikey179/vfsStream": "^1.6",
"squizlabs/php_codesniffer": "^2.2"
},
"suggest": {
"zendframework/zenddiagnostics": "Universal set of diagnostic tests for PHP applications."
},
"autoload": {
"psr-4": {
"Abacaphiliac\\ZendPhpDotEnv\\": "src/ZendPhpDotEnv"
26 changes: 26 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

return array(
'service_manager' => array(
'factories' => array(
'Abacaphiliac\ZendPhpDotEnv\Diagnostics\RequireEnv' =>
'Abacaphiliac\ZendPhpDotEnv\Diagnostics\RequireEnvFactory',
'Abacaphiliac\ZendPhpDotEnv\Diagnostics\SuggestEnv' =>
'Abacaphiliac\ZendPhpDotEnv\Diagnostics\SuggestEnvFactory',
),
'aliases' => array(
'Abacaphiliac\ZendPhpDotEnv\Diagnostics\CheckEnvFiles' =>
'Abacaphiliac\ZendPhpDotEnv\Diagnostics\SuggestEnv',
),
),
'diagnostics' => array(
'abacaphiliac/zend-phpdotenv' => array(
'Readable `.env` file(s)' => 'Abacaphiliac\ZendPhpDotEnv\Diagnostics\CheckEnvFiles',
),
),
'abacaphiliac/zend-phpdotenv' => array(
'files' => array(
'default' => getcwd() . '/.env',
),
),
);
26 changes: 26 additions & 0 deletions src/ZendPhpDotEnv/Diagnostics/RequireEnvFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Abacaphiliac\ZendPhpDotEnv\Diagnostics;

use Abacaphiliac\ZendPhpDotEnv\Options\ModuleOptionsFactory;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class RequireEnvFactory implements FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @return RequireReadableFile
* @throws \InvalidArgumentException
* @throws \Zend\Stdlib\Exception\InvalidArgumentException
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$files = ModuleOptionsFactory::getFiles($serviceLocator);

return new RequireReadableFile($files);
}
}
23 changes: 23 additions & 0 deletions src/ZendPhpDotEnv/Diagnostics/RequireReadableFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Abacaphiliac\ZendPhpDotEnv\Diagnostics;

use ZendDiagnostics\Check\AbstractFileCheck;
use ZendDiagnostics\Result\ResultInterface;
use ZendDiagnostics\Result\Success;

class RequireReadableFile extends AbstractFileCheck
{
/**
* Validates a specific file type and returns a check result
*
* @param string $file
* @return ResultInterface
*/
protected function validateFile($file)
{
// This is an optional secondary validation. The parent validates existence of the file.

return new Success();
}
}
25 changes: 25 additions & 0 deletions src/ZendPhpDotEnv/Diagnostics/SuggestEnvFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Abacaphiliac\ZendPhpDotEnv\Diagnostics;

use Abacaphiliac\ZendPhpDotEnv\Options\ModuleOptionsFactory;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class SuggestEnvFactory implements FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @return SuggestReadableFile
* @throws \Zend\Stdlib\Exception\InvalidArgumentException
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$files = ModuleOptionsFactory::getFiles($serviceLocator);

return new SuggestReadableFile($files);
}
}
24 changes: 24 additions & 0 deletions src/ZendPhpDotEnv/Diagnostics/SuggestReadableFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Abacaphiliac\ZendPhpDotEnv\Diagnostics;

use ZendDiagnostics\Result\FailureInterface;
use ZendDiagnostics\Result\ResultInterface;
use ZendDiagnostics\Result\Warning;

class SuggestReadableFile extends RequireReadableFile
{
/**
* @return ResultInterface
*/
public function check()
{
$result = parent::check();

if ($result instanceof FailureInterface) {
return new Warning($result->getMessage(), $result->getData());
}

return $result;
}
}
13 changes: 12 additions & 1 deletion src/ZendPhpDotEnv/Module.php
Original file line number Diff line number Diff line change
@@ -2,11 +2,12 @@

namespace Abacaphiliac\ZendPhpDotEnv;

use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\InitProviderInterface;
use Zend\ModuleManager\ModuleEvent;
use Zend\ModuleManager\ModuleManagerInterface;

class Module implements InitProviderInterface
class Module implements InitProviderInterface, ConfigProviderInterface
{
/** @var string */
private $constant = 'APPLICATION_PATH';
@@ -38,6 +39,16 @@ public function __construct($constant = null, $variable = null, $file = null)
}
}

/**
* Returns configuration to merge with application configuration
*
* @return array|\Traversable
*/
public function getConfig()
{
return require __DIR__ . '/../../config/module.config.php';
}

/**
* @param ModuleManagerInterface $moduleManager
*/
28 changes: 28 additions & 0 deletions src/ZendPhpDotEnv/Options/ModuleOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Abacaphiliac\ZendPhpDotEnv\Options;

use Zend\Stdlib\AbstractOptions;

class ModuleOptions extends AbstractOptions
{
/** @var string[] */
private $files = array();

/**
* @return string[]
*/
public function getFiles()
{
return $this->files;
}

/**
* @param string[] $files
* @return void
*/
public function setFiles(array $files)
{
$this->files = $files;
}
}
45 changes: 45 additions & 0 deletions src/ZendPhpDotEnv/Options/ModuleOptionsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Abacaphiliac\ZendPhpDotEnv\Options;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class ModuleOptionsFactory implements FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @return ModuleOptions
* @throws \Zend\Stdlib\Exception\InvalidArgumentException
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$options = new ModuleOptions();

$config = $serviceLocator->get('config');

if (array_key_exists('abacaphiliac/zend-phpdotenv', $config)) {
$options->setFromArray($config['abacaphiliac/zend-phpdotenv']);
}

return $options;
}

/**
* @param ServiceLocatorInterface $serviceLocator
* @return string[]
* @throws \Zend\Stdlib\Exception\InvalidArgumentException
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
*/
public static function getFiles(ServiceLocatorInterface $serviceLocator)
{
$factory = new self;

$options = $factory->createService($serviceLocator);

return $options->getFiles();
}
}
37 changes: 37 additions & 0 deletions tests/ZendPhpDotEnv/Diagnostics/RequireEnvFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace AbacaphiliacTest\ZendPhpDotEnv\Diagnostics;

use Abacaphiliac\ZendPhpDotEnv\Diagnostics\RequireEnvFactory;
use Zend\ServiceManager\ServiceManager;

/**
* @covers \Abacaphiliac\ZendPhpDotEnv\Diagnostics\RequireEnvFactory
*/
class RequireEnvFactoryTest extends \PHPUnit_Framework_TestCase
{
/** @var \ArrayObject */
private $config;

/** @var RequireEnvFactory */
private $sut;

/** @var ServiceManager */
private $serviceLocator;

protected function setUp()
{
parent::setUp();

$this->serviceLocator = new ServiceManager();
$this->serviceLocator->setService('config', $this->config = new \ArrayObject());

$this->sut = new RequireEnvFactory();
}

public function testCreateService()
{
$service = $this->sut->createService($this->serviceLocator);
self::assertInstanceOf('\Abacaphiliac\ZendPhpDotEnv\Diagnostics\RequireReadableFile', $service);
}
}
59 changes: 59 additions & 0 deletions tests/ZendPhpDotEnv/Diagnostics/RequireReadableFileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace AbacaphiliacTest\ZendPhpDotEnv\Diagnostics;

use Abacaphiliac\ZendPhpDotEnv\Diagnostics\RequireReadableFile;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamFile;

/**
* @covers \Abacaphiliac\ZendPhpDotEnv\Diagnostics\RequireReadableFile
*/
class RequireReadableFileTest extends \PHPUnit_Framework_TestCase
{
/** @var vfsStreamDirectory */
private $root;

/** @var vfsStreamFile */
private $file;

/** @var RequireReadableFile */
private $sut;

protected function setUp()
{
parent::setUp();

$this->root = vfsStream::setup('test');
$this->file = vfsStream::newFile('.env');
$this->root->addChild($this->file);

$this->sut = new RequireReadableFile(array(
$this->file->url(),
));
}

public function testReadableFileIsValid()
{
$this->file->chmod(777);
$result = $this->sut->check();
self::assertInstanceOf('\ZendDiagnostics\Result\SuccessInterface', $result);
}

public function testDirectoryIsInvalid()
{
$sut = new RequireReadableFile(array(
$this->root->url(),
));
$result = $sut->check();
self::assertInstanceOf('\ZendDiagnostics\Result\FailureInterface', $result);
}

public function testNonReadableFileIsInvalid()
{
$this->file->chmod(0);
$result = $this->sut->check();
self::assertInstanceOf('\ZendDiagnostics\Result\FailureInterface', $result);
}
}
37 changes: 37 additions & 0 deletions tests/ZendPhpDotEnv/Diagnostics/SuggestEnvFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace AbacaphiliacTest\ZendPhpDotEnv\Diagnostics;

use Abacaphiliac\ZendPhpDotEnv\Diagnostics\SuggestEnvFactory;
use Zend\ServiceManager\ServiceManager;

/**
* @covers \Abacaphiliac\ZendPhpDotEnv\Diagnostics\SuggestEnvFactory
*/
class SuggestEnvFactoryTest extends \PHPUnit_Framework_TestCase
{
/** @var \ArrayObject */
private $config;

/** @var SuggestEnvFactory */
private $sut;

/** @var ServiceManager */
private $serviceLocator;

protected function setUp()
{
parent::setUp();

$this->serviceLocator = new ServiceManager();
$this->serviceLocator->setService('config', $this->config = new \ArrayObject());

$this->sut = new SuggestEnvFactory();
}

public function testCreateService()
{
$service = $this->sut->createService($this->serviceLocator);
self::assertInstanceOf('\Abacaphiliac\ZendPhpDotEnv\Diagnostics\SuggestReadableFile', $service);
}
}
69 changes: 69 additions & 0 deletions tests/ZendPhpDotEnv/Diagnostics/SuggestReadableFileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace AbacaphiliacTest\ZendPhpDotEnv\Diagnostics;

use Abacaphiliac\ZendPhpDotEnv\Diagnostics\SuggestReadableFile;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamFile;

/**
* @covers \Abacaphiliac\ZendPhpDotEnv\Diagnostics\SuggestReadableFile
*/
class SuggestReadableFileTest extends \PHPUnit_Framework_TestCase
{
/** @var vfsStreamDirectory */
private $root;

/** @var vfsStreamFile */
private $file;

/** @var SuggestReadableFile */
private $sut;

protected function setUp()
{
parent::setUp();

$this->root = vfsStream::setup('test');
$this->file = vfsStream::newFile('.env');
$this->root->addChild($this->file);

$this->sut = new SuggestReadableFile(array(
$this->file->url(),
));
}

public function testReadableFileIsValid()
{
$this->file->chmod(777);
$result = $this->sut->check();
self::assertInstanceOf('\ZendDiagnostics\Result\SuccessInterface', $result);
}

public function testDirectoryIsInvalid()
{
$sut = new SuggestReadableFile(array(
$this->root->url(),
));
$result = $sut->check();
self::assertInstanceOf('\ZendDiagnostics\Result\WarningInterface', $result);
}

public function testNonReadableFileIsInvalid()
{
$this->file->chmod(0);
$result = $this->sut->check();
self::assertInstanceOf('\ZendDiagnostics\Result\WarningInterface', $result);
}

public function testNonExistentFileIsInvalid()
{
self::assertFileExists($this->file->url());
unlink($this->file->url());
self::assertFileNotExists($this->file->url());

$result = $this->sut->check();
self::assertInstanceOf('\ZendDiagnostics\Result\WarningInterface', $result);
}
}
7 changes: 7 additions & 0 deletions tests/ZendPhpDotEnv/ModuleTest.php
Original file line number Diff line number Diff line change
@@ -15,6 +15,13 @@
*/
class ModuleTest extends \PHPUnit_Framework_TestCase
{
public function testGetConfig()
{
$module = new Module();
$config = $module->getConfig();
self::assertArraySubset($config, unserialize(serialize($config)));
}

/**
* @expectedException \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidConstantPathException
*/
64 changes: 64 additions & 0 deletions tests/ZendPhpDotEnv/Options/ModuleOptionsFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace AbacaphiliacTest\ZendPhpDotEnv\Options;

use Abacaphiliac\ZendPhpDotEnv\Options\ModuleOptionsFactory;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\ServiceManager;

/**
* @covers \Abacaphiliac\ZendPhpDotEnv\Options\ModuleOptionsFactory
*/
class ModuleOptionsFactoryTest extends \PHPUnit_Framework_TestCase
{
/** @var \ArrayObject */
private $config;

/** @var ServiceLocatorInterface */
private $serviceLocator;

/** @var ModuleOptionsFactory */
private $sut;

protected function setUp()
{
parent::setUp();

$this->serviceLocator = new ServiceManager();
$this->serviceLocator->setService('config', $this->config = new \ArrayObject());

$this->sut = new ModuleOptionsFactory();
}

public function testCreateService()
{
$actual = $this->sut->createService($this->serviceLocator);

self::assertInstanceOf('Abacaphiliac\ZendPhpDotEnv\Options\ModuleOptions', $actual);
}

public function testCreateServiceWithFiles()
{
$this->config['abacaphiliac/zend-phpdotenv'] = array(
'files' => $expected = array(
'relative/path/to/file',
),
);

$actual = $this->sut->createService($this->serviceLocator);

self::assertInstanceOf('Abacaphiliac\ZendPhpDotEnv\Options\ModuleOptions', $actual);
self::assertArraySubset($expected, $actual->getFiles());
}

public function testGetFiles()
{
$this->config['abacaphiliac/zend-phpdotenv'] = array(
'files' => $expected = array(
'relative/path/to/file',
),
);

self::assertArraySubset($expected, ModuleOptionsFactory::getFiles($this->serviceLocator));
}
}
34 changes: 34 additions & 0 deletions tests/ZendPhpDotEnv/Options/ModuleOptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace AbacaphiliacTest\ZendPhpDotEnv\Options;

use Abacaphiliac\ZendPhpDotEnv\Options\ModuleOptions;

/**
* @covers \Abacaphiliac\ZendPhpDotEnv\Options\ModuleOptions
*/
class ModuleOptionsTest extends \PHPUnit_Framework_TestCase
{
/** @var ModuleOptions */
private $sut;

protected function setUp()
{
parent::setUp();

$this->sut = new ModuleOptions();
}

public function testFiles()
{
self::assertCount(0, $this->sut->getFiles());

$this->sut->setFromArray(array(
'files' => $expected = array(
'relative/path/to/file',
),
));

self::assertArraySubset($expected, $this->sut->getFiles());
}
}