<commit_before><?php

namespace Mango\Tests;

use Mango\Mango;
use Mango\DocumentManager;
use Mango\Tests\Document\User;

class DocumentTest extends \PHPUnit_Framework_TestCase
{
    private function getConnection()
    {
        $mango = new Mango('mongodb://localhost:27017/mango-unittests');
        $dm = new DocumentManager($mango);

        return $mango;
    }

    public function tearDown()
    {
        User::where()->each(function($document) {
            $document->remove();
        });
    }

    public function testFind()
    {
        $this->getConnection();
        $ids = [];

        for ($i = 0; $i <= 3; $i++) {
            $user = new User();
            $user->name = "Test {$i}";
            $user->store();
            $ids[] = (string)$user->_id;
        }

        $result = call_user_func_array(['\Mango\Tests\Document\User', 'find'], $ids);

        self::assertEquals(4, $result->count());
    }

    public function testStore()
    {
        $mango = $this->getConnection();
        $document = new User();
        $document->name = '<NAME>';
        $document->store();
        $query = ['name' => '<NAME>'];
        self::assertEquals(1, $document::where($query)->count());

        $document->remove();
        self::assertEquals(0, $document::where($query)->count());
    }

    public function testQuery()
    {
        $mango = $this->getConnection();
        $document = new User();
        $document->name = '<NAME>';
        $document->store();
        $query = ['name' => '<NAME>'];

        $user = $document::where($query)->first();
        self::assertEquals($document->allPrepared(), $user->allPrepared());

        $document->remove();
        self::assertEquals(0, $document::where($query)->count());
    }

    public function testHydration()
    {
        $mango = $this->getConnection();
        $document = new User();
        $document->name = '<NAME>';
        $document->store();
        $query = ['name' => '<NAME>'];

        $user = $document::where($query)->first();
        $user->updated_at = new \DateTime('+4 hours');
        $user->name = '<NAME>';
        $user->store();
        self::assertEquals(0, $document::where($query)->count());
        self::assertEquals(1, $document::where(['name' => 'Foo Bar 2'])->count());

        $result = $document::where(['name' => 'Foo Bar 2'])->first();
        self::assertInstanceOf('\DateTime', $result->updated_at);

        $document->remove();
        self::assertEquals(0, $document::find($user->getId())->count());
    }

    public function testUpdate()
    {
        $mango = $this->getConnection();
        $document = new User();
        $document->update(['name' => 'Updater']);

        self::assertEquals('Updater', $document->name);
        $document->store();

        self::assertEquals(1, $document::where(['name' => 'Updater'])->count());

        $document->remove();
        self::assertEquals(0, $document::find($document->getId())->count());
    }

    public function testDefaultValue()
    {
        $mango = $this->getConnection();
        $document = new User();
        $document->store();
        print_r($document);

        self::assertEquals('UnitTest', $document->name);
        $document->remove();
    }

}<commit_msg>removed debug code<commit_after><?php

namespace Mango\Tests;

use Mango\Mango;
use Mango\DocumentManager;
use Mango\Tests\Document\User;

class DocumentTest extends \PHPUnit_Framework_TestCase
{
    private function getConnection()
    {
        $mango = new Mango('mongodb://localhost:27017/mango-unittests');
        $dm = new DocumentManager($mango);

        return $mango;
    }

    public function tearDown()
    {
        User::where()->each(function($document) {
            $document->remove();
        });
    }

    public function testFind()
    {
        $this->getConnection();
        $ids = [];

        for ($i = 0; $i <= 3; $i++) {
            $user = new User();
            $user->name = "Test {$i}";
            $user->store();
            $ids[] = (string)$user->_id;
        }

        $result = call_user_func_array(['\Mango\Tests\Document\User', 'find'], $ids);

        self::assertEquals(4, $result->count());
    }

    public function testStore()
    {
        $mango = $this->getConnection();
        $document = new User();
        $document->name = '<NAME>';
        $document->store();
        $query = ['name' => 'Foo Bar'];
        self::assertEquals(1, $document::where($query)->count());

        $document->remove();
        self::assertEquals(0, $document::where($query)->count());
    }

    public function testQuery()
    {
        $mango = $this->getConnection();
        $document = new User();
        $document->name = '<NAME>';
        $document->store();
        $query = ['name' => 'Foo Bar'];

        $user = $document::where($query)->first();
        self::assertEquals($document->allPrepared(), $user->allPrepared());

        $document->remove();
        self::assertEquals(0, $document::where($query)->count());
    }

    public function testHydration()
    {
        $mango = $this->getConnection();
        $document = new User();
        $document->name = '<NAME>';
        $document->store();
        $query = ['name' => 'Foo Bar'];

        $user = $document::where($query)->first();
        $user->updated_at = new \DateTime('+4 hours');
        $user->name = '<NAME>';
        $user->store();
        self::assertEquals(0, $document::where($query)->count());
        self::assertEquals(1, $document::where(['name' => 'Foo Bar 2'])->count());

        $result = $document::where(['name' => 'Foo Bar 2'])->first();
        self::assertInstanceOf('\DateTime', $result->updated_at);

        $document->remove();
        self::assertEquals(0, $document::find($user->getId())->count());
    }

    public function testUpdate()
    {
        $mango = $this->getConnection();
        $document = new User();
        $document->update(['name' => 'Updater']);

        self::assertEquals('Updater', $document->name);
        $document->store();

        self::assertEquals(1, $document::where(['name' => 'Updater'])->count());

        $document->remove();
        self::assertEquals(0, $document::find($document->getId())->count());
    }

    public function testDefaultValue()
    {
        $mango = $this->getConnection();
        $document = new User();
        $document->store();

        self::assertEquals('UnitTest', $document->name);
        $document->remove();
    }

}
{'fraction_non_alphanumeric': 0.20073891625615764, 'fraction_numerical': 0.005849753694581281, 'mean_word_length': 2.509994597514857, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 122, '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': '4133105', 'n_tokens_mistral': 1894, 'n_tokens_neox': 1842, 'n_words': 338}