<commit_before><?php

use TenUp\Exodus\Migrator\Parsers\JSON as JSON;

function is_email() {
	return false;
}

class JSONTest extends PHPUnit_Framework_TestCase {

	protected $json_parser;

	protected function setUp() {
		$schema = Mockery::mock( 'TenUp\Exodus\Schema\Base_Schema' );
		$schema->shouldReceive( 'build' )->andReturn( $this->get_schema_build() )->once();
		$schema->shouldReceive( 'keys' )->andReturn( $this->get_key_build() )->once();

		$this->json_parser = new JSON( $this->get_data(), $schema );
	}

	public function testContentIsGettingPopulated() {
		$this->assertEquals( 2, $this->json_parser->get_content_count() );
		$this->assertCount( 2, $this->json_parser->get_content() );
	}

	public function testUpdateIterator() {
		$data = json_decode( json_encode( array(
			"item" => array(
				"child" => array(
					"childitem" => 'content'
				)
			)
		) ), false );

		$iterator = $this->invokeMethod( $this->json_parser, 'update_iterator_path', array( $data ) );
		$this->assertObjectHasAttribute( 'item', $iterator );

		$this->json_parser->schema->iterator = 'child.childitem';
		$update_iterator                     = $this->invokeMethod( $this->json_parser, 'update_iterator_path', array( $data->item ) );
		$this->assertEquals( 'content', $update_iterator );
	}

	public function testBuildingPostObject() {
		$content = $this->json_parser->data;
		$import_object = $this->invokeMethod( $this->json_parser, 'build_post_object', array( $content[0] ) );

		$this->assertObjectHasAttribute( 'post_title', $import_object );
		$this->assertObjectHasAttribute( 'post_date', $import_object );
		$this->assertObjectHasAttribute( 'post_content', $import_object );
		$this->assertObjectHasAttribute( 'post_author', $import_object );
		$this->assertObjectHasAttribute( 'user_login', $import_object->post_author );
	}

	protected function tearDown() {
		\Mockery::close();
	}

	public function invokeMethod( &$object, $methodName, array $parameters = array() ) {
		$reflection = new \ReflectionClass( get_class( $object ) );
		$method     = $reflection->getMethod( $methodName );
		$method->setAccessible( true );

		return $method->invokeArgs( $object, $parameters );
	}

	protected function get_data() {
		return '[{
	        "timestamp": 1408464060,
	        "title": "Test title",
	        "body": "body content",
	        "author": "<NAME>"
	      },{
	        "timestamp": 1408677660,<commit_msg>seup is_emial mock before test run<commit_after><?php

use TenUp\Exodus\Migrator\Parsers\JSON as JSON;


class JSONTest extends \WP_Mock\Tools\TestCase{

	protected $json_parser;

	public function setUp() {
		parent::setUp();
		$schema = Mockery::mock( 'TenUp\Exodus\Schema\Base_Schema' );
		$schema->shouldReceive( 'build' )->andReturn( $this->get_schema_build() )->once();
		$schema->shouldReceive( 'keys' )->andReturn( $this->get_key_build() )->once();

		$content = json_decode( $this->get_data() );

		\WP_Mock::wpFunction( 'is_email', array(
			'times' => '2+',
			'args' => array( $content[0]->author ),
			'return' => false
		) );

		$this->json_parser = new JSON( $this->get_data(), $schema );
	}

	public function testContentIsGettingPopulated() {
		$this->assertEquals( 2, $this->json_parser->get_content_count() );
		$this->assertCount( 2, $this->json_parser->get_content() );
	}

	public function testUpdateIterator() {
		$data = json_decode( json_encode( array(
			"item" => array(
				"child" => array(
					"childitem" => 'content'
				)
			)
		) ), false );

		$iterator = $this->invokeMethod( $this->json_parser, 'update_iterator_path', array( $data ) );
		$this->assertObjectHasAttribute( 'item', $iterator );

		$this->json_parser->schema->iterator = 'child.childitem';
		$update_iterator                     = $this->invokeMethod( $this->json_parser, 'update_iterator_path', array( $data->item ) );
		$this->assertEquals( 'content', $update_iterator );
	}

	public function testBuildingPostObject() {
		$content = $this->json_parser->data;
		$import_object = $this->invokeMethod( $this->json_parser, 'build_post_object', array( $content[0] ) );

		$this->assertObjectHasAttribute( 'post_title', $import_object );
		$this->assertObjectHasAttribute( 'post_date', $import_object );
		$this->assertObjectHasAttribute( 'post_content', $import_object );
		$this->assertObjectHasAttribute( 'post_author', $import_object );
		$this->assertObjectHasAttribute( 'user_login', $import_object->post_author );
	}

	public  function tearDown() {
		parent::tearDown();
		\Mockery::close();
	}

	public function invokeMethod( &$object, $methodName, array $parameters = array() ) {
		$reflection = new \ReflectionClass( get_class( $object ) );
		$method     = $reflection->getMethod( $methodName );
		$method->setAccessible( true );

		return $method->invokeArgs( $object, $parameters );
	}

	protected function get_data() {
		return '[{
	        "timestamp": 1408464060,
	        "title": "Test title",
	        "body": "body content",
	        "author": "<NAME>"
	      },{
	        "timestamp": 1408677660,
{'fraction_non_alphanumeric': 0.1696837079769246, 'fraction_numerical': 0.00954843843246469, 'mean_word_length': 4.495081967213115, 'pattern_counts': {'":': 10, '<': 7, '<?xml version=': 0, '>': 94, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6327078', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1600, 'n_words': 309}