Skip to content

Commit 973bab2

Browse files
committed
add diagnostic checks to require .env file and to warn if file does not 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.
1 parent b34c9fc commit 973bab2

12 files changed

+314
-4
lines changed

Diff for: composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@
2929
"zendframework/zend-mvc": "^2",
3030
"zendframework/zend-loader": "^2",
3131
"jakub-onderka/php-parallel-lint": "^0.9",
32+
"zendframework/zenddiagnostics": "^1.0",
33+
"mikey179/vfsStream": "^1.6",
3234
"squizlabs/php_codesniffer": "^2.2"
3335
},
36+
"suggest": {
37+
"zendframework/zenddiagnostics": "Universal set of diagnostic tests for PHP applications."
38+
},
3439
"autoload": {
3540
"psr-4": {
3641
"Abacaphiliac\\ZendPhpDotEnv\\": "src/ZendPhpDotEnv"

Diff for: config/module.config.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
return array(
4+
'service_manager' => array(
5+
'factories' => array(
6+
'RequireEnvFile' => '\Abacaphiliac\ZendPhpDotEnv\Diagnostics\RequireEnvFactory',
7+
'SuggestEnvFile' => '\Abacaphiliac\ZendPhpDotEnv\Diagnostics\SuggestEnvFactory',
8+
),
9+
),
10+
'diagnostics' => array(
11+
'Abacaphiliac\ZendPhpDotEnv' => array(
12+
'Readable `.env` file' => 'RequireEnvFile',
13+
),
14+
),
15+
);

Diff for: src/ZendPhpDotEnv/Diagnostics/RequireEnvFactory.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Abacaphiliac\ZendPhpDotEnv\Diagnostics;
4+
5+
use Zend\ServiceManager\FactoryInterface;
6+
use Zend\ServiceManager\ServiceLocatorInterface;
7+
8+
class RequireEnvFactory implements FactoryInterface
9+
{
10+
/**
11+
* Create service
12+
*
13+
* @param ServiceLocatorInterface $serviceLocator
14+
* @return RequireReadableFile
15+
*/
16+
public function createService(ServiceLocatorInterface $serviceLocator)
17+
{
18+
return new RequireReadableFile(array(
19+
getcwd() . '/.env',
20+
));
21+
}
22+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Abacaphiliac\ZendPhpDotEnv\Diagnostics;
4+
5+
use ZendDiagnostics\Check\AbstractFileCheck;
6+
use ZendDiagnostics\Result\ResultInterface;
7+
use ZendDiagnostics\Result\Success;
8+
9+
class RequireReadableFile extends AbstractFileCheck
10+
{
11+
/**
12+
* Validates a specific file type and returns a check result
13+
*
14+
* @param string $file
15+
* @return ResultInterface
16+
*/
17+
protected function validateFile($file)
18+
{
19+
return new Success();
20+
}
21+
}

Diff for: src/ZendPhpDotEnv/Diagnostics/SuggestEnvFactory.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Abacaphiliac\ZendPhpDotEnv\Diagnostics;
4+
5+
use Zend\ServiceManager\FactoryInterface;
6+
use Zend\ServiceManager\ServiceLocatorInterface;
7+
8+
class SuggestEnvFactory implements FactoryInterface
9+
{
10+
/**
11+
* Create service
12+
*
13+
* @param ServiceLocatorInterface $serviceLocator
14+
* @return mixed
15+
*/
16+
public function createService(ServiceLocatorInterface $serviceLocator)
17+
{
18+
return new SuggestReadableFile(array(
19+
getcwd() . '/.env',
20+
));
21+
}
22+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Abacaphiliac\ZendPhpDotEnv\Diagnostics;
4+
5+
use ZendDiagnostics\Result\FailureInterface;
6+
use ZendDiagnostics\Result\ResultInterface;
7+
use ZendDiagnostics\Result\Warning;
8+
9+
class SuggestReadableFile extends RequireReadableFile
10+
{
11+
/**
12+
* @return ResultInterface
13+
*/
14+
public function check()
15+
{
16+
$result = parent::check();
17+
18+
if ($result instanceof FailureInterface) {
19+
return new Warning($result->getMessage(), $result->getData());
20+
}
21+
22+
return $result;
23+
}
24+
}

Diff for: src/ZendPhpDotEnv/Module.php

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Abacaphiliac\ZendPhpDotEnv;
44

5+
use Zend\ModuleManager\Feature\ConfigProviderInterface;
6+
use Zend\ModuleManager\Feature\InitProviderInterface;
57
use Zend\ModuleManager\ModuleEvent;
6-
use Zend\ModuleManager\ModuleManager;
8+
use Zend\ModuleManager\ModuleManagerInterface;
79

8-
class Module
10+
class Module implements InitProviderInterface, ConfigProviderInterface
911
{
1012
/** @var string */
1113
private $constant = 'APPLICATION_PATH';
@@ -38,9 +40,19 @@ public function __construct($constant = null, $variable = null, $file = null)
3840
}
3941

4042
/**
41-
* @param ModuleManager $moduleManager
43+
* Returns configuration to merge with application configuration
44+
*
45+
* @return array|\Traversable
4246
*/
43-
public function init(ModuleManager $moduleManager)
47+
public function getConfig()
48+
{
49+
return require __DIR__ . '/../../config/module.config.php';
50+
}
51+
52+
/**
53+
* @param ModuleManagerInterface $moduleManager
54+
*/
55+
public function init(ModuleManagerInterface $moduleManager)
4456
{
4557
$events = $moduleManager->getEventManager();
4658

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace AbacaphiliacTest\ZendPhpDotEnv\Diagnostics;
4+
5+
use Abacaphiliac\ZendPhpDotEnv\Diagnostics\RequireEnvFactory;
6+
use Zend\ServiceManager\ServiceManager;
7+
8+
/**
9+
* @covers \Abacaphiliac\ZendPhpDotEnv\Diagnostics\RequireEnvFactory
10+
*/
11+
class RequireEnvFactoryTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/** @var RequireEnvFactory */
14+
private $sut;
15+
16+
/** @var ServiceManager */
17+
private $serviceManager;
18+
19+
protected function setUp()
20+
{
21+
parent::setUp();
22+
23+
$this->serviceManager = new ServiceManager();
24+
$this->sut = new RequireEnvFactory();
25+
}
26+
27+
public function testCreateService()
28+
{
29+
$service = $this->sut->createService($this->serviceManager);
30+
self::assertInstanceOf('\Abacaphiliac\ZendPhpDotEnv\Diagnostics\RequireReadableFile', $service);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace AbacaphiliacTest\ZendPhpDotEnv\Diagnostics;
4+
5+
use Abacaphiliac\ZendPhpDotEnv\Diagnostics\RequireReadableFile;
6+
use org\bovigo\vfs\vfsStream;
7+
use org\bovigo\vfs\vfsStreamDirectory;
8+
use org\bovigo\vfs\vfsStreamFile;
9+
10+
/**
11+
* @covers \Abacaphiliac\ZendPhpDotEnv\Diagnostics\RequireReadableFile
12+
*/
13+
class RequireReadableFileTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/** @var vfsStreamDirectory */
16+
private $root;
17+
18+
/** @var vfsStreamFile */
19+
private $file;
20+
21+
/** @var RequireReadableFile */
22+
private $sut;
23+
24+
protected function setUp()
25+
{
26+
parent::setUp();
27+
28+
$this->root = vfsStream::setup('test');
29+
$this->file = vfsStream::newFile('.env');
30+
$this->root->addChild($this->file);
31+
32+
$this->sut = new RequireReadableFile(array(
33+
$this->file->url(),
34+
));
35+
}
36+
37+
public function testReadableFileIsValid()
38+
{
39+
$this->file->chmod(777);
40+
$result = $this->sut->check();
41+
self::assertInstanceOf('\ZendDiagnostics\Result\SuccessInterface', $result);
42+
}
43+
44+
public function testDirectoryIsInvalid()
45+
{
46+
$sut = new RequireReadableFile(array(
47+
$this->root->url(),
48+
));
49+
$result = $sut->check();
50+
self::assertInstanceOf('\ZendDiagnostics\Result\FailureInterface', $result);
51+
}
52+
53+
public function testNonReadableFileIsInvalid()
54+
{
55+
$this->file->chmod(0);
56+
$result = $this->sut->check();
57+
self::assertInstanceOf('\ZendDiagnostics\Result\FailureInterface', $result);
58+
}
59+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace AbacaphiliacTest\ZendPhpDotEnv\Diagnostics;
4+
5+
use Abacaphiliac\ZendPhpDotEnv\Diagnostics\SuggestEnvFactory;
6+
use Zend\ServiceManager\ServiceManager;
7+
8+
/**
9+
* @covers \Abacaphiliac\ZendPhpDotEnv\Diagnostics\SuggestEnvFactory
10+
*/
11+
class SuggestEnvFactoryTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/** @var SuggestEnvFactory */
14+
private $sut;
15+
16+
/** @var ServiceManager */
17+
private $serviceManager;
18+
19+
protected function setUp()
20+
{
21+
parent::setUp();
22+
23+
$this->serviceManager = new ServiceManager();
24+
$this->sut = new SuggestEnvFactory();
25+
}
26+
27+
public function testCreateService()
28+
{
29+
$service = $this->sut->createService($this->serviceManager);
30+
self::assertInstanceOf('\Abacaphiliac\ZendPhpDotEnv\Diagnostics\SuggestReadableFile', $service);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace AbacaphiliacTest\ZendPhpDotEnv\Diagnostics;
4+
5+
use Abacaphiliac\ZendPhpDotEnv\Diagnostics\SuggestReadableFile;
6+
use org\bovigo\vfs\vfsStream;
7+
use org\bovigo\vfs\vfsStreamDirectory;
8+
use org\bovigo\vfs\vfsStreamFile;
9+
10+
/**
11+
* @covers \Abacaphiliac\ZendPhpDotEnv\Diagnostics\SuggestReadableFile
12+
*/
13+
class SuggestReadableFileTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/** @var vfsStreamDirectory */
16+
private $root;
17+
18+
/** @var vfsStreamFile */
19+
private $file;
20+
21+
/** @var SuggestReadableFile */
22+
private $sut;
23+
24+
protected function setUp()
25+
{
26+
parent::setUp();
27+
28+
$this->root = vfsStream::setup('test');
29+
$this->file = vfsStream::newFile('.env');
30+
$this->root->addChild($this->file);
31+
32+
$this->sut = new SuggestReadableFile(array(
33+
$this->file->url(),
34+
));
35+
}
36+
37+
public function testReadableFileIsValid()
38+
{
39+
$this->file->chmod(777);
40+
$result = $this->sut->check();
41+
self::assertInstanceOf('\ZendDiagnostics\Result\SuccessInterface', $result);
42+
}
43+
44+
public function testDirectoryIsInvalid()
45+
{
46+
$sut = new SuggestReadableFile(array(
47+
$this->root->url(),
48+
));
49+
$result = $sut->check();
50+
self::assertInstanceOf('\ZendDiagnostics\Result\WarningInterface', $result);
51+
}
52+
53+
public function testNonReadableFileIsInvalid()
54+
{
55+
$this->file->chmod(0);
56+
$result = $this->sut->check();
57+
self::assertInstanceOf('\ZendDiagnostics\Result\WarningInterface', $result);
58+
}
59+
}

Diff for: tests/ZendPhpDotEnv/ModuleTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212

1313
class ModuleTest extends \PHPUnit_Framework_TestCase
1414
{
15+
public function testGetConfig()
16+
{
17+
$module = new Module();
18+
$config = $module->getConfig();
19+
self::assertArraySubset($config, unserialize(serialize($config)));
20+
}
21+
1522
/**
1623
* @expectedException \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidConstantPathException
1724
*/

0 commit comments

Comments
 (0)