Skip to content

Commit

Permalink
cleaned code
Browse files Browse the repository at this point in the history
  • Loading branch information
danrot committed Jun 11, 2015
1 parent 8106bb2 commit 1e8ba0b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 121 deletions.
10 changes: 2 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"doctrine/dbal": ">=2.4.5,<3.0.x-dev",
"phpcr/phpcr": "~2.1.2",
"phpcr/phpcr-utils": "~1.2,>=1.2.6",
"jackalope/jackalope": "dev-version-history as 1.3.0"
"jackalope/jackalope": "dev-versioning as 1.3.0"
},
"provide": {
"jackalope/jackalope-transport": "1.1.0"
Expand All @@ -39,11 +39,5 @@
"branch-alias": {
"dev-master": "1.2-dev"
}
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/danrot/jackalope.git"
}
]
}
}
190 changes: 77 additions & 113 deletions tests/Jackalope/Test/Fixture/DBUnitFixtureXML.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ class DBUnitFixtureXML extends XMLDocument
private $rootNodes;

/**
* @param string $file - file path
* @param int $options - libxml option constants: http://www.php.net/manual/en/libxml.constants.php
* @param string $file - file path
* @param int $options - libxml option constants: http://www.php.net/manual/en/libxml.constants.php
*/
public function __construct($file, $options = null)
{
parent::__construct($file, $options);

$this->tables = array();
$this->ids = array();
$this->references = array();
$this->tables = array();
$this->ids = array();
$this->references = array();
$this->expectedNodes = array();
}

Expand All @@ -65,16 +65,13 @@ public function addDataset()
$this->appendChild($this->createElement('dataset'));

// purge binary in case no binary properties are in fixture
$this->ensureTableExists(
'phpcr_binarydata',
array(
'node_id',
'property_name',
'workspace_name',
'idx',
'data',
)
);
$this->ensureTableExists('phpcr_binarydata', array(
'node_id',
'property_name',
'workspace_name',
'idx',
'data',
));

return $this;
}
Expand Down Expand Up @@ -102,7 +99,7 @@ public function addNamespaces(array $namespaces)
*
* If the root node is not called jcr:root, autogenerate a root node.
*
* @param string $workspaceName
* @param string $workspaceName
* @param \DOMNodeList $nodes
*
* @return DBUnitFixtureXML
Expand All @@ -117,9 +114,7 @@ public function addNodes($workspaceName, \DOMNodeList $nodes)
$this->rootNodes[$workspaceName] = true;
}

$srcDom = new \Jackalope\Test\Fixture\JCRSystemXML(
__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'system.xml'
);
$srcDom = new \Jackalope\Test\Fixture\JCRSystemXML(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'system.xml');
foreach ($srcDom->load()->getNodes() as $node) {
$this->addNode($workspaceName, $node);
}
Expand All @@ -136,43 +131,40 @@ public function addRootNode($workspaceName = 'default')
$uuid = UUIDHelper::generateUUID();
$this->ids[$uuid] = self::$idCounter++;

