Skip to content

Commit

Permalink
Merge pull request #45 from kdambekalns/bugfix/test-failures
Browse files Browse the repository at this point in the history
BUGFIX: Set savePath to avoid "must be of the type string, null given"
  • Loading branch information
kdambekalns committed Jul 7, 2017
2 parents 82b9e98 + 0302464 commit 64f8a4f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
27 changes: 23 additions & 4 deletions Classes/Persistence/YamlPersistenceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/

use Neos\Flow\Annotations as Flow;
use Neos\Form\Exception\PersistenceManagerException;
use Symfony\Component\Yaml\Yaml;

/**
* persistence identifier is some resource:// uri probably
Expand Down Expand Up @@ -43,15 +45,15 @@ public function injectSettings(array $settings)
*
* @param string $persistenceIdentifier
* @return array
* @throws \Neos\Form\Exception\PersistenceManagerException
* @throws PersistenceManagerException
*/
public function load($persistenceIdentifier)
{
if (!$this->exists($persistenceIdentifier)) {
throw new \Neos\Form\Exception\PersistenceManagerException(sprintf('The form identified by "%s" could not be loaded in "%s".', $persistenceIdentifier, $this->getFormPathAndFilename($persistenceIdentifier)), 1329307034);
throw new PersistenceManagerException(sprintf('The form identified by "%s" could not be loaded in "%s".', $persistenceIdentifier, $this->getFormPathAndFilename($persistenceIdentifier)), 1329307034);
}
$formPathAndFilename = $this->getFormPathAndFilename($persistenceIdentifier);
return \Symfony\Component\Yaml\Yaml::parse(file_get_contents($formPathAndFilename));
return Yaml::parse(file_get_contents($formPathAndFilename));
}

/**
Expand All @@ -63,7 +65,7 @@ public function load($persistenceIdentifier)
public function save($persistenceIdentifier, array $formDefinition)
{
$formPathAndFilename = $this->getFormPathAndFilename($persistenceIdentifier);
file_put_contents($formPathAndFilename, \Symfony\Component\Yaml\Yaml::dump($formDefinition, 99));
file_put_contents($formPathAndFilename, Yaml::dump($formDefinition, 99));
}

/**
Expand All @@ -88,6 +90,8 @@ public function exists($persistenceIdentifier)
*/
public function listForms()
{
$this->assertSavePathIsValid();

$forms = array();
$directoryIterator = new \DirectoryIterator($this->savePath);

Expand Down Expand Up @@ -119,7 +123,22 @@ public function listForms()
*/
protected function getFormPathAndFilename($persistenceIdentifier)
{
$this->assertSavePathIsValid();

$formFileName = sprintf('%s.yaml', $persistenceIdentifier);
return \Neos\Utility\Files::concatenatePaths(array($this->savePath, $formFileName));
}

/**
* Check if the save path is set and points to a directory.
*
* @return void
* @throws PersistenceManagerException
*/
protected function assertSavePathIsValid()
{
if ($this->savePath === null || !is_dir($this->savePath)) {
throw new PersistenceManagerException(sprintf('The savePath "%s" is not usable.', $this->savePath), 1499347363);
}
}
}
37 changes: 33 additions & 4 deletions Tests/Unit/Persistence/YamlPersistenceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* source code.
*/

use Neos\Form\Persistence\YamlPersistenceManager;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamWrapper;

Expand All @@ -20,14 +21,14 @@
class YamlPersistenceManagerTest extends \Neos\Flow\Tests\UnitTestCase
{
/**
* @var \Neos\Form\Persistence\YamlPersistenceManager
* @var YamlPersistenceManager
*/
protected $yamlPersistenceManager;

public function setUp()
{
vfsStream::setup('someSavePath');
$this->yamlPersistenceManager = new \Neos\Form\Persistence\YamlPersistenceManager();
$this->yamlPersistenceManager = new YamlPersistenceManager();
$this->yamlPersistenceManager->injectSettings(array(
'yamlPersistenceManager' =>
array('savePath' => vfsStream::url('someSavePath')
Expand All @@ -42,7 +43,7 @@ public function setUp()
public function injectSettingsCreatesSaveDirectoryIfItDoesntExist()
{
$this->assertFalse(vfsStreamWrapper::getRoot()->hasChild('foo/bar'));
$yamlPersistenceManager = new \Neos\Form\Persistence\YamlPersistenceManager();
$yamlPersistenceManager = new YamlPersistenceManager();
$settings = array(
'yamlPersistenceManager' =>
array('savePath' => vfsStream::url('someSavePath/foo/bar')
Expand All @@ -52,13 +53,30 @@ public function injectSettingsCreatesSaveDirectoryIfItDoesntExist()
$this->assertTrue(vfsStreamWrapper::getRoot()->hasChild('foo/bar'));
}


/**
* @test
* @expectedException \Neos\Form\Exception\PersistenceManagerException
*/
public function loadThrowsExceptionIfSavePathIsNotSet()
{
$yamlPersistenceManager = new YamlPersistenceManager();
$yamlPersistenceManager->load('dummy');
}

/**
* @test
* @expectedException \Neos\Form\Exception\PersistenceManagerException
*/
public function loadThrowsExceptionIfSpecifiedFormDoesNotExist()
{
$yamlPersistenceManager = new \Neos\Form\Persistence\YamlPersistenceManager();
$yamlPersistenceManager = new YamlPersistenceManager();
$settings = array(
'yamlPersistenceManager' =>
array('savePath' => vfsStream::url('someSavePath/foo/bar')
)
);
$yamlPersistenceManager->injectSettings($settings);
$yamlPersistenceManager->load('someNonExistingPersistenceIdentifier');
}

Expand Down Expand Up @@ -124,6 +142,17 @@ public function existsReturnsTrueIfTheSpecifiedFormExists()
$this->assertTrue($this->yamlPersistenceManager->exists('mockFormPersistenceIdentifier'));
}

/**
* @test
* @expectedException \Neos\Form\Exception\PersistenceManagerException
*/
public function listFormsThrowsExceptionIfSavePathIsNotSet()
{
$yamlPersistenceManager = new YamlPersistenceManager();
$yamlPersistenceManager->listForms();
}


/**
* @test
*/
Expand Down

0 comments on commit 64f8a4f

Please sign in to comment.