<commit_before><?php
/**
 * ArangoDB PHP client testsuite
 * File: documentextendedtest.php
 *
 * @package ArangoDbPhpClient
 * @author <NAME>
 */

namespace triagens\ArangoDb;

class DocumentExtendedTest extends \PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        $this->connection = getConnection();
        $this->collectionHandler = new \triagens\ArangoDb\CollectionHandler($this->connection);
        $this->collection = new \triagens\ArangoDb\Collection();
        $this->collection->setName('ArangoDB-PHP-TestSuite-TestCollection-01');
        $this->collectionHandler->add($this->collection);
        $this->documentHandler = new DocumentHandler($this->connection);
    }

    /**
     * test for creation, get, and delete of a document given its settings through createFromArray()
     */
    public function testCreateGetAndDeleteDocumentThroughCreateFromArray()
    {
        $documentHandler = $this->documentHandler;

        $document = Document::createFromArray(array('someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue'));
        $documentId = $documentHandler->add($this->collection->getId(), $document);

        $this->assertTrue(is_numeric($documentId), 'Did not return an id!');

        $resultingDocument = $documentHandler->get($this->collection->getId(), $documentId);

        $this->assertObjectHasAttribute('_id', $resultingDocument, '_id field should exist, empty or with an id');
        $this->assertTrue(true === ($resultingDocument->someAttribute == 'someValue'));
        $this->assertTrue(true === ($resultingDocument->someOtherAttribute == 'someOtherValue'));

        $response = $documentHandler->delete($document);
        $this->assertTrue(true === $response, 'Delete should return true!');
    }

    /**
     * test for creation, get by example, and delete of a document given its settings through createFromArray()
     */
    public function testCreateGetbyExampleAndDeleteDocumentThroughCreateFromArray()
    {
        $documentHandler = $this->documentHandler;

        $document = Document::createFromArray(array('someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue'));
        $documentId = $documentHandler->add($this->collection->getId(), $document);

        $this->assertTrue(is_numeric($documentId), 'Did not return an id!');

        $cursor = $documentHandler->getByExample($this->collection->getId(), $document);

        $this->assertInstanceOf('triagens\ArangoDb\Cursor', $cursor);
        $resultingDocument=$cursor->current();
        
        $this->assertTrue(true === ($resultingDocument->someAttribute == 'someValue'));
        $this->assertTrue(true === ($resultingDocument->someOtherAttribute == 'someOtherValue'));

        $response = $documentHandler->delete($document);
        $this->assertTrue(true === $response, 'Delete should return true!');
    }

    /**
     * test for creation, update, get, and delete of a document given its settings through createFromArray()
     */
    public function testCreateUpdateGetAndDeleteDocumentThroughCreateFromArray()
    {
        $documentHandler = $this->documentHandler;

        $document = Document::createFromArray(array('someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue'));
        $documentId = $documentHandler->add($this->collection->getId(), $document);

        $this->assertTrue(is_numeric($documentId), 'Did not return an id!');

        $document->set('someAttribute','someValue2');
        $document->set('someOtherAttribute','someOtherValue2');
        $result = $documentHandler->update($document);

        $this->assertTrue($result);
        $resultingDocument = $documentHandler->get($this->collection->getId(), $documentId);

        $this->assertObjectHasAttribute('_id', $resultingDocument, '_id field should exist, empty or with an id');
        
        $this->assertTrue(true === ($resultingDocument->someAttribute == 'someValue2'));
        $this->assertTrue(true === ($resultingDocument->someOtherAttribute == 'someOtherValue2'));

        $response = $documentHandler->delete($document);
        $this->assertTrue(true === $response, 'Delete should return true!');
    }


    /**
     * test to set some attributes and get all attributes of the document through getAll()
     */
    public function testGetAll()
    {
        $documentHandler = $this->documentHandler;

        $document = Document::createFromArray(array('someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue'));
        $documentHandler->add($this->collection->getId(), $document);

        $result = $document->getAll();

        $this->assertTrue(true === ($result['someAttribute'] == 'someValue'));
        $this->assertTrue(true === ($result['someOtherAttribute'] == 'someOtherValue'));
    }<commit_msg>Implemented tests for getAll(includeInternals) and revision check on replace<commit_after><?php
