<commit_before><?php
/**
 * ArangoDB PHP client testsuite
 * File: StatementTest.php
 *
 * @package triagens\ArangoDb
 * @author  <NAME>
 */

namespace triagens\ArangoDb;

/**
 * Class StatementTest
 *
 * @property Connection        $connection
 * @property Collection        $collection
 * @property CollectionHandler $collectionHandler
 * @property DocumentHandler   $documentHandler
 *
 * @package triagens\ArangoDb
 */
class StatementTest extends
    \PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        $this->connection        = getConnection();
        $this->collectionHandler = new CollectionHandler($this->connection);

        // clean up first
        try {
            $this->collectionHandler->delete('ArangoDB_PHP_TestSuite_TestCollection_01');
        } catch (\Exception $e) {
            // don't bother us, if it's already deleted.
        }

        $this->collection = new Collection();
        $this->collection->setName('ArangoDB_PHP_TestSuite_TestCollection_01');
        $this->collectionHandler->add($this->collection);
    }


    /**
     * This is just a test to really test connectivity with the server before moving on to further tests.
     */
    public function testExecuteStatement()
    {
        $connection      = $this->connection;
        $collection      = $this->collection;
        $document        = new Document();
        $documentHandler = new DocumentHandler($connection);

        $document->someAttribute = 'someValue';

        $documentHandler->add($collection->getId(), $document);

        $statement = new Statement($connection, array(
                                                     "query"     => '',
                                                     "count"     => true,
                                                     "batchSize" => 1000,
                                                     "_sanitize" => true,
                                                ));
        $statement->setQuery('FOR a IN `ArangoDB_PHP_TestSuite_TestCollection_01` RETURN a');
        $cursor = $statement->execute();

        $result = $cursor->current();

        $this->assertTrue(
             $result->someAttribute === 'someValue',
             'Expected value someValue, found :' . $result->someAttribute
        );
    }
    
    
    /**
     * Test warnings returned by statement
     */
    public function testStatementReturnNoWarnings()
    {
        $connection      = $this->connection;

        $statement = new Statement($connection, array("query"     => 'RETURN 1'));
        $cursor = $statement->execute();

        $this->assertEquals(0, count($cursor->getWarnings()));
    }

    /**
     * Test warnings returned by statement
     */
    public function testStatementReturnWarnings()
    {
        $connection      = $this->connection;

        $statement = new Statement($connection, array("query"     => 'RETURN 1/0'));
        $cursor = $statement->execute();

        $this->assertEquals(1, count($cursor->getWarnings()));
        $this->assertEquals(1562, $cursor->getWarnings()[0]["code"]);
    }
    
    
    /**
     * Test statistics returned by query
     */
    public function testStatisticsInsert()
    {
        $connection      = $this->connection;
        $collection      = $this->collection;

        $statement = new Statement($connection, array());
        $statement->setQuery('FOR i IN 1..1000 INSERT { _key: CONCAT("test", i) } IN ' . $collection->getName());
        $cursor = $statement->execute();<commit_msg>fixed code for PHP 5.3-compatibility<commit_after><?php
/**
 * ArangoDB PHP client testsuite
 * File: StatementTest.php
 *
 * @package triagens\ArangoDb
 * @author  <NAME>
 */

namespace triagens\ArangoDb;

/**
 * Class StatementTest
 *
 * @property Connection        $connection
 * @property Collection        $collection
 * @property CollectionHandler $collectionHandler
 * @property DocumentHandler   $documentHandler
 *
 * @package triagens\ArangoDb
 */
class StatementTest extends
    \PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        $this->connection        = getConnection();
        $this->collectionHandler = new CollectionHandler($this->connection);

        // clean up first
        try {
            $this->collectionHandler->delete('ArangoDB_PHP_TestSuite_TestCollection_01');
        } catch (\Exception $e) {
            // don't bother us, if it's already deleted.
        }

        $this->collection = new Collection();
        $this->collection->setName('ArangoDB_PHP_TestSuite_TestCollection_01');
        $this->collectionHandler->add($this->collection);
    }


    /**
     * This is just a test to really test connectivity with the server before moving on to further tests.
     */
    public function testExecuteStatement()
    {
        $connection      = $this->connection;
        $collection      = $this->collection;
        $document        = new Document();
        $documentHandler = new DocumentHandler($connection);

        $document->someAttribute = 'someValue';

        $documentHandler->add($collection->getId(), $document);

        $statement = new Statement($connection, array(
                                                     "query"     => '',
                                                     "count"     => true,
                                                     "batchSize" => 1000,
                                                     "_sanitize" => true,
                                                ));
        $statement->setQuery('FOR a IN `ArangoDB_PHP_TestSuite_TestCollection_01` RETURN a');
        $cursor = $statement->execute();

        $result = $cursor->current();

        $this->assertTrue(
             $result->someAttribute === 'someValue',
             'Expected value someValue, found :' . $result->someAttribute
        );
    }
    
    
    /**
     * Test warnings returned by statement
     */
    public function testStatementReturnNoWarnings()
    {
        $connection      = $this->connection;

        $statement = new Statement($connection, array("query"     => 'RETURN 1'));
        $cursor = $statement->execute();

        $this->assertEquals(0, count($cursor->getWarnings()));
    }

    /**
     * Test warnings returned by statement
     */
    public function testStatementReturnWarnings()
    {
        $connection      = $this->connection;

        $statement = new Statement($connection, array("query"     => 'RETURN 1/0'));
        $cursor = $statement->execute();

        $this->assertEquals(1, count($cursor->getWarnings()));
        $warnings = $this->getWarnings();
        $this->assertEquals(1562, $warnings[0]["code"]);
    }
    
    
    /**
     * Test statistics returned by query
     */
    public function testStatisticsInsert()
    {
        $connection      = $this->connection;
        $collection      = $this->collection;

        $statement = new Statement($connection, array());
        $statement->setQuery('FOR i IN 1..1000 INSERT { _key: CONCAT("test", i) } IN ' . $collection->getName());
        $cursor = $statement->execute();
{'fraction_non_alphanumeric': 0.1259134345137718, 'fraction_numerical': 0.007307476110174255, 'mean_word_length': 2.0004215851602023, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6476678', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1723, 'n_words': 395}