<?php

require_once(dirname(__FILE__).'/../src/xmlStroller.php');

use XmlStroller\XmlStroller as XmlStroller;

class XmlStrollerTester extends PHPUnit_Framework_TestCase
{
	public function testLoad()
	{
		$parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.xml")));
		$success = true;
		try {
			$parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/booksFail.xml")));
			$success = false;
		}
		catch (Exception $e) {}
		$this->assertTrue($success);
	}

	public function testSelectSimpleChild()
	{
		$parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.xml")));
		$this->assertCount(1, $parser->catalog);
		$this->assertCount(13, $parser->{"catalog book"});
		$this->assertCount(13, $parser->{"catalog    book"});
		$this->assertCount(13, $parser->{"book"});
		$this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog book"});
		$this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog   book"});
		$this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"book"});
		$this->assertCount(0, $parser->{"fail"});
		$this->assertCount(0, $parser->{"catalog fail"});
		$this->assertCount(0, $parser->{"book fail"});
	}

	public function testSelectDirectChild()
	{
		$parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.xml")));
		$this->assertCount(1, $parser->{">catalog"});
		$this->assertCount(1, $parser->{"> catalog"});
		$this->assertCount(1, $parser->{" > catalog"});
		$this->assertCount(13, $parser->{"catalog>book"});
		$this->assertCount(13, $parser->{"catalog >book"});
		$this->assertCount(13, $parser->{"catalog>  book"});
		$this->assertCount(13, $parser->{"catalog  >book"});
		$this->assertCount(13, $parser->{"catalog> book"});
		$this->assertCount(13, $parser->{"catalog > book"});
		$this->assertCount(13, $parser->{"catalog   >   book"});
		$this->assertCount(13, $parser->{" > catalog > book"});
		$this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{">catalog>book"});
		$this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog>book"});
		$this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog >book"});
		$this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog> book"});
		$this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog > book"});
		$this->assertContainsOnlyInstancesOf("\XmlStroller\XmlStroller", $parser->{"catalog   >   book"});
		$this->assertCount(0, $parser->{"catalog > fail"});
		$this->assertCount(0, $parser->{">fail"});
	}

	public function testSelectAttribute()
	{
		$parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.xml")));
		$book = $parser->{"catalog book[id=bk101]"};
		$this->assertCount(1, $book);
		$this->assertEquals("bk101", $book[0]->attr("id"));
		$book = $parser->{"catalog [id=bk101]book"};
		$this->assertCount(1, $book);
		$this->assertEquals("bk101", $book[0]->attr("id"));
		$book = $parser->{"catalog [id=bk101]"};
		$this->assertCount(1, $book);
		$this->assertEquals("bk101", $book[0]->attr("id"));
		$this->assertCount(0, $book = $parser->{"catalog[id=oupas]"});
		$this->assertCount(12, $parser->{"catalog [id]"});
		$this->assertCount(12, $parser->{"catalog [id]book"});
		$this->assertCount(12, $parser->{"catalog book[id]"});
		$this->assertCount(12, $parser->{"catalog book[id] *[currency]"});
		$this->assertCount(12, $parser->{"catalog book[id] [currency]"});
		$this->assertCount(12, $parser->{"catalog book[id] price[currency]"});
		$this->assertCount(13, $parser->{"catalog book price[currency=USD]"});
		$this->assertCount(1, $parser->{"catalog book price[currency=EURO]"});
		$books = $parser->{"price[currency=USD]"};
		$this->assertEquals(49.90, $books[count($books) -1]->valueDouble());
		$this->assertEquals(39.95, $parser->{"catalog book price[currency=EURO]"}[0]->valueDouble());
		$book = $parser->{"book price[currency=EURO]"}[0];
		$book = $book->node()->parent();
		try {
			$parser->{"catalog book[id=]"};
			$this->fail("Expected Exception");
		} catch (\Exception $e) {}
	}

	public function testGetAttributes()
	{
		$parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.xml")));
		$this->assertCount(13, $parser->{"catalog >book"});
		$firstBook = $parser->{"catalog >book"}[0];
		$this->assertInternalType('string', $firstBook->attr("id"));
		$this->assertEquals("bk101", $firstBook->attr("id"));
		$this->assertInternalType('array', $firstBook->attr());
		$this->assertCount(1, $firstBook->attr());
		$this->assertNull($firstBook->attr("fail"));
		$this->assertNull($firstBook->attr("fail", null));
		$parser->debug = true;
		$this->assertEquals("test", $firstBook->attr("fail", "test"));
		$parser->debug = false;
	}

	public function testFnc()
	{
	}

	public function testAttributeFnc()
	{
		$parser = new XmlStroller(utf8_encode(file_get_contents(__DIR__."/books.xml")));
		/*
		$this->assertCount(1, $parser->{"catalog book[id=bk101]:first-child"});
		$this->assertCount(1, $parser->{"catalog book:first-child[id=bk101]"});
		$this->assertCount(1, $parser->{"catalog :first-child[id=bk101]"});
		$this->assertCount(1, $parser->{"catalog [id=bk101]:first-child"});
		*/
	}
}

