Skip to content

Commit

Permalink
Fixture generation
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed Sep 16, 2014
1 parent 9e139ce commit afa4512
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/Transport/Fs/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,8 @@ private function getNodeRecordPath($path)

private function nodeExists($path)
{
return $this->fs->exists($this->getNodeRecordPath($path));
$path = $this->getNodeRecordPath($path);
return $this->fs->exists($path);
}

private function createNode($path, $nodeData)
Expand Down
6 changes: 5 additions & 1 deletion src/Transport/Fs/Filesystem/Adapter/LocalAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ public function remove($path, $recursive = false)

public function exists($path)
{
return file_exists($this->getAbsPath($path));
$path = $this->getAbsPath($path);
$exists = file_exists($path);
error_log($path . ' ' . ($exists ? 'does' : 'does not') . ' exist');

return $exists;
}

public function ls($path)
Expand Down
99 changes: 99 additions & 0 deletions src/Transport/Fs/Test/FixtureGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Jackalope\Transport\Fs\Test;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;
use PHPCR\Util\UUIDHelper;

class FixtureGenerator
{
const NS_SV = 'http://www.jcp.org/jcr/sv/1.0';

protected $destDir;
protected $fs;

function generateFixtures($srcDir, $destDir)
{
$this->destDir = $destDir;
$this->fs = new Filesystem();
$this->fs->remove($destDir);
mkdir($destDir);
$this->makeRootNode();

foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($srcDir)) as $srcFile) {
if (!$srcFile->isFile() || $srcFile->getExtension() !== 'xml') {
continue;
}

$dom = new \DOMDocument(1.0);
$dom->load($srcFile->getPathname());
$dom->preserveWhitepace = true;
$dom->format = true;

$this->iterateNode($dom->firstChild);
}
}

function makeRootNode()
{
$uuid = UUIDHelper::generateUUID();
$node = array(
'jcr:uuid' => $uuid,
'jcr:primaryType' => 'nt:unstructured',
);

$yaml = Yaml::dump($node);
$path = $this->destDir .'/node.yml';
file_put_contents($path, $yaml);
}

function iterateNode(\DomNode $domNode)
{
if ($domNode->nodeName == 'sv:node') {
$this->persistSystemNode($domNode);
}

if (!$domNode->childNodes) {
return;
}

foreach ($domNode->childNodes as $child) {
$this->iterateNode($child);
}
}

function persistSystemNode(\DomNode $domNode)
{
$xpath = new \DOMXpath($domNode->ownerDocument);
foreach ($domNode->childNodes as $domProperty) {
if ($domProperty->nodeName != 'sv:property') {
continue;
}

$node[$domProperty->getAttributeNS(self::NS_SV, 'name')] = array(
'type' => $domProperty->getAttributeNs(self::NS_SV, 'type'),
'value' => trim($domProperty->nodeValue)
);
}

$ancestors = $xpath->query('ancestor::*', $domNode);
$path = array();

foreach ($ancestors as $ancestorNode) {
$path[] = $ancestorNode->getAttributeNs(self::NS_SV, 'name');
}

$path[] = $domNode->getAttributeNs(self::NS_SV, 'name');

$path = implode('/', $path);
$filePath = sprintf('%s/%s/node.yml', $this->destDir, $path);
$dirPath = dirname($filePath);
if (!file_exists($dirPath)) {
$this->fs->mkdir($dirPath);
}

$yaml = Yaml::dump($node);
file_put_contents($filePath, $yaml);
}
}
3 changes: 1 addition & 2 deletions tests/ImplementationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ protected function __construct()
parent::__construct('Jackalope\RepositoryFactoryFilesystem', $GLOBALS['phpcr.workspace']);

$this->unsupportedChapters = array(
'Reading',
'Query',
'Export',
'NodeTypeDiscovery',
Expand Down Expand Up @@ -113,7 +112,7 @@ public function getRepository()

public function getFixtureLoader()
{
return new \Jackalope\Test\Tester\FilesystemFixtureLoader(__DIR__ . '/data.dist', '/data');
return new \Jackalope\Test\Tester\FilesystemFixtureLoader();
}

}
11 changes: 5 additions & 6 deletions tests/Tester/FilesystemFixtureLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
namespace Jackalope\Test\Tester;

use PHPCR\Test\FixtureLoaderInterface;
use Jackalope\Transport\Fs\Test\FixtureGenerator;

/**
* Filesystem fixture loader
*/
class FilesystemFixtureLoader implements FixtureLoaderInterface
{
protected $distPath;
protected $testPath;

public function __construct($distPath, $testPath)
public function __construct()
{
$this->distPath = $distPath;
$this->testPath = $testPath;
$fixtureGenerator = new FixtureGenerator();
$srcDir = __DIR__ . '/../../vendor/phpcr/phpcr-api-tests/fixtures';
$fixtureGenerator->generateFixtures($srcDir, __DIR__ . '/../data/tests');
}

public function import($fixture, $workspaceKey = 'workspace')
Expand Down
15 changes: 15 additions & 0 deletions tests/bin/generate_fixtures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/env php
<?php

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;

require_once(__DIR__ . '/../../vendor/autoload.php');

}

$gen = new FixtureGenerator();
$gen->generateFixtures(
__DIR__ . '/../../vendor/phpcr/phpcr-api-tests/fixtures',
__DIR__ . '/../data/tests'
);
4 changes: 2 additions & 2 deletions tests/data/tests/node.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
':jcr:primaryType': Name
'jcr:primaryType': 'rep:root'
'jcr:uuid': 1bd4db57-65ce-4cbc-a02f-8fd2f3dcc21c
'jcr:primaryType': 'nt:unstructured'
Empty file modified tests/data/testsAdditional/node.yml
100644 → 100755
Empty file.

0 comments on commit afa4512

Please sign in to comment.