/**
 * ArangoDB PHP client testsuite
 * File: documentextendedtest.php
 *
 * @package ArangoDbPhpClient
 * @author <NAME>
 */

namespace triagens\ArangoDb;

class DocumentExtendedTest extends \PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        $this->connection = getConnection();
        $this->collectionHandler = new \triagens\ArangoDb\CollectionHandler($this->connection);
        $this->collection = new \triagens\ArangoDb\Collection();
        $this->collection->setName('ArangoDB-PHP-TestSuite-TestCollection-01');
        $this->collectionHandler->add($this->collection);
        $this->documentHandler = new DocumentHandler($this->connection);
    }

    /**
     * test for creation, get, and delete of a document given its settings through createFromArray()
     */
    public function testCreateGetAndDeleteDocumentThroughCreateFromArray()
    {
        $documentHandler = $this->documentHandler;

        $document = Document::createFromArray(array('someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue'));
        $documentId = $documentHandler->add($this->collection->getId(), $document);

        $this->assertTrue(is_numeric($documentId), 'Did not return an id!');

        $resultingDocument = $documentHandler->get($this->collection->getId(), $documentId);

        $this->assertObjectHasAttribute('_id', $resultingDocument, '_id field should exist, empty or with an id');
        $this->assertTrue(true === ($resultingDocument->someAttribute == 'someValue'));
        $this->assertTrue(true === ($resultingDocument->someOtherAttribute == 'someOtherValue'));

        $response = $documentHandler->delete($document);
        $this->assertTrue(true === $response, 'Delete should return true!');
    }

    /**
     * test for creation, get by example, and delete of a document given its settings through createFromArray()
     */
    public function testCreateGetbyExampleAndDeleteDocumentThroughCreateFromArray()
    {
        $documentHandler = $this->documentHandler;

        $document = Document::createFromArray(array('someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue'));
        $documentId = $documentHandler->add($this->collection->getId(), $document);

        $this->assertTrue(is_numeric($documentId), 'Did not return an id!');

        $cursor = $documentHandler->getByExample($this->collection->getId(), $document);

        $this->assertInstanceOf('triagens\ArangoDb\Cursor', $cursor);
        $resultingDocument=$cursor->current();
        
        $this->assertTrue(true === ($resultingDocument->someAttribute == 'someValue'));
        $this->assertTrue(true === ($resultingDocument->someOtherAttribute == 'someOtherValue'));

        $response = $documentHandler->delete($document);
        $this->assertTrue(true === $response, 'Delete should return true!');
    }

    /**
     * test for creation, update, get, and delete of a document given its settings through createFromArray()
     */
    public function testCreateUpdateGetAndDeleteDocumentThroughCreateFromArray()
    {
        $documentHandler = $this->documentHandler;

        $document = Document::createFromArray(array('someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue'));
        $documentId = $documentHandler->add($this->collection->getId(), $document);

        $this->assertTrue(is_numeric($documentId), 'Did not return an id!');

        $document->set('someAttribute','someValue2');
        $document->set('someOtherAttribute','someOtherValue2');
        $result = $documentHandler->update($document);

        $this->assertTrue($result);
        $resultingDocument = $documentHandler->get($this->collection->getId(), $documentId);

        $this->assertObjectHasAttribute('_id', $resultingDocument, '_id field should exist, empty or with an id');
        
        $this->assertTrue(true === ($resultingDocument->someAttribute == 'someValue2'));
        $this->assertTrue(true === ($resultingDocument->someOtherAttribute == 'someOtherValue2'));

        $response = $documentHandler->delete($document);
        $this->assertTrue(true === $response, 'Delete should return true!');
    }
    
    /**
     * test for creation, update, get, and delete of a document given its settings through createFromArray()
     */
    public function testCreateUpdateGetAndDeleteDocumentWithRevisionCheck()
    {
        $documentHandler = $this->documentHandler;

        $document = Document::createFromArray(array('someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue'));
        $documentId = $documentHandler->add($this->collection->getId(), $document);

        $this->assertTrue(is_numeric($documentId), 'Did not return an id!');

        $resultingDocument = $documentHandler->get($this->collection->getId(), $documentId);

        $this->assertObjectHasAttribute('_id', $resultingDocument, '_id field should exist, empty or with an id');

        
        // Set some new values on the attributes and include the revision in the _rev attribute
        // This should result in a successfull update
        $document->set('someAttribute','someValue2');
        $document->set('someOtherAttribute','someOtherValue2');
        $document->set('_rev',$resultingDocument->getRevision());

        $result = $documentHandler->update($document, 'error');

        $this->assertTrue($result);
        $resultingDocument = $documentHandler->get($this->collection->getId(), $documentId);
        
        $this->assertTrue(true === ($resultingDocument->someAttribute == 'someValue2'));
        $this->assertTrue(true === ($resultingDocument->someOtherAttribute == 'someOtherValue2'));
        
        // Set some new values on the attributes and include a fake revision in the _rev attribute
        // This should result in a successfull update
        $document->set('someAttribute','someValue3');
        $document->set('someOtherAttribute','someOtherValue3');
        $document->set('_rev',$resultingDocument->getRevision()-1000);

        try {
                 $result = $documentHandler->update($document, 'error');
        } catch (\Exception $e) {
            // don't bother us... just give us the $e
        }

        $this->assertInstanceOf('Exception', $e);
        $this->assertTrue($e->getMessage() == 'HTTP/1.1 412 Precondition Failed');
        $resultingDocument = $documentHandler->get($this->collection->getId(), $documentId);
        
        $this->assertTrue(true === ($resultingDocument->someAttribute == 'someValue2'));
        $this->assertTrue(true === ($resultingDocument->someOtherAttribute == 'someOtherValue2'));
        unset ($e);
        
        $document = Document::createFromArray(array('someAttribute' => 'someValue3', 'someOtherAttribute' => 'someOtherValue3'));
        $document->setInternalId($this->collection->getId().'/'.$documentId);
        // Set some new values on the attributes and  _rev attribute to NULL
        // This should result in a successfull update
        try {
                 $result = $documentHandler->update($document, 'error');
        } catch (\Exception $e) {
            // don't bother us... just give us the $e
        }
        $resultingDocument = $documentHandler->get($this->collection->getId(), $documentId);
        
        $this->assertTrue(true === ($resultingDocument->someAttribute == 'someValue3'));
        $this->assertTrue(true === ($resultingDocument->someOtherAttribute == 'someOtherValue3'));

        
        
        
        
        $response = $documentHandler->delete($document);
        $this->assertTrue(true === $response, 'Delete should return true!');
    }


    /**
     * test to set some attributes and get all attributes of the document through getAll()
     * Also testing to optionally get internal attributes _id and _rev 
     */
    public function testGetAll()
    {
        $documentHandler = $this->documentHandler;

        $document = Document::createFromArray(array('someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue'));
        $documentHandler->add($this->collection->getId(), $document);

        $result = $document->getAll();

        $this->assertTrue(true === ($result['someAttribute'] == 'someValue'));
        $this->assertTrue(true === ($result['someOtherAttribute'] == 'someOtherValue'));
        
        $result = $document->getAll(true);
        $this->assertArrayHasKey('_id', $result);
        $this->assertArrayHasKey('_rev', $result);
    }
{'fraction_non_alphanumeric': 0.14937728391378924, 'fraction_numerical': 0.002461033634126333, 'mean_word_length': 3.6920923722883137, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 214, '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': '6791073', 'n_tokens_mistral': 3445, 'n_tokens_neox': 3423, 'n_words': 726}