<commit_before><?php

/**
 * LICENSE: This source code is subject to the license that is available
 * in the LICENSE file distributed along with this package.
 *
 * @package    Penelope
 * @author     <NAME> <<EMAIL>>
 * @copyright  Karwana Ltd
 * @since      File available since Release 1.0.0
 */

use Everyman\Neo4j;

use Karwana\Penelope\Schema;

class NodeTest extends \PHPUnit_Framework_TestCase {

	private static $schema, $client;

	public static function setUpBeforeClass() {
		$client = static::$client = new Neo4j\Client('localhost', 7474);
		$schema = static::$schema = new Schema($client);

		$schema->addNode('TEST', 'test-node', array('test-property'));
		$schema->addEdge('TEST_EDGE', 'test-edge', 'TEST', 'TEST', array('test-property'));
	}

	public function testGetPath_throwsForNodeWithNoId() {
		$this->setExpectedException('LogicException');
		$node = static::$schema->getNode('TEST')->create();
		$node->getPath();
	}

	public function testGetPath_returnsPath() {
		$node = static::$schema->getNode('TEST')->create();
		$node->save();
		$this->assertEquals('/test-node/' . $node->getId(), $node->getPath());
	}

	public function testSave_savesNode() {
		$node_a = static::$schema->getNode('TEST')->create();

		$this->assertNull($node_a->getId());

		$node_a->save();

		$this->assertNotNull($node_a->getId());
		$this->assertInstanceOf('Everyman\\Neo4j\Node', static::$client->getNode($node_a->getId()));
	}

	public function testDelete_deletesNode() {
		$node_a = static::$schema->getNode('TEST')->create();

		$node_a->save();

		$node_a_id = $node_a->getId();
		$this->assertNotNull($node_a_id);

		$node_a->delete();

		$this->assertNull($node_a->getId());
		$this->assertNull(static::$client->getNode($node_a_id));
	}