return $this->addRow(
'phpcr_nodes',
array(
'id' => $this->ids[$uuid],
'path' => '/',
'parent' => '',
'local_name' => '',
'namespace' => '',
'workspace_name' => $workspaceName,
'identifier' => $uuid,
'type' => 'nt:unstructured',
'props' => '<?xml version="1.0" encoding="UTF-8"?>'
. '<sv:node xmlns:crx="http://www.day.com/crx/1.0"'
. 'xmlns:lx="http://flux-cms.org/2.0"'
. 'xmlns:test="http://liip.to/jackalope"'
. 'xmlns:mix="http://www.jcp.org/jcr/mix/1.0"'
. 'xmlns:sling="http://sling.apache.org/jcr/sling/1.0"'
. 'xmlns:nt="http://www.jcp.org/jcr/nt/1.0"'
. 'xmlns:fn_old="http://www.w3.org/2004/10/xpath-functions"'
. 'xmlns:fn="http://www.w3.org/2005/xpath-functions"'
. 'xmlns:vlt="http://www.day.com/jcr/vault/1.0"'
. 'xmlns:xs="http://www.w3.org/2001/XMLSchema"'
. 'xmlns:new_prefix="http://a_new_namespace"'
. 'xmlns:jcr="http://www.jcp.org/jcr/1.0"'
. 'xmlns:sv="http://www.jcp.org/jcr/sv/1.0"'
. 'xmlns:rep="internal" />',
'depth' => 0,
'sort_order' => 0,
)
);
return $this->addRow('phpcr_nodes', array(
'id' => $this->ids[$uuid],
'path' => '/',
'parent' => '',
'local_name' => '',
'namespace' => '',
'workspace_name' => $workspaceName,
'identifier' => $uuid,
'type' => 'nt:unstructured',
'props' => '<?xml version="1.0" encoding="UTF-8"?>'
. '<sv:node xmlns:crx="http://www.day.com/crx/1.0"'
. 'xmlns:lx="http://flux-cms.org/2.0"'
. 'xmlns:test="http://liip.to/jackalope"'
. 'xmlns:mix="http://www.jcp.org/jcr/mix/1.0"'
. 'xmlns:sling="http://sling.apache.org/jcr/sling/1.0"'
. 'xmlns:nt="http://www.jcp.org/jcr/nt/1.0"'
. 'xmlns:fn_old="http://www.w3.org/2004/10/xpath-functions"'
. 'xmlns:fn="http://www.w3.org/2005/xpath-functions"'
. 'xmlns:vlt="http://www.day.com/jcr/vault/1.0"'
. 'xmlns:xs="http://www.w3.org/2001/XMLSchema"'
. 'xmlns:new_prefix="http://a_new_namespace"'
. 'xmlns:jcr="http://www.jcp.org/jcr/1.0"'
. 'xmlns:sv="http://www.jcp.org/jcr/sv/1.0"'
. 'xmlns:rep="internal" />',
'depth' => 0,
'sort_order' => 0,
));
}

public function addNode($workspaceName, \DOMElement $node)
{
$properties = $this->getAttributes($node);
$uuid = isset($properties['jcr:uuid']['value'][0])
? (string)$properties['jcr:uuid']['value'][0] : UUIDHelper::generateUUID();
? (string) $properties['jcr:uuid']['value'][0] : UUIDHelper::generateUUID();
$this->ids[$uuid] = $id = isset($this->expectedNodes[$uuid])
? $this->expectedNodes[$uuid] : self::$idCounter++;

Expand All @@ -192,25 +184,23 @@ public function addNode($workspaceName, \DOMElement $node)
throw new \InvalidArgumentException('"' . $propertyData['type'] . '" is not a valid JCR type.');
}

$phpcrNode->appendChild(
$this->createPropertyNode($workspaceName, $propertyName, $propertyData, $id, $dom, $phpcrNode)
);
$phpcrNode->appendChild($this->createPropertyNode($workspaceName, $propertyName, $propertyData, $id, $dom, $phpcrNode));
}

list ($parentPath, $childPath) = $this->getPath($node);

$namespace = '';
$name = $node->getAttributeNS($this->namespaces['sv'], 'name');
$name = $node->getAttributeNS($this->namespaces['sv'], 'name');
if (count($parts = explode(':', $name, 2)) == 2) {
list($namespace, $name) = $parts;
}

if ($namespace == 'jcr' && $name == 'root') {
$id = 1;
$childPath = '/';
$id = 1;
$childPath = '/';
$parentPath = '';
$name = '';
$namespace = '';
$name = '';
$namespace = '';
}

if (isset($properties['jcr:mixinTypes'])
Expand All @@ -219,30 +209,27 @@ public function addNode($workspaceName, \DOMElement $node)
$this->addVersioningProperties($dom, $phpcrNode, $workspaceName, $id, $uuid);
}

$this->addRow(
'phpcr_nodes',
array(
'id' => $id,
'path' => $childPath,
'parent' => $parentPath,
'local_name' => $name,
'namespace' => $namespace,
'workspace_name' => $workspaceName,
'identifier' => $uuid,
'type' => $properties['jcr:primaryType']['value'][0],
'props' => $dom->saveXML(),
'depth' => PathHelper::getPathDepth($childPath),
'sort_order' => $id - 2,
)
);
$this->addRow('phpcr_nodes', array(
'id' => $id,
'path' => $childPath,
'parent' => $parentPath,
'local_name' => $name,
'namespace' => $namespace,
'workspace_name' => $workspaceName,
'identifier' => $uuid,
'type' => $properties['jcr:primaryType']['value'][0],
'props' => $dom->saveXML(),
'depth' => PathHelper::getPathDepth($childPath),
'sort_order' => $id - 2,
));

return $this;
}

public function addReferences()
{
foreach ($this->references as $type => $references) {
$table = 'phpcr_nodes_' . $type . 's';
$table = 'phpcr_nodes_'.$type.'s';

// make sure we have the references even if there is not a single entry in it to have it truncated
$this->ensureTableExists($table, array('source_id', 'source_property_name', 'target_id'));
Expand Down Expand Up @@ -290,10 +277,7 @@ public function getChildAttribute(\DOMElement $node)
$isMultiValue = false;
if ($name == 'jcr:mixinTypes'
|| count($values) > 1
|| ($node->hasAttributeNS($this->namespaces['sv'], 'multiple') && $node->getAttributeNS(
$this->namespaces['sv'],
'multiple'
) == 'true')
|| ($node->hasAttributeNS($this->namespaces['sv'], 'multiple') && $node->getAttributeNS($this->namespaces['sv'], 'multiple') == 'true')
) {
$isMultiValue = true;
}
Expand All @@ -310,31 +294,14 @@ public function createPropertyNode($workspaceName, $propertyName, $propertyData,

$binaryDataIdx = 0;
foreach ($propertyData['value'] as $value) {
$propertyNode->appendChild(
$this->createValueNodeByType(
$workspaceName,
$propertyData['type'],
$value,
$id,
$propertyName,
$binaryDataIdx++,
$dom
)
);
$propertyNode->appendChild($this->createValueNodeByType($workspaceName, $propertyData['type'], $value, $id, $propertyName, $binaryDataIdx++, $dom));
}

return $propertyNode;
}

public function createValueNodeByType(
$workspaceName,
$type,
$value,
$id,
$propertyName,
$binaryDataIdx,
\DOMDocument $dom
) {
public function createValueNodeByType($workspaceName, $type, $value, $id, $propertyName, $binaryDataIdx, \DOMDocument $dom)
{
$length = is_scalar($value) ? strlen($value) : null;
switch ($type) {
case 'binary':
Expand Down Expand Up @@ -362,9 +329,9 @@ public function createValueNodeByType(
}
// do not repeat references
$this->references[$type][$value][$id . $propertyName . $targetId] = array(
'source_id' => $id,
'source_id' => $id,
'source_property_name' => $propertyName,
'target_id' => $targetId,
'target_id' => $targetId,
);
break;
}
Expand Down Expand Up @@ -412,10 +379,10 @@ public function getPath(\DOMElement $node)
}

/**
* @param int $id
* @param int $id
* @param string $propertyName
* @param string $workspaceName
* @param int $idx
* @param int $idx
* @param string $data
*
* @return int - length of base64 decoded string
Expand All @@ -424,16 +391,13 @@ public function addBinaryNode($id, $propertyName, $workspaceName, $idx, $data)
{
$data = base64_decode($data);

$this->addRow(
'phpcr_binarydata',
array(
'node_id' => $id,
'property_name' => $propertyName,
'workspace_name' => $workspaceName,
'idx' => $idx,
'data' => $data,
)
);
$this->addRow('phpcr_binarydata', array(
'node_id' => $id,
'property_name' => $propertyName,
'workspace_name' => $workspaceName,
'idx' => $idx,
'data' => $data,
));

return strlen($data);
}
Expand Down

0 comments on commit 1e8ba0b

Please sign in to comment.