Skip to content

Commit

Permalink
add diagnostic checks to require .env file and to warn if file does n…
Browse files Browse the repository at this point in the history
…ot exist. default diagnostic requires the file, but the application can override to warn. applications can provide custom factories to override the .env filename and path.
  • Loading branch information
abacaphiliac committed Oct 29, 2016
1 parent b34c9fc commit 973bab2
Show file tree
Hide file tree
Showing 12 changed files with 314 additions and 4 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@
"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"
Expand Down
15 changes: 15 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

return array(
'service_manager' => array(
'factories' => array(
'RequireEnvFile' => '\Abacaphiliac\ZendPhpDotEnv\Diagnostics\RequireEnvFactory',
'SuggestEnvFile' => '\Abacaphiliac\ZendPhpDotEnv\Diagnostics\SuggestEnvFactory',
),
),
'diagnostics' => array(
'Abacaphiliac\ZendPhpDotEnv' => array(
'Readable `.env` file' => 'RequireEnvFile',
),
),
);
22 changes: 22 additions & 0 deletions src/ZendPhpDotEnv/Diagnostics/RequireEnvFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Abacaphiliac\ZendPhpDotEnv\Diagnostics;

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

class RequireEnvFactory implements FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @return RequireReadableFile
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
return new RequireReadableFile(array(
getcwd() . '/.env',
));
}
}
21 changes: 21 additions & 0 deletions src/ZendPhpDotEnv/Diagnostics/RequireReadableFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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)
{
return new Success();
}
}
22 changes: 22 additions & 0 deletions src/ZendPhpDotEnv/Diagnostics/SuggestEnvFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Abacaphiliac\ZendPhpDotEnv\Diagnostics;

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

class SuggestEnvFactory implements FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @return mixed
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
return new SuggestReadableFile(array(
getcwd() . '/.env',
));
}
}
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;
}
}
20 changes: 16 additions & 4 deletions src/ZendPhpDotEnv/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Abacaphiliac\ZendPhpDotEnv;

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

class Module
class Module implements InitProviderInterface, ConfigProviderInterface
{
/** @var string */
private $constant = 'APPLICATION_PATH';
Expand Down Expand Up @@ -38,9 +40,19 @@ public function __construct($constant = null, $variable = null, $file = null)
}

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

/**
* @param ModuleManagerInterface $moduleManager
*/
public function init(ModuleManagerInterface $moduleManager)
{
$events = $moduleManager->getEventManager();

Expand Down
32 changes: 32 additions & 0 deletions tests/ZendPhpDotEnv/Diagnostics/RequireEnvFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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 RequireEnvFactory */
private $sut;

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

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

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

public function testCreateService()
{
$service = $this->sut->createService($this->serviceManager);
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);
}
}
32 changes: 32 additions & 0 deletions tests/ZendPhpDotEnv/Diagnostics/SuggestEnvFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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 SuggestEnvFactory */
private $sut;

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

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

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

public function testCreateService()
{
$service = $this->sut->createService($this->serviceManager);
self::assertInstanceOf('\Abacaphiliac\ZendPhpDotEnv\Diagnostics\SuggestReadableFile', $service);
}
}
59 changes: 59 additions & 0 deletions tests/ZendPhpDotEnv/Diagnostics/SuggestReadableFileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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);
}
}
7 changes: 7 additions & 0 deletions tests/ZendPhpDotEnv/ModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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
*/
Expand Down

0 comments on commit 973bab2

Please sign in to comment.