	public function testDelete_deletesNodeWithRelationship() {
		$node_schema = static::$schema->getNode('TEST');

		// Create an edge.
		$edge = static::$schema->getEdge('TEST_EDGE')->create();

		$this->assertNull($edge->getId());

		// Create the nodes.
		$node_a = $node_schema->create();
		$node_a->save();

		$this->assertNotNull($node_a->getId());

		$node_b = $node_schema->create();
		$node_b->save();

		$this->assertNotNull($node_b->getId());

		// Save the relationship.<commit_msg>Add node path tests<commit_after><?php

/**
 * LICENSE: This source code is subject to the license that is available
 * in the LICENSE file distributed along with this package.
 *
 * @package    Penelope
 * @author     <NAME> <<EMAIL>>
 * @copyright  Karwana Ltd
 * @since      File available since Release 1.0.0
 */

use Everyman\Neo4j;

use Karwana\Penelope\Schema;

class NodeTest extends \PHPUnit_Framework_TestCase {

	private static $schema, $client;

	public static function setUpBeforeClass() {
		$client = static::$client = new Neo4j\Client('localhost', 7474);
		$schema = static::$schema = new Schema($client);

		$schema->addNode('TEST_PERSON', 'test-person');
		$schema->addEdge('TEST_FRIEND', 'test-friend', 'TEST_PERSON', 'TEST_PERSON');

		$schema->addNode('TEST_CAR', 'test-car');
		$schema->addEdge('TEST_OWNER', 'test-owner', 'TEST_CAR', 'TEST_PERSON');
	}

	public function testGetPath_throwsForNodeWithNoId() {
		$this->setExpectedException('LogicException', 'Cannot create path for node with no ID.');
		$node = static::$schema->getNode('TEST_PERSON')->create();
		$node->getPath();
	}

	public function testGetPath_returnsPath() {
		$node = static::$schema->getNode('TEST_PERSON')->create();
		$node->save();
		$this->assertEquals('/test-person/' . $node->getId(), $node->getPath());
	}

	public function testGetEditPath_throwsForNodeWithNoId() {
		$this->setExpectedException('LogicException', 'Cannot create edit path for node with no ID.');
		$node = static::$schema->getNode('TEST_PERSON')->create();
		$node->getEditPath();
	}

	public function testGetEditPath_returnsPath() {
		$node = static::$schema->getNode('TEST_PERSON')->create();
		$node->save();
		$this->assertEquals('/test-person/' . $node->getId() . '/edit', $node->getEditPath());
	}

	public function testGetNewEdgePath_throwsForNodeWithNoId() {
		$this->setExpectedException('LogicException', 'Cannot create new edge path for node with no ID.');
		$node = static::$schema->getNode('TEST_PERSON')->create();
		$node->getNewEdgePath(static::$schema->getEdge('TEST_FRIEND'));
	}

	public function testGetNewEdgePath_throwsForUnrelatableSchema() {
		$this->setExpectedException('LogicException', 'Cannot create new edge path for unrelatable node.');
		$node = static::$schema->getNode('TEST_PERSON')->create();
		$node->save();
		$node->getNewEdgePath(static::$schema->getEdge('TEST_OWNER'));
	}

	public function testGetNewEdgePath_returnsPath() {
		$node = static::$schema->getNode('TEST_PERSON')->create();
		$node->save();
		$this->assertEquals('/test-person/' . $node->getId() . '/test-friend/new', $node->getNewEdgePath(static::$schema->getEdge('TEST_FRIEND')));
	}

	public function testGetEdgeCollectionPath_throwsForNodeWithNoId() {
		$this->setExpectedException('LogicException', 'Cannot create edge collection path for node with no ID.');
		$node = static::$schema->getNode('TEST_PERSON')->create();
		$node->getEdgeCollectionPath(static::$schema->getEdge('TEST_FRIEND'));
	}

	public function testGetEdgeCollectionPath_throwsForUnrelatableSchema() {
		$this->setExpectedException('LogicException', 'Cannot create edge collection path for unrelatable node.');
		$node = static::$schema->getNode('TEST_PERSON')->create();
		$node->save();
		$node->getEdgeCollectionPath(static::$schema->getEdge('TEST_OWNER'));
	}

	public function testGetEdgeCollectionPath_returnsPath() {
		$node = static::$schema->getNode('TEST_PERSON')->create();
		$node->save();
		$this->assertEquals('/test-person/' . $node->getId() . '/test-friend/', $node->getEdgeCollectionPath(static::$schema->getEdge('TEST_FRIEND')));
	}

	public function testFetch_throwsForNodeWithNoId() {
		$this->setExpectedException('LogicException', 'Cannot fetch without ID.');
		$node = static::$schema->getNode('TEST_PERSON')->create();
		$node->fetch();
	}

	public function testSave_savesNode() {
		$node_a = static::$schema->getNode('TEST_PERSON')->create();

		$this->assertNull($node_a->getId());

		$node_a->save();

		$this->assertNotNull($node_a->getId());
		$this->assertInstanceOf('Everyman\\Neo4j\Node', static::$client->getNode($node_a->getId()));
	}

	public function testDelete_deletesNode() {
		$node_a = static::$schema->getNode('TEST_PERSON')->create();

		$node_a->save();

		$node_a_id = $node_a->getId();
		$this->assertNotNull($node_a_id);

		$node_a->delete();

		$this->assertNull($node_a->getId());
		$this->assertNull(static::$client->getNode($node_a_id));
	}

	public function testDelete_deletesNodeWithRelationship() {
		$node_schema = static::$schema->getNode('TEST_PERSON');

		// Create an edge.
		$edge = static::$schema->getEdge('TEST_FRIEND')->create();

		$this->assertNull($edge->getId());

		// Create the nodes.
		$node_a = $node_schema->create();
		$node_a->save();

		$this->assertNotNull($node_a->getId());

		$node_b = $node_schema->create();
		$node_b->save();

		$this->assertNotNull($node_b->getId());

		// Save the relationship.
{'fraction_non_alphanumeric': 0.1893664526932738, 'fraction_numerical': 0.0027909572983533353, 'mean_word_length': 5.605529953917051, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 151, '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': '2105728', 'n_tokens_mistral': 2422, 'n_tokens_neox': 2333, 'n_words': 408}