Create a gist now

Instantly share code, notes, and snippets.

@jadell /Actor.php
Last active Jun 2, 2016

What would you like to do?
Neo4j and PHP sample application
<?php
use Everyman\Neo4j\Node,
Everyman\Neo4j\Index;
class Actor
{
public $id = null;
public $name = '';
protected $node = null;
public static function save(Actor $actor)
{
if (!$actor->node) {
$actor->node = new Node(Neo4Play::client());
}
$actor->node->setProperty('name', $actor->name);
$actor->node->save();
$actor->id = $actor->node->getId();
$actorIndex = new Index(Neo4Play::client(), Index::TypeNode, 'actors');
$actorIndex->add($actor->node, 'name', $actor->name);
}
public static function getActorByName($name)
{
$actorIndex = new Index(Neo4Play::client(), Index::TypeNode, 'actors');
$node = $actorIndex->findOne('name', $name);
if (!$node) {
return null;
}
$actor = new Actor();
$actor->id = $node->getId();
$actor->name = $node->getProperty('name');
$actor->node = $node;
return $actor;
}
}
<?php
class ActorTest extends PHPUnit_Framework_TestCase
{
public function testCreateActorAndRetrieveByName()
{
$actor = new Actor();
$actor->name = 'Test Guy '.rand();
Actor::save($actor);
$actorId = $actor->id;
self::assertNotNull($actorId);
$retrievedActor = Actor::getActorByName($actor->name);
self::assertInstanceOf('Actor', $retrievedActor);
self::assertEquals($actor->id, $retrievedActor->id);
self::assertEquals($actor->name, $retrievedActor->name);
}
public function testActorDoesNotExist()
{
$retrievedActor = Actor::getActorByName('Test Guy '.rand());
self::assertNull($retrievedActor);
}
}
<?php
define('APPLICATION_ENV', 'testing');
require_once('../bootstrap.php');
$transport->delete('/cleandb/secret-key');
<?php
require_once(__DIR__.'/vendor/autoload.php');
require_once(__DIR__.'/lib/Neo4Play.php');
require_once(__DIR__.'/lib/Actor.php');
error_reporting(-1);
ini_set('display_errors', 1);
if (!defined('APPLICATION_ENV')) {
define('APPLICATION_ENV', 'development');
}
$host = 'localhost';
$port = (APPLICATION_ENV == 'development') ? 7474 : 7475;
$transport = new Everyman\Neo4j\Transport($host, $port);
$client = new Everyman\Neo4j\Client($transport);
Neo4Play::setClient($client);
<?php
require_once('bootstrap.php');
if (!empty($_POST['actorName'])) {
$actor = new Actor();
$actor->name = $_POST['actorName'];
Actor::save($actor);
} else if (!empty($_GET['actorName'])) {
$actor = Actor::getActorByName($_GET['actorName']);
}
?>
<form action="" method="POST">
Add Actor Name: <input type="text" name="actorName" />
<input type="submit" value="Add" />
</form>
<form action="" method="GET">
Find Actor Name: <input type="text" name="actorName" />
<input type="submit" value="Search" />
</form>
<?php if (!empty($actor)) : ?>
Name: <?php echo $actor->name; ?><br />
Id: <?php echo $actor->id; ?><br />
<?php elseif (!empty($_GET['actorName'])) : ?>
No actor found by the name of "<?php echo $_GET['actorName']; ?>"<br />
<?php endif; ?>
<?php
class Neo4Play
{
protected static $client = null;
public static function client()
{
return self::$client;
}
public static function setClient(Everyman\Neo4j\Client $client)
{
self::$client = $client;
}
}
<phpunit colors="true" bootstrap="./bootstrap-test.php">
<testsuite name="Neo4j Play Test Results">
<directory>./unit</directory>
</testsuite>
</phpunit>

Rmota commented Feb 13, 2014

I'm following the steps of your blog. But i'm stuck at bootstrap.php file. Which diretory do I need to create it?

what about the /vendor/autoload.php in bootsrap.php ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment