<commit_before><?php
/*
 *  $Id: ResultSetTest.php 480 2012-05-01 18:13:54Z <EMAIL> $
 *
 * psx
 * A object oriented and modular based PHP framework for developing
 * dynamic web applications. For the current version and informations
 * visit <http://phpsx.org>
 *
 * Copyright (c) 2010-2012 <NAME> <<EMAIL>>
 *
 * This file is part of psx. psx is free software: you can
 * redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * psx is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with psx. If not, see <http://www.gnu.org/licenses/>.
 */

namespace PSX\Data;

use PSX\Exception;
use PSX\Data\Reader\Json;
use PSX\Http\Message;

/**
 * PSX_Data_ResultSetTest
 *
 * @author     <NAME> <<EMAIL>>
 * @license    http://www.gnu.org/licenses/gpl.html GPLv3
 * @link       http://phpsx.org
 * @category   tests
 * @version    $Revision: 480 $
 */
class RecordTest extends \PHPUnit_Framework_TestCase
{
	protected function setUp()
	{
	}

	protected function tearDown()
	{
	}

	public function testImport()
	{
		$body = <<<DATA
{
	"id": 1,
	"title": "foobar",
	"active": 1,
	"count": 12,
	"rating": 12.45,
	"person": {
		"name": "Foo"
	},
	"tags": [{
		"name": "bar"
	},{
		"name": "foo"
	},{
		"name": "test"
	}]
}
DATA;

		// read json
		$reader  = new Json();
		$message = new Message(array(), $body);

		$result = $reader->read($message);

		// create news object
		$news = new News();
		$news->import($result);

		$this->assertEquals(1, $news->getId());
		$this->assertInternalType('integer', $news->getId());
		$this->assertEquals('foobar', $news->getTitle());
		$this->assertInternalType('string', $news->getTitle());
		$this->assertEquals(true, $news->getActive());
		$this->assertInternalType('boolean', $news->getActive());
		$this->assertEquals(12, $news->getCount());
		$this->assertInternalType('integer', $news->getCount());
		$this->assertEquals(12.45, $news->getRating());
		$this->assertInternalType('float', $news->getRating());
		$this->assertInstanceOf('PSX\Data\Person', $news->getPerson());
		$this->assertEquals(true, is_array($news->getTags()));

		foreach($news->getTags() as $tag)
		{
			$this->assertInstanceOf('PSX\Data\Tag', $tag);
		}
	}
}

class News extends RecordAbstract
{
	protected $id;
	protected $title;
	protected $active;
	protected $count;
	protected $rating;
	protected $person;
	protected $tags;

	public function getName()
	{
		return 'news';
	}

