Exemplo n.º 1
0
<?php

$invalid_xml = file_get_contents(dirname(__FILE__) . '/fixtures/xml_with_0_value.xml');
$craur = Craur::createFromXml($invalid_xml);
assert($craur->get('root.zerovalue') == '0');
Exemplo n.º 2
0
<?php

$invalid_xml = file_get_contents(dirname(__FILE__) . '/fixtures/invalid_xml.xml');
$craur = Craur::createFromXml($invalid_xml, 'iso-8859-1');
assert($craur->get('test.wrong') == 'xx');
Exemplo n.º 3
0
<?php

try {
    $node = Craur::createFromXml('');
    assert(false);
} catch (Exception $exception) {
    /*
     * Great, it broke! :)
     */
}
Exemplo n.º 4
0
<?php

try {
    $node = Craur::createFromXml('<book><author></author</book>');
    assert(false);
} catch (Exception $exception) {
    /*
     * Great, it broke! :)
     */
}
Exemplo n.º 5
0
<?php

/* Craur#createFromJson */
$node = Craur::createFromJson('{"book": {"authors": ["Hans", "Paul"]}}');
$authors = $node->get('book.authors[]');
assert(count($authors) == 2);
/* Craur#createFromXml */
$node = Craur::createFromXml('<book><author>Hans</author><author>Paul</author></book>');
$authors = $node->get('book.author[]');
assert(count($authors) == 2);
/* Craur#createFromHtml */
$node = Craur::createFromHtml('<html><head><title>Hans</title></head><body>Paul</body></html>');
assert($node->get('html.head.title') == 'Hans');
assert($node->get('html.body') == 'Paul');
/* Craur#createFromCsvFile */
// If the file loooks like this:
// Book Name;Book Year;Author Name
// My Book;2012;Hans
// My Book;2012;Paul
// My second Book;2010;Erwin
$shelf = Craur::createFromCsvFile('fixtures/books.csv', array('book[].name', 'book[].year', 'book[].author[].name'));
assert(count($shelf->get('book[]')) === 2);
foreach ($shelf->get('book[]') as $book) {
    assert(in_array($book->get('name'), array('My Book', 'My second Book')));
    foreach ($book->get('author[]') as $author) {
        assert(in_array($author->get('name'), array('Hans', 'Paul', 'Erwin')));
    }
}
/* Craur#createFromExcelFile */
// If the file loooks like this:
// Book Name;Book Year;Author Name
Exemplo n.º 6
0
<?php

$xml_string = file_get_contents(dirname(__FILE__) . '/fixtures/example_atom_feed.xml');
$document = Craur::createFromXml($xml_string);
/*
 * Can we grab all feeds? (it should be just one)
 */
$feeds = $document->get('feed[]');
assert(count($feeds) === 1);
$feed = $document->get('feed');
foreach ($feed->get('entry[]', array()) as $entry) {
    /*
     * At least 1 contributor
     */
    $contributors = $entry->get('contributor[]');
    assert(count($contributors) > 0);
    /*
     * first link.@rel must be alternate or self
     */
    assert(in_array($entry->get('link.@rel'), array('alternate', 'self')));
    $entry_data = $entry->getValues(array('id' => 'id', 'title' => 'title', 'link' => 'link.@href', 'author_name' => 'author.name'));
    assert(0 === count(array_diff(array_keys($entry_data), array('id', 'title', 'link', 'author_name'))));
}
<?php

$xml_string = file_get_contents(dirname(__FILE__) . '/fixtures/working_feed.xml');
$node = Craur::createFromXml($xml_string);
assert((string) $node->get('feed.title') === 'Example Feed');
/*
 * Test if a string works as default value
 */
assert($node->get('feed.non_existant_key', 'default') === 'default');
/*
 * Test if null and false work, too!
 */
assert($node->get('feed.non_existant_key', null) === null);
assert($node->get('feed.non_existant_key', false) === false);
try {
    $node->get('feed.non_existant_key');
    /*
     * This should not work!
     */
    assert(false);
} catch (Exception $exception) {
    /*
     * Nice, we got an exception!
     */
}
try {
    $node->get('feed.non_existant_key[]');
    /*
     * This should not work!
     */
    assert(false);
<?php

$craur = Craur::createFromXml(file_get_contents('fixtures/example_atom_feed.xml'));
$craur->saveToCsvFile('fixtures/temp_csv_file.csv', array('feed.entry[].title', 'feed.entry[].link[].@rel', 'feed.entry[].link[].nonexistantsubkey[].name', 'feed.entry[].link[].@href'));
$result_csv_content = trim(file_get_contents('fixtures/temp_csv_file.csv'));
unlink('fixtures/temp_csv_file.csv');
$lines = array_slice(explode(PHP_EOL, $result_csv_content), 1);
$expected_lines = array('"Atom draft-07 snapshot";alternate;;http://example.org/2005/04/02/atom', '"Atom draft-07 snapshot";enclosure;;http://example.org/audio/ph34r_my_podcast.mp3');
assert(count(array_diff($expected_lines, $lines)) === 0);
<?php

$xml_string = file_get_contents(dirname(__FILE__) . '/fixtures/working_feed.xml');
$node = Craur::createFromXml($xml_string);
/*
 * JSON -> Craur -> XML -> Craur -> JSON 
 */
$json = $node->toJsonString();
$test_node_from_json = Craur::createFromJson($json);
$xml = $test_node_from_json->toXmlString();
$test_node_from_xml_from_json = Craur::createFromXml($xml);
$json_from_xml_from_json = $test_node_from_xml_from_json->toJsonString();
assert($json === $json_from_xml_from_json);