<commit_before><?php

namespace Sokil\Mongo;

class DocumentMock extends \Sokil\Mongo\Document
{
    protected $_data = array(
        'status' => 'ACTIVE',
        'profile' => array(
            'name' => 'USER_NAME',
            'birth' => array(
                'year' => 1984,
                'month' => 8,
                'day' => 10,
            )
        ),
        'interests' => 'none',
        'languages' => array('php'),
    );
}

class DocumentTest extends \PHPUnit_Framework_TestCase
{
    /**
     *
     * @var \Sokil\Mongo\Collection
     */
    private $collection;

    public function setUp()
    {
        $client = new Client();
        $database = $client->getDatabase('test');
        $this->collection = $database
            ->getCollection('phpmongo_test_collection')
            ->delete();
    }

    public function tearDown()
    {
        $this->collection->delete();
    }

    public function testReset()
    {
        $document = $this->collection
            ->createDocument(array(
                'param1'    => 'value1'
            ))
            ->save();

        $document->param1 = 'changedValue';

        $this->assertEquals('changedValue', $document->get('param1'));

        $document->reset();

        $this->assertEquals('value1', $document->get('param1'));
    }

    public function testReload()
    {
        // Create document
        $id = $this->collection
            ->createDocument(array('param' => 'value'))
            ->save()
            ->getId();

        // Load two unreferenced copies of same document
        $document1 = $this->collection->getDocumentDirectly($id);
        $document2 = $this->collection->getDocumentDirectly($id);

        // store changes on one of them
        $document1
            ->set('param', 'updatedValue')
            ->save();

        // Changes not exists in seconde document before refresh
        $this->assertEquals('value', $document2->param);

        // Document 2 is in not-saved state.
        // After refresh all not saved changes reset
        $document2
            ->set('param', 'someParalelUpdatedButNotSavedValue')
            ->increment('newField');

        // refresh data
        $document2->refresh();

        // now data is fresh
        $this->assertEquals('updatedValue', $document2->param);
        $this->assertEquals(array(), $document2->getOperator()->getAll());
        $this->assertNull($document2->newField);
    }

    public function testToString()
    {
        $document = $this->collection->createDocument(array(
            'param1'    => 'value1'
        ));

        $this->collection->saveDocument($document);

        $this->assertEquals((string) $document, $document->getId());
    }

    public function testVirtualGetter()
    {
        $document = $this->collection->createDocument(array(
            'param' => 'value',
        ));

        $this->assertEquals('value', $document->getParam());

        $this->assertEquals(null, $document->getUnexistedParam());
    }

    public function testVirtualSetter()
    {
        $document = $this->collection->createDocument(array(
            'param' => 'value',
        ));

        $document->setParam('newValue');

        $this->assertEquals('newValue', $document->get('param'));
    }

    /**
     * Test call of method not described in behaviors, not setter and not getter
     * @expectedException \Exception
     * @expectedExceptionMessage Document has no method "unexistedMethod"
     */
    public function testUnhandledMethodCall()
    {
        $document = $this->collection->createDocument(array(
            'param' => 'value',
        ));

        $document->unexistedMethod();
    }

    public function testCreateDocumentFromArray()
    {
        $document = $this->collection->createDocument(array(
            'param1'    => 'value1',
            'param2'    => array(
                'param21'   => 'value21',
                'param22'   => 'value22',
            )
        ));

        $this->assertEquals('value1', $document->get('param1'));
        $this->assertEquals('value22', $document->get('param2.param22'));
    }

    public function testDefineId_AsMongoIdClass()
    {
        $id = new \MongoId();
        $document = $this->collection->createDocument()->defineId($id);
        $this->assertEquals($id, $document->getId());
    }

    public function testDefineId_AsStringThatCanBeMongoIdClass()
    {
        $id = '541073e62de5725a2a8b4567';
        $document = $this->collection->createDocument()->defineId($id);
        $this->assertEquals($id, (string) $document->getId());
    }

    public function testDefineId_AsVarchar()
    {
        $id = 'i_am_id';
        $document = $this->collection->createDocument()->defineId($id);
        $this->assertEquals($id, $document->getId());
    }

    public function testSetId_AsMongoIdClass()
    {
        // save document
        $id = new \MongoId();

        $doc = $this->collection->createDocument(array('a' => 'a'));
        $doc->setId($id);
        $this->collection->saveDocument($doc);

        // find document
        $this->assertNotEmpty($this->collection->getDocument($id));

        // delete document
        $this->collection->deleteDocument($doc);
    }

    public function testSetId_AsStringThatCanBeMongoIdClass()
    {
        // save document
        $id = '541073e62de5725a2a8b4567';

        $doc = $this->collection->createDocument(array('a' => 'a'));
        $doc->setId($id);
        $this->collection->saveDocument($doc);

        // find document
        $this->assertNotEmpty($this->collection->getDocument($id));

        // delete document
        $this->collection->deleteDocument($doc);
    }

    public function testSetId_AsVarchar()
    {
        // save document
        $id = 'im_a_key';

        $doc = $this->collection->createDocument(array('a' => 'a'));
        $doc->setId($id);
        $this->collection->saveDocument($doc);

        // find document
        $this->assertNotEmpty($this->collection->getDocument($id));

        // delete document
        $this->collection->deleteDocument($doc);
    }

    public function testIsStored()
    {
        // not stored
        $document = $this->collection->createDocument(array('k' => 'v'));
        $this->assertFalse($document->isStored());

        // stored
        $this->collection->saveDocument($document);
        $this->assertTrue($document->isStored());
    }

    public function testIsStoredWhenIdSet()
    {
        // not stored
        $document = $this->collection->createDocument(array('k' => 'v'));
        $document->setId(new \MongoId);
        $this->assertFalse($document->isStored());

        // stored
        $this->collection->saveDocument($document);
        $this->assertTrue($document->isStored());
    }

    public function testSet_Unsaved()
    {
        $document = $this->collection
            ->createDocument(array(
                'param' => 'value',
            ));

        $document->set('a.b.c', 'value1');
        $this->assertEquals('value1', $document->get('a.b.c'));
        $this->assertEquals(array('c' => 'value1'), $document->get('a.b'));
        $this->assertEquals(array('b' => array('c' => 'value1')), $document->get('a'));

        $document->set('a.b.c', 'value2');
        $this->assertEquals('value2', $document->get('a.b.c'));
        $this->assertEquals(array('c' => 'value2'), $document->get('a.b'));
        $this->assertEquals(array('b' => array('c' => 'value2')), $document->get('a'));
    }

    public function testSet_Saved()
    {
        $document = $this->collection
            ->createDocument(array(
                'param' => 'value',
            ))
            ->save();

        $document = $this->collection->getDocumentDirectly($document->getId());

        $document->set('a.b.c', 'value1');
        $this->assertEquals('value1', $document->get('a.b.c'));
        $this->assertEquals(array('c' => 'value1'), $document->get('a.b'));
        $this->assertEquals(array('b' => array('c' => 'value1')), $document->get('a'));

        $document->set('a.b.c', 'value2');
        $this->assertEquals('value2', $document->get('a.b.c'));
        $this->assertEquals(array('c' => 'value2'), $document->get('a.b'));
        $this->assertEquals(array('b' => array('c' => 'value2')), $document->get('a'));
    }

    public function testSetStructure_NewDocument()
    {
        $obj = new Structure;
        $obj->param = 'value';

        // save
        $document = $this->collection->createDocument()
            ->set('d', $obj)
            ->save();

        $this->assertEquals(
            array('param' => 'value'),
            $document->d
        );

        $this->assertEquals(
            array('param' => 'value'),
            $this->collection->getDocumentDirectly($document->getId())->d
        );
    }

    public function testSetStructure_StoredDocument()
    {
        $document = $this->collection->createDocument([
            'some' => 'field',
        ])->save();

        $obj = new Structure;
        $obj->param = 'value';

        // save
        $document->set('d', $obj)->save();

        $this->assertEquals(
            array('param' => 'value'),
            $document->d
        );<commit_msg>fix test for compartibility with php 5.3<commit_after><?php

namespace Sokil\Mongo;

class DocumentMock extends \Sokil\Mongo\Document
{
    protected $_data = array(
        'status' => 'ACTIVE',
        'profile' => array(
            'name' => 'USER_NAME',
            'birth' => array(
                'year' => 1984,
                'month' => 8,
                'day' => 10,
            )
        ),
        'interests' => 'none',
        'languages' => array('php'),
    );
}

class DocumentTest extends \PHPUnit_Framework_TestCase
{
    /**
     *
     * @var \Sokil\Mongo\Collection
     */
    private $collection;

    public function setUp()
    {
        $client = new Client();
        $database = $client->getDatabase('test');
        $this->collection = $database
            ->getCollection('phpmongo_test_collection')
            ->delete();
    }

    public function tearDown()
    {
        $this->collection->delete();
    }

    public function testReset()
    {
        $document = $this->collection
            ->createDocument(array(
                'param1'    => 'value1'
            ))
            ->save();

        $document->param1 = 'changedValue';

        $this->assertEquals('changedValue', $document->get('param1'));

        $document->reset();

        $this->assertEquals('value1', $document->get('param1'));
    }

    public function testReload()
    {
        // Create document
        $id = $this->collection
            ->createDocument(array('param' => 'value'))
            ->save()
            ->getId();

        // Load two unreferenced copies of same document
        $document1 = $this->collection->getDocumentDirectly($id);
        $document2 = $this->collection->getDocumentDirectly($id);

        // store changes on one of them
        $document1
            ->set('param', 'updatedValue')
            ->save();

        // Changes not exists in seconde document before refresh
        $this->assertEquals('value', $document2->param);

        // Document 2 is in not-saved state.
        // After refresh all not saved changes reset
        $document2
            ->set('param', 'someParalelUpdatedButNotSavedValue')
            ->increment('newField');

        // refresh data
        $document2->refresh();

        // now data is fresh
        $this->assertEquals('updatedValue', $document2->param);
        $this->assertEquals(array(), $document2->getOperator()->getAll());
        $this->assertNull($document2->newField);
    }

    public function testToString()
    {
        $document = $this->collection->createDocument(array(
            'param1'    => 'value1'
        ));

        $this->collection->saveDocument($document);

        $this->assertEquals((string) $document, $document->getId());
    }

    public function testVirtualGetter()
    {
        $document = $this->collection->createDocument(array(
            'param' => 'value',
        ));

        $this->assertEquals('value', $document->getParam());

        $this->assertEquals(null, $document->getUnexistedParam());
    }

    public function testVirtualSetter()
    {
        $document = $this->collection->createDocument(array(
            'param' => 'value',
        ));

        $document->setParam('newValue');

        $this->assertEquals('newValue', $document->get('param'));
    }

    /**
     * Test call of method not described in behaviors, not setter and not getter
     * @expectedException \Exception
     * @expectedExceptionMessage Document has no method "unexistedMethod"
     */
    public function testUnhandledMethodCall()
    {
        $document = $this->collection->createDocument(array(
            'param' => 'value',
        ));

        $document->unexistedMethod();
    }

    public function testCreateDocumentFromArray()
    {
        $document = $this->collection->createDocument(array(
            'param1'    => 'value1',
            'param2'    => array(
                'param21'   => 'value21',
                'param22'   => 'value22',
            )
        ));

        $this->assertEquals('value1', $document->get('param1'));
        $this->assertEquals('value22', $document->get('param2.param22'));
    }

    public function testDefineId_AsMongoIdClass()
    {
        $id = new \MongoId();
        $document = $this->collection->createDocument()->defineId($id);
        $this->assertEquals($id, $document->getId());
    }

    public function testDefineId_AsStringThatCanBeMongoIdClass()
    {
        $id = '541073e62de5725a2a8b4567';
        $document = $this->collection->createDocument()->defineId($id);
        $this->assertEquals($id, (string) $document->getId());
    }

    public function testDefineId_AsVarchar()
    {
        $id = 'i_am_id';
        $document = $this->collection->createDocument()->defineId($id);
        $this->assertEquals($id, $document->getId());
    }

    public function testSetId_AsMongoIdClass()
    {
        // save document
        $id = new \MongoId();

        $doc = $this->collection->createDocument(array('a' => 'a'));
        $doc->setId($id);
        $this->collection->saveDocument($doc);

        // find document
        $this->assertNotEmpty($this->collection->getDocument($id));

        // delete document
        $this->collection->deleteDocument($doc);
    }

    public function testSetId_AsStringThatCanBeMongoIdClass()
    {
        // save document
        $id = '541073e62de5725a2a8b4567';

        $doc = $this->collection->createDocument(array('a' => 'a'));
        $doc->setId($id);
        $this->collection->saveDocument($doc);

        // find document
        $this->assertNotEmpty($this->collection->getDocument($id));

        // delete document
        $this->collection->deleteDocument($doc);
    }

    public function testSetId_AsVarchar()
    {
        // save document
        $id = 'im_a_key';

        $doc = $this->collection->createDocument(array('a' => 'a'));
        $doc->setId($id);
        $this->collection->saveDocument($doc);

        // find document
        $this->assertNotEmpty($this->collection->getDocument($id));

        // delete document
        $this->collection->deleteDocument($doc);
    }

    public function testIsStored()
    {
        // not stored
        $document = $this->collection->createDocument(array('k' => 'v'));
        $this->assertFalse($document->isStored());

        // stored
        $this->collection->saveDocument($document);
        $this->assertTrue($document->isStored());
    }

    public function testIsStoredWhenIdSet()
    {
        // not stored
        $document = $this->collection->createDocument(array('k' => 'v'));
        $document->setId(new \MongoId);
        $this->assertFalse($document->isStored());

        // stored
        $this->collection->saveDocument($document);
        $this->assertTrue($document->isStored());
    }

    public function testSet_Unsaved()
    {
        $document = $this->collection
            ->createDocument(array(
                'param' => 'value',
            ));

        $document->set('a.b.c', 'value1');
        $this->assertEquals('value1', $document->get('a.b.c'));
        $this->assertEquals(array('c' => 'value1'), $document->get('a.b'));
        $this->assertEquals(array('b' => array('c' => 'value1')), $document->get('a'));

        $document->set('a.b.c', 'value2');
        $this->assertEquals('value2', $document->get('a.b.c'));
        $this->assertEquals(array('c' => 'value2'), $document->get('a.b'));
        $this->assertEquals(array('b' => array('c' => 'value2')), $document->get('a'));
    }

    public function testSet_Saved()
    {
        $document = $this->collection
            ->createDocument(array(
                'param' => 'value',
            ))
            ->save();

        $document = $this->collection->getDocumentDirectly($document->getId());

        $document->set('a.b.c', 'value1');
        $this->assertEquals('value1', $document->get('a.b.c'));
        $this->assertEquals(array('c' => 'value1'), $document->get('a.b'));
        $this->assertEquals(array('b' => array('c' => 'value1')), $document->get('a'));

        $document->set('a.b.c', 'value2');
        $this->assertEquals('value2', $document->get('a.b.c'));
        $this->assertEquals(array('c' => 'value2'), $document->get('a.b'));
        $this->assertEquals(array('b' => array('c' => 'value2')), $document->get('a'));
    }

    public function testSetStructure_NewDocument()
    {
        $obj = new Structure;
        $obj->param = 'value';

        // save
        $document = $this->collection->createDocument()
            ->set('d', $obj)
            ->save();

        $this->assertEquals(
            array('param' => 'value'),
            $document->d
        );

        $this->assertEquals(
            array('param' => 'value'),
            $this->collection->getDocumentDirectly($document->getId())->d
        );
    }

    public function testSetStructure_StoredDocument()
    {
        $document = $this->collection->createDocument(array(
            'some' => 'field',
        ))->save();

        $obj = new Structure;
        $obj->param = 'value';

        // save
        $document->set('d', $obj)->save();

        $this->assertEquals(
            array('param' => 'value'),
            $document->d
        );
{'fraction_non_alphanumeric': 0.17540930633347696, 'fraction_numerical': 0.01034037052994399, 'mean_word_length': 2.208189357290947, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 449, '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': '4589364', 'n_tokens_mistral': 5373, 'n_tokens_neox': 5188, 'n_words': 870}