	public function getFields()
	{
		return array(
			'id'     => $this->id,<commit_msg>improve test<commit_after><?php
/*
 *  $Id: ResultSetTest.php 480 2012-05-01 18:13:54Z k42b3.x<EMAIL> $
 *
 * psx
 * A object oriented and modular based PHP framework for developing
 * dynamic web applications. For the current version and informations
 * visit <http://phpsx.org>
 *
 * Copyright (c) 2010-2012 <NAME> <<EMAIL>>
 *
 * This file is part of psx. psx is free software: you can
 * redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * psx is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with psx. If not, see <http://www.gnu.org/licenses/>.
 */

namespace PSX\Data;

use PSX\Exception;
use PSX\Http\Message;

/**
 * PSX_Data_ResultSetTest
 *
 * @author     <NAME> <<EMAIL>>
 * @license    http://www.gnu.org/licenses/gpl.html GPLv3
 * @link       http://phpsx.org
 * @category   tests
 * @version    $Revision: 480 $
 */
class RecordTest extends \PHPUnit_Framework_TestCase
{
	protected function setUp()
	{
	}

	protected function tearDown()
	{
	}

	public function testImportJson()
	{
		$body = <<<DATA
{
	"id": 1,
	"title": "foobar",
	"active": 1,
	"count": 12,
	"rating": 12.45,
	"person": {
		"title": "Foo"
	},
	"tags": [{
		"title": "bar"
	},{
		"title": "foo"
	},{
		"title": "test"
	}]
}
DATA;

		// read json
		$reader  = new Reader\Json();
		$message = new Message(array(), $body);

		$result = $reader->read($message);

		// create news object
		$news = new News();
		$news->import($result);

		$this->assertEquals(1, $news->getId());
		$this->assertInternalType('integer', $news->getId());
		$this->assertEquals('foobar', $news->getTitle());
		$this->assertInternalType('string', $news->getTitle());
		$this->assertEquals(true, $news->getActive());
		$this->assertInternalType('boolean', $news->getActive());
		$this->assertEquals(12, $news->getCount());
		$this->assertInternalType('integer', $news->getCount());
		$this->assertEquals(12.45, $news->getRating());
		$this->assertInternalType('float', $news->getRating());
		$this->assertInstanceOf('PSX\Data\Person', $news->getPerson());
		$this->assertEquals(true, is_array($news->getTags()));

		foreach($news->getTags() as $tag)
		{
			$this->assertInstanceOf('PSX\Data\Tag', $tag);
		}
	}

	public function testExportJson()
	{
		$body = <<<DATA
{
	"id": 1,
	"title": "foobar",
	"active": 1,
	"count": 12,
	"rating": 12.45,
	"person": {
		"title": "Foo"
	},
	"tags": [{
		"title": "bar"
	},{
		"title": "foo"
	},{
		"title": "test"
	}]
}
DATA;

		// read json
		$reader  = new Reader\Json();
		$message = new Message(array(), $body);

		$result = $reader->read($message);

		$news = new News();
		$news->import($result);

		$writer = new Writer\Json();

		ob_start();

		$writer->write($news);

		$resp = ob_get_contents();

		ob_end_clean();

		$this->assertJsonStringEqualsJsonString($body, $resp);
	}


	public function testImportXml()
	{
		$body = <<<DATA
<news>
	<id>1</id>
	<title>foobar</title>
	<active>1</active>
	<count>12</count>
	<rating>12.45</rating>
	<person>
		<title>Foo</title>
	</person>
	<tags>
		<title>bar</title>
	</tags>
	<tags>
		<title>foo</title>
	</tags>
	<tags>
		<title>test</title>
	</tags>
</news>
DATA;

		// read json
		$reader  = new Reader\Xml();
		$message = new Message(array(), $body);

		$result = $reader->read($message);

		// create news object
		$news = new News();
		$news->import($result);

		$this->assertEquals(1, $news->getId());
		$this->assertEquals('foobar', $news->getTitle());
		$this->assertEquals(true, $news->getActive());
		$this->assertEquals(12, $news->getCount());
		$this->assertEquals(12.45, $news->getRating());
		$this->assertInstanceOf('PSX\Data\Person', $news->getPerson());
		$this->assertEquals(true, is_array($news->getTags()));

		foreach($news->getTags() as $tag)
		{
			$this->assertInstanceOf('PSX\Data\Tag', $tag);
		}
	}

	public function testExportXml()
	{
		$body = <<<DATA
<news>
	<id>1</id>
	<title>foobar</title>
	<active>true</active>
	<count>12</count>
	<rating>12.45</rating>
	<person>
		<title>Foo</title>
	</person>
	<tags>
		<title>bar</title>
	</tags>
	<tags>
		<title>foo</title>
	</tags>
	<tags>
		<title>test</title>
	</tags>
</news>
DATA;

		// read json
		$reader  = new Reader\Xml();
		$message = new Message(array(), $body);

		$result = $reader->read($message);

		$news = new News();
		$news->import($result);

		$writer = new Writer\Xml();

		ob_start();

		$writer->write($news);

		$resp = ob_get_contents();

		ob_end_clean();

		$this->assertXmlStringEqualsXmlString($body, $resp);
	}
}

class News extends RecordAbstract
{
	protected $id;
	protected $title;
	protected $active;
	protected $count;
	protected $rating;
	protected $person;
	protected $tags;

	public function getName()
	{
		return 'news';
	}

	public function getFields()
	{
		return array(
			'id'     => $this->id,
{'fraction_non_alphanumeric': 0.18309683843664426, 'fraction_numerical': 0.015309932785660941, 'mean_word_length': 3.9629400864731315, 'pattern_counts': {'":': 33, '<': 94, '<?xml version=': 0, '>': 163, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7195394', 'n_tokens_mistral': 3047, 'n_tokens_neox': 2765, 'n_words': 697}