2011-11-05

Development Setup for Neo4j and PHP: Part 2

Update 2014-02-15: Using Neo4jPHP from a downloaded PHAR file has been deprecated. The preferred and supported way to install the library is via Composer. The examples below have been updated to reflect this. It has also been updated for the 2.0 release of Neo4j.

This is Part 2 of a series on setting up a development environment for building projects using the graph database Neo4j and PHP. In Part 1 of this series, we set up unit test and development databases. In this part, we'll build a skeleton project that includes unit tests, and a minimalistic user interface.

All the files will live under a directory on our web server. In a real project, you'll probably want only the user interface files under the web server directory and your testing and library files somewhere more protected.

Also, I won't be using any specific PHP framework. The principles in the code below should apply equally to any framework you decide to use.

Create the Project and Testing Harness

Create a project in a directory on your web server. For this project, mine is "/var/www/neo4play" on my local host. We'll also need a Neo4j client library. I recommend Neo4jPHP (disclaimer: I'm the author) and all the code samples in Part 2 use it.
> cd /var/www/neo4play
> mkdir -p tests/unit
> mkdir lib
> echo '{"require":{"everyman/neo4jphp":"dev-master"}}' > composer.json
> composer install
> echo "<?php phpinfo(); ?>" > index.php
Test the setup by browsing to http://localhost/neo4play. You should see the output of `phpinfo`.

Now we'll create a bootstrap file that we can include to do project-wide and environment specific setup. Call this file "bootstrap.php" in the root project directory.
<?php
require_once(__DIR__.'/vendor/autoload.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;
$client = new Everyman\Neo4j\Client($host, $port);
The main point of this file at the moment is to differentiate between our development and testing environments, and set up our connection to the correct database. We do this by attaching the database client to the correct port based on an application constant.

We'll use the bootstrap file to setup a different, unit testing specific bootstrap file. Create the following file as "tests/bootstap-test.php":
<?php
define('APPLICATION_ENV', 'testing');
require_once(__DIR__.'/../bootstrap.php');

// Clean the database
$query = new Everyman\Neo4j\Cypher\Query($client, 'MATCH n-[r]-m DELETE n, r, m');
$query->getResultSet();
The purpose of this file to to tell our application bootstrap that we are in the "testing" environment. Then it cleans out the database so that our tests run from a known state.

Tell PHPUnit to use our test bootstrap with the following config file, called "tests/phpunit.xml":
<phpunit colors="true" bootstrap="./bootstrap-test.php">
    <testsuite name="Neo4j Play Test Results">
        <directory>./unit</directory>
    </testsuite>
</phpunit>
And because we're following TDD, we'll create our first test file, "tests/unit/ActorTest.php":
<?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);
    }
}
So we know we want a domain object called "Actor" (apparently we're building some sort of movie application) and that Actors have names and ids. We also know we want to be able to look up an Actor by their name. If we can't find the Actor by name, we should get a `null` value back.

Run the tests:
> cd tests
> phpunit
Excellent, our tests failed! If you've been playing along, they probably failed because the "Actor" class isn't defined. Our next step is to start creating our domain objects.

Defining the Application Domain

So far, we only have one domain object, and a test that asserts its behavior. In order to make the test pass, we'll need to connect to the database, persist entities to it, and then query it for those entities.

For persisting our entities, we'll need a way to get the client connection in our "Actor" class and any other domain object classes we define. To do this, we'll create an application registry/dependency-injection container/pattern-buzzword-of-the-month class. Put the following in the file "lib/Neo4Play.php":
<?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;
    }
}
Now our domain objects will have access to the client connection through `Neo4Play::client()` when we persist them to the database. It's time to define our actor class, in the file "lib/Actor.php":
<?php
use Everyman\Neo4j\Node,
    Everyman\Neo4j\Index;

class Actor
{
    public $id = null;
    public $name = '';

    public static function save(Actor $actor)
    {
    }

    public static function getActorByName($name)
    {
    }
}
Requiring our classes and setting up the client connection is part of the bootstrapping process of the application, so we'll need to add some thing to "bootstrap.php":
<?php
require_once(__DIR__.'/vendor/autoload.php');
require_once(__DIR__.'/lib/Neo4Play.php');
require_once(__DIR__.'/lib/Actor.php');

// ** set up error reporting, environment and connection... **//

Neo4Play::setClient($client);
We have a stub class for the domain object. The tests will still fail when we run them again, but at least all the classes should be found correctly.

Let's start with finding an Actor by name. With our knowledge of graph databases, we know this will involve an index lookup, and that we will get a Node object in return. If the lookup returns no result, we'll get a `null`. If we do get a Node back, we'll want to hold on to it, for updating the Actor later.

Modify the Actor class with the following contents:
class Actor
{
    //

    protected $node = null;

    //

    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;
    }
}
The main thing we're trying to accomplish here is keeping our domain classes as Plain-Old PHP Objects, that don't require any special class inheritance or interface, and that hide the underlying persistence layer from the outside world.

The tests still fail. We'll finish up our Actor class by saving the Actor to the database.
class Actor
{
    //

    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);
    }

    //
}
Run the tests again. If you see all green, then everything is working properly. To double check, browse to the testing instance webadmin panel http://localhost:7475/webadmin/#. You should see 2 nodes and 1 property (Why 2 nodes? Because there is a node 0 -- the reference node -- that is not deleted when the database is cleaned out.)

Build Something Useful

It's time to start tacking on some user functionality to our application. Thanks to our work on the unit tests, we can create actors in the database and find them again via an exact name match. Let's expose that functionality.

Change the contents of "index.php" to the following:
<?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; ?>
Browse to your index file. Mine is at http://localhost/neo4play/index.php.

You should see the page you just created. Enter a name in the "Add Actor Name" box and click the "Add" button. If everything went according to plan, you should see the actor name and the id assigned to the actor by the database.

Try finding that actor using the search box. Note the actor's id.

Browse to http://localhost:7474/webadmin/# and click the "Data browser" tab. Enter the actor id in the text box at the top. The node you created when you added the actor should show up.

The interesting thing is that our actual application doesn't know anything about how the Actors are stored. Nothing in "index.php" references graphs or nodes or indexes. This means that, in theory, we could swap out the persistence layer for a SQL databases later, or MongoDB, or anything else, and nothing in our application would have to change. If we started with a SQL database, we could easily transition to a graph database.

Explore the Wonderful World of Graphs

Your development environment is now set up, and your application is bootstrapped. There's a lot more to add to this application, including creating movies, and linking actors and movies together. Maybe you'll want to add a social aspect, with movie recommendations. Graph databases are powerful tools that enable such functionality to be added easily.

Go ahead and explore the rest of the Neo4jPHP library (wiki and API). Also, be sure to checkout the Neo4j documentation, especially the sections about the REST API, Cypher and Gremlin (two powerful graph querying and processing languages.)

All the code for this sample application is available as a gist: http://gist.github.com/1341833.

Happy graphing!

23 comments:

  1. Hi i get this error when i try to add some node whit the index.php

    "  Fatal error: Uncaught exception 'Everyman\Neo4j\Exception' with message 'Unable to search index [500]: Headers: Array ( ) Body: Array ( [error] => Couldn't resolve host 'xubuntu' [6] ) ' in phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Command.php:116 Stack trace: #0 phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Command/SearchIndex.php(95): Everyman\Neo4j\Command->throwException('Unable to searc...', 500, Array, Array) #1 phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Command.php(69): Everyman\Neo4j\Command\SearchIndex->handleResult(500, Array, Array) #2 phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Client.php(571): Everyman\Neo4j\Command->execute() #3 phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Client.php(513): Everyman\Neo4j\Client->runCommand(Object(Everyman\Neo4j\Command\SearchIndex)) #4 phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Index.php(77): Everyman\Neo4j\Client->searchIndex(Object(Everyman\Neo4j\Index), 'name', inphar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Command.php on line 116   "

    ReplyDelete
  2. Sorry this one is when i try to save. another one is when i search  Please help .
    AND THANKS JOSH ADELL GREAT JOB.
    "  Fatal error: Uncaught exception 'Everyman\Neo4j\Exception' with message 'Unable to create node [500]: Headers: Array ( ) Body: Array ( [error] => Couldn't resolve host 'xubuntu' [6] ) ' in phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Command.php:116 Stack trace: #0 phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Command/CreateNode.php(68): Everyman\Neo4j\Command->throwException('Unable to creat...', 500, Array, Array) #1 phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Command.php(69): Everyman\Neo4j\Command\CreateNode->handleResult(500, Array, Array) #2 phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Client.php(571): Everyman\Neo4j\Command->execute() #3 phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Client.php(478): Everyman\Neo4j\Client->runCommand(Object(Everyman\Neo4j\Command\CreateNode)) #4 phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Node.php(94): Everyman\Neo4j\Client->saveNode(Object(Everyman\Neo4j\Node)) #5 /var/www/neo4 inphar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Command.php on line 116"

    ReplyDelete
  3. If you are using the example scripts from the Gist, you will need to change the $host variable in bootstrap.php to be 'localhost' or whatever the hostname of your Neo4j instance is.

    ReplyDelete
  4. Wow ur awesome

    ReplyDelete
  5. Not able execute phpunit giving error while testing :-1) ActorTest::testCreateActorAndRetrieveByName

    Everyman\Neo4j\Exception: Unable to create node [503]:

    Headers: Array

    (

    [Server] => squid/3.2.3

    [Mime-Version] => 1.0

    [Date] => Tue, 19 Mar 2013 07:37:23 GMT

    [Content-Type] => text/html

    [Content-Length] => 3299

    [X-Squid-Error] => ERR_CONNECT_FAIL 111

    [X-Cache] => MISS from localhost

    [X-Cache-Lookup] => MISS from localhost:8080

    [Via] => 1.1 localhost (squid/3.2.3)

    [Connection] => close

    )

    Body: Array

    (

    )

    phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Command.php:116

    phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Command/CreateNode.php:68

    phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Command.php:69

    phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Client.php:587

    phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Client.php:494

    phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Node.php:110

    /var/www/neo4play/lib/Actor.php:18

    /var/www/neo4play/tests/unit/ActorTest.php:8

    2) ActorTest::testActorDoesNotExist

    Everyman\Neo4j\Exception: Unable to search index [503]:

    Headers: Array

    (

    [Server] => squid/3.2.3

    [Mime-Version] => 1.0

    [Date] => Tue, 19 Mar 2013 07:37:23 GMT

    [Content-Type] => text/html

    [Content-Length] => 3435

    [X-Squid-Error] => ERR_CONNECT_FAIL 111

    [X-Cache] => MISS from localhost

    [X-Cache-Lookup] => MISS from localhost:8080

    [Via] => 1.1 localhost (squid/3.2.3)

    [Connection] => close

    )

    Body: Array

    (

    )

    phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Command.php:116

    phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Command/SearchIndex.php:95

    phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Command.php:69

    phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Client.php:587

    phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Client.php:529

    phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Index.php:77

    /var/www/neo4play/lib/Actor.php:28

    /var/www/neo4play/tests/unit/ActorTest.php:21

    FAILURES!

    Tests: 2, Assertions: 0, Errors: 2.

    ReplyDelete
  6. Looks like the script was unable to connect to the database. Is Neo4j running on port 8080? What happens if you use a browser to go to localhost:8080?

    ReplyDelete
  7. podrias ayudarme, tengo este error

    Fatal error: Uncaught exception 'Everyman\Neo4j\Exception' with message 'cUrl extension not enabled/installed' in phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Transport.php:32 Stack trace: #0 /var/www/neo4play/bootstrap.php(15): Everyman\Neo4j\Transport->__construct('localhost', 7474) #1 /var/www/neo4play/index.php(2): require_once('/var/www/neo4pl...') #2 {main} thrown in phar:///var/www/neo4play/lib/neo4jphp.phar/lib/Everyman/Neo4j/Transport.php on line 32

    ReplyDelete
  8. gracias, pero ya encontre la solucion y ya me funciona bien el ejemplo....
    como lo desia el error no tenia instalado el "curl"
    lo que hice fue:
    sudo apt-get install php5-curl
    y listo funciono el ejemplo...... :D

    ReplyDelete
  9. Hello, I am new to neo4j. I don't have experience with Linux os. So can you please explain the process for windows? Thank you.

    ReplyDelete
  10. PHP Fatal error: Uncaught exception 'PharException' with message 'phar "/var/www/neo4play/lib/neo4jphp.phar" has a broken signature' in /var/www/neo4play/lib/neo4jphp.phar:2

    ReplyDelete
  11. if i include the phar file, everything is OK. but i wanted to modify the source so i'm including the files via spl_autoload_register, and i have this error:

    Cannot instantiate abstract class Everyman\Neo4j\Transport


    what is so special inside the phar file which makes it possible to instantiate an abstract class?

    ReplyDelete
  12. You can't instantiate Everyman\Neo4j\Transport, since it is an abstract class. You must instantiate Everyman\Neo4j\Transport\Curl or Everyman\Neo4j\Transport\Stream depending on your needs. If any documentation is unclear on this, please file an issue in the github repo.

    ReplyDelete
  13. thank you for answer. i know i can't instantiate an abstract class. that's why i'm asking why, using the phar file, a can do that? does it have any feature inside it which process the line "new Everyman\Neo4j\Transport" into "Everyman\Neo4j\Transport\Curl" ?

    ReplyDelete
  14. The PHAR file is using a very out-of-date version of neo4jphp, back when the Transport class was not abstract. I suggest not using the PHAR file and using Composer install.

    ReplyDelete
  15. HI,

    Where the var directory was created?

    ReplyDelete
  16. That's really great! Thank you.

    ReplyDelete
  17. How to make the connection with username and password?

    ReplyDelete
  18. The documentation shows how to connect with basic HTTP authentication https://github.com/jadell/neo4jphp/wiki/Getting-started

    ReplyDelete
  19. Your composer install is not set up to allow "development" level packages. Change the require line to be:

    "everyman/neo4jphp": "dev-master@dev"

    ReplyDelete
  20. You shouldn't be using the composer.json from the neo4jphp library; that is for package managers like Packagist to use.

    You should write your own composer.json for your project. From the Getting Started page of the wiki, make a composer.json file that looks like:

    {
    "require": {
    "everyman/neo4jphp": "dev-master"
    }
    }

    Then do `composer install` in the root of your project to install the library.

    ReplyDelete
  21. Thanks Josh, that worked

    ReplyDelete
  22. hi, your post is greati. But i when i got a proplem with testcase
    Exception: Serialization of 'Closure' is not allowed. how can i solve it.

    ReplyDelete