-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add diagnostic checks to require .env file and to warn if file does n…
…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
1 parent
b34c9fc
commit 973bab2
Showing
12 changed files
with
314 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
), | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
59
tests/ZendPhpDotEnv/Diagnostics/RequireReadableFileTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
59
tests/ZendPhpDotEnv/Diagnostics/SuggestReadableFileTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters