Skip to content

Commit

Permalink
Internal cleanup and code coverage completion.
Browse files Browse the repository at this point in the history
  • Loading branch information
daniele-orlando committed Jan 23, 2016
1 parent 5213ea9 commit b697ca7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 50 deletions.
3 changes: 3 additions & 0 deletions documents/ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[travis-build]: https://travis-ci.org/servo-php/fluidxml
[travis-build-badge]: https://travis-ci.org/servo-php/fluidxml.svg?branch=master
[codeship-build]: https://codeship.com/projects/129206
[codeship-build-badge]: https://codeship.com/projects/8f977260-a359-0133-4946-1ac8bff03ae9/status?branch=master
[scrutinizer-coverage]: https://scrutinizer-ci.com/g/servo-php/fluidxml/?branch=master
[scrutinizer-coverage-badge]: https://scrutinizer-ci.com/g/servo-php/fluidxml/badges/coverage.png?b=master
[scrutinizer-quality]: https://scrutinizer-ci.com/g/servo-php/fluidxml/?branch=master
Expand All @@ -24,6 +26,7 @@
[thankyou]: https://upload.wikimedia.org/wikipedia/commons/thumb/f/f1/Heart_corazón.svg/2000px-Heart_corazón.svg.png

[![Travis Build][travis-build-badge]][travis-build]
[![Codeship Build][codeship-build-badge]][codeship-build]
[![Scrutinizer Coverage][scrutinizer-coverage-badge]][scrutinizer-coverage]
[![Scrutinizer Quality][scrutinizer-quality-badge]][scrutinizer-quality]
[![Code Climate][codeclimate-quality-badge]][codeclimate-quality]
Expand Down
58 changes: 19 additions & 39 deletions source/FluidXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,11 @@ class FluidXml implements FluidInterface
public static function load($document)
{
if (\is_string($document) && ! is_an_xml_string($document)) {
// Removes any empty new line at the beginning,
// otherwise the first character check fails.
$file = $document;
$document = \file_get_contents($file);

$file = $document;
$is_file = \is_file($file);
$is_readable = \is_readable($file);

if ($is_file && $is_readable) {
$document = \file_get_contents($file);
}

if (! $is_file || ! $is_readable || ! $document) {
// file_get_contents returns false if it can't read.
if (! $document) {
throw new \Exception("File '$file' not accessible.");
}
}
Expand Down Expand Up @@ -410,23 +403,19 @@ public function remove(...$xpath)

protected function context()
{
if ($this->document->dom->documentElement === null) {
// If the user has requested ['root' => null] at construction time
// the 'documentElement' property is null because we have not created
// a root node yet. Whether there is not a root node, the DOMDocument
// is promoted as root node.
if ($this->context === null) {
$this->context = new FluidContext($this->document, $this->handler, $this->document->dom);
}
$el = $this->document->dom->documentElement;

return $this->context;
if ($el === null) {
// Whether there is not a root node
// the DOMDocument is promoted as root node.
$el = $this->document->dom;
}

if ($this->contextEl !== $this->document->dom->documentElement) {
if ($this->context === null || $el !== $this->contextEl) {
// The user can prepend a root node to the current root node.
// In this case we have to update the context with the new first root node.
$this->context = new FluidContext($this->document, $this->handler, $this->document->dom->documentElement);
$this->contextEl = $this->document->dom->documentElement;
$this->context = new FluidContext($this->document, $this->handler, $el);
$this->contextEl = $el;
}

return $this->context;
Expand Down Expand Up @@ -489,7 +478,7 @@ public function mode()
return $this->config[self::MODE];
}

public function querify($xpath)
public function __invoke($xpath)
{
$id = $this->id();

Expand All @@ -504,9 +493,9 @@ public function querify($xpath)
$nodes = \explode('/', $xpath);

foreach ($nodes as $node) {
// An XPath query may have multiple slashes ('/')
// example: //target
if ($node) {
if (! empty($node)) {
// An XPath query can have multiple slashes.
// Example: //target
$new_xpath .= "{$id}{$node}";
}

Expand Down Expand Up @@ -692,7 +681,6 @@ public function insertElement(&$nodes, $element, &$optionals, $fn, $orig_context

foreach ($nodes as $n) {
foreach ($element as $k => $v) {
// I give up, it's a too complex job for only one method like me.
$cx = $this->handleInsertion($n, $k, $v, $fn, $optionals);
$new_nodes = \array_merge($new_nodes, $cx);
}
Expand All @@ -708,11 +696,7 @@ public function insertElement(&$nodes, $element, &$optionals, $fn, $orig_context
$new_context->setAttribute($attributes);
}

if ($switch_context) {
return $new_context;
}

return $orig_context;
return $switch_context ? $new_context : $orig_context;
}

protected function newContext(&$context)
Expand Down Expand Up @@ -1212,9 +1196,7 @@ public function query(...$xpath)
// the xpath, relative (../..) or absolute (//), returns identical
// matching results that must be collapsed in an unique result
// otherwise a subsequent operation is performed multiple times.
$results = $this->filterQueryResults($results);

return $this->newContext($results);
return $this->newContext($this->filterQueryResults($results));
}

public function times($times, callable $fn = null)
Expand Down Expand Up @@ -1437,9 +1419,7 @@ protected function filterQueryResults(&$results)
$found = false;

foreach ($set as $u) {
if ($r === $u) {
$found = true;
}
$found = ($r === $u) || $found;
}

if (! $found) {
Expand Down
50 changes: 42 additions & 8 deletions specs/FluidXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@
});

describe('FluidXml', function() {
it('should throw invoking not existing staic method', function() {
try {
FluidXml::lload();
} catch (\Exception $e) {
$actual = $e;
}

assert_is_a($actual, \Exception::class);
});

describe(':load', function() {
$doc = "<root>\n"
. " <parent>content</parent>\n"
Expand Down Expand Up @@ -239,7 +249,7 @@
try {
$xml = FluidXml::load('.impossible.xml');
} catch (\Exception $e) {
$actual = $e;
$actual = $e;
}

assert_is_a($actual, \Exception::class);
Expand All @@ -249,7 +259,7 @@
try {
$xml = FluidXml::load(0);
} catch (\Exception $e) {
$actual = $e;
$actual = $e;
}

assert_is_a($actual, \Exception::class);
Expand Down Expand Up @@ -327,6 +337,17 @@
$expected = $stylesheet;
assert_equal_xml($xml, $expected);
});

it('should throw invoking not existing method', function() {
$xml = new FluidXml();
try {
$xml->qquery();
} catch (\Exception $e) {
$actual = $e;
}

assert_is_a($actual, \Exception::class);
});
});

describe('.dom', function() {
Expand Down Expand Up @@ -1173,7 +1194,7 @@ function addchild($parent, $i)
try {
$xml->appendChild(0);
} catch (\Exception $e) {
$actual = $e;
$actual = $e;
}

assert_is_a($actual, \Exception::class);
Expand Down Expand Up @@ -1928,6 +1949,19 @@ function addchild($parent, $i)
$expected = $cx->asArray();
\assert($actual === $expected, __($actual, $expected));
});

it('should throw for not supported document', function() {
$doc = new FluidDocument();
$handler = new FluidInsertionHandler($doc);

try {
new FluidContext($doc, $handler, 'node');
} catch (\Exception $e) {
$actual = $e;
}

assert_is_a($actual, \Exception::class);
});
});

describe('[]', function() {
Expand Down Expand Up @@ -1961,7 +1995,7 @@ function addchild($parent, $i)
try {
$cx[] = "value";
} catch (\Exception $e) {
$actual = $e;
$actual = $e;
}
assert_is_a($actual, \Exception::class);

Expand Down Expand Up @@ -2148,21 +2182,21 @@ function addchild($parent, $i)
it('should format an XPath query to use the namespace id', function() {
$ns = new FluidNamespace('x', 'x.com');

$actual = $ns->querify('current/child');
$actual = $ns('current/child');
$expected = 'x:current/x:child';
\assert($actual === $expected, __($actual, $expected));

$actual = $ns->querify('//current/child');
$actual = $ns('//current/child');
$expected = '//x:current/x:child';
\assert($actual === $expected, __($actual, $expected));

$ns = new FluidNamespace('x', 'x.com', FluidNamespace::MODE_IMPLICIT);

$actual = $ns->querify('current/child');
$actual = $ns('current/child');
$expected = 'x:current/x:child';
\assert($actual === $expected, __($actual, $expected));

$actual = $ns->querify('//current/child');
$actual = $ns('//current/child');
$expected = '//x:current/x:child';
\assert($actual === $expected, __($actual, $expected));
});
Expand Down
4 changes: 2 additions & 2 deletions support/tools/coverage
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ if ! chkcmd 'peridot'; then
exit 1
fi

coverage_index="$PWD/sandbox/code-coverage-report/index.html"

cd sandbox

reporter=html-code-coverage
Expand Down Expand Up @@ -39,8 +41,6 @@ else
exit 1
fi

coverage_index="code-coverage-report/index.html"

if test -f "$coverage_index" && chkcmd 'open'; then
open "$coverage_index"
fi
2 changes: 1 addition & 1 deletion support/tools/init
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ fi
mkdir -pv "sandbox"
mkdir -pv "sandbox/composer"

composer install -d "."
composer install -d "." --no-interaction
rm -f "./composer.lock"

0 comments on commit b697ca7

Please sign in to comment.