<commit_before><?php

use Zizaco\Mongolid\Model;
use Zizaco\Mongolid\OdmCursor;
use Zizaco\Mongolid\CachableOdmCursor;
use Mockery as m;

class CachableOdmCursorTest extends PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        $this->mongoMock = m::mock('Connection');
        $this->OdmCursor = m::mock(new _stubOdmCursor);

        $this->objA = new _stubModelForCachable;
        $this->objA->name = 'bob';

        $this->objB = new _stubModelForCachable;
        $this->objB->name = 'billy';

        $this->OdmCursor
            ->shouldReceive('toArray')
            ->with(false)
            ->andReturn([
                $this->objA,
                $this->objB
            ]);

        _stubModelForCachable::$connection = $this->mongoMock;
        _stubModelForCachable::$returnToWhere = $this->OdmCursor;
    }

    public function tearDown()
    {
        m::close();

        _stubModelForCachable::$connection = null;
    }

    public function testShouldGetCursor()
    {
        $this->OdmCursor
            ->shouldReceive('getCursor')
            ->once()
            ->andReturn('theCursor');

        $cachableOdmCursor = new CachableOdmCursor(['name'=>'bob'], '_stubModelForCachable');
        $result = $cachableOdmCursor->getCursor();

        $this->assertEquals( 'theCursor', $result );
    }

    public function testShouldCallOdmCursorMethod()
    {
        $this->OdmCursor
            ->shouldReceive('randomMethod')
            ->once()
            ->with(1,2,3)
            ->andReturn(true);

        $cachableOdmCursor = new CachableOdmCursor(['name'=>'bob'], '_stubModelForCachable');
        $result = $cachableOdmCursor->randomMethod(1,2,3);

        $this->assertTrue( $result );
    }

    public function testShouldGetCurrent()
    {
        $cachableOdmCursor = new CachableOdmCursor($this->OdmCursor, '_stubModelForCachable');
        $result = $cachableOdmCursor->current();

        $this->assertInstanceOf('_stubModelForCachable', $result);
    }

    public function testShouldConvertToArrayCurrent()
    {
        $cachableOdmCursor = new CachableOdmCursor($this->OdmCursor, '_stubModelForCachable');
        $result = $cachableOdmCursor->toArray();
        $should_be = array($this->objA->attributes,$this->objB->attributes);

        $this->assertEquals($should_be, $result);
    }

    public function testShouldGetFirst()
    {
        $cachableOdmCursor = new CachableOdmCursor($this->OdmCursor, '_stubModelForCachable');
        $result = $cachableOdmCursor->first();

        $this->assertEquals($this->objA, $result);
    }

    public function testShouldRewind()
    {
        $this->OdmCursor->shouldReceive('rewind');

        $cachableOdmCursor = new CachableOdmCursor($this->OdmCursor, '_stubModelForCachable');
        $cachableOdmCursor->rewind();
    }

    public function testGoNextAndGetKey()
    {
        $cachableOdmCursor = new CachableOdmCursor($this->OdmCursor, '_stubModelForCachable');

        $cachableOdmCursor->next();
        $cachableOdmCursor->next();
        $cachableOdmCursor->next();

        $this->assertEquals(3,$cachableOdmCursor->key());
    }

    public function testShouldSort()
    {
        $this->OdmCursor->shouldReceive('sort');

        $cachableOdmCursor = new CachableOdmCursor($this->OdmCursor, '_stubModelForCachable');
        $result = $cachableOdmCursor->sort(['name']);
    }

    public function testToJson()
    {
        $cachableOdmCursor = new CachableOdmCursor($this->OdmCursor, '_stubModelForCachable');
        $result = $cachableOdmCursor->toJson();

        $shouldBe = json_encode(array($this->objA->attributes, $this->objB->attributes));

        $this->assertEquals($shouldBe, $result);
    }
}

class _stubModelForCachable extends Model {
    protected $collection = 'test_model';

    public static $returnToWhere;

    public static function where($query = array(), $fields = array(), $cachable = false) {
        return static::$returnToWhere;
    }
}

class _stubOdmCursor {}
<commit_msg>fixing test that was broken in php 5.3 version<commit_after><?php

use Zizaco\Mongolid\Model;
use Zizaco\Mongolid\OdmCursor;
use Zizaco\Mongolid\CachableOdmCursor;
use Mockery as m;

class CachableOdmCursorTest extends PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        $this->mongoMock = m::mock('Connection');
        $this->OdmCursor = m::mock(new _stubOdmCursor);

        $this->objA = new _stubModelForCachable;
        $this->objA->name = 'bob';

        $this->objB = new _stubModelForCachable;
        $this->objB->name = 'billy';

        $this->OdmCursor
            ->shouldReceive('toArray')
            ->with(false)
            ->andReturn(array(
                $this->objA,
                $this->objB
            ));

        _stubModelForCachable::$connection = $this->mongoMock;
        _stubModelForCachable::$returnToWhere = $this->OdmCursor;
    }

    public function tearDown()
    {
        m::close();

        _stubModelForCachable::$connection = null;
    }

    public function testShouldGetCursor()
    {
        $this->OdmCursor
            ->shouldReceive('getCursor')
            ->once()
            ->andReturn('theCursor');

        $cachableOdmCursor = new CachableOdmCursor(array('name'=>'bob'), '_stubModelForCachable');
        $result = $cachableOdmCursor->getCursor();

        $this->assertEquals( 'theCursor', $result );
    }

    public function testShouldCallOdmCursorMethod()
    {
        $this->OdmCursor
            ->shouldReceive('randomMethod')
            ->once()
            ->with(1,2,3)
            ->andReturn(true);

        $cachableOdmCursor = new CachableOdmCursor(array('name'=>'bob'), '_stubModelForCachable');
        $result = $cachableOdmCursor->randomMethod(1,2,3);

        $this->assertTrue( $result );
    }

    public function testShouldGetCurrent()
    {
        $cachableOdmCursor = new CachableOdmCursor($this->OdmCursor, '_stubModelForCachable');
        $result = $cachableOdmCursor->current();

        $this->assertInstanceOf('_stubModelForCachable', $result);
    }

    public function testShouldConvertToArrayCurrent()
    {
        $cachableOdmCursor = new CachableOdmCursor($this->OdmCursor, '_stubModelForCachable');
        $result = $cachableOdmCursor->toArray();
        $should_be = array($this->objA->attributes,$this->objB->attributes);

        $this->assertEquals($should_be, $result);
    }

    public function testShouldGetFirst()
    {
        $cachableOdmCursor = new CachableOdmCursor($this->OdmCursor, '_stubModelForCachable');
        $result = $cachableOdmCursor->first();

        $this->assertEquals($this->objA, $result);
    }

    public function testShouldRewind()
    {
        $this->OdmCursor->shouldReceive('rewind');

        $cachableOdmCursor = new CachableOdmCursor($this->OdmCursor, '_stubModelForCachable');
        $cachableOdmCursor->rewind();
    }

    public function testGoNextAndGetKey()
    {
        $cachableOdmCursor = new CachableOdmCursor($this->OdmCursor, '_stubModelForCachable');

        $cachableOdmCursor->next();
        $cachableOdmCursor->next();
        $cachableOdmCursor->next();

        $this->assertEquals(3,$cachableOdmCursor->key());
    }

    public function testShouldSort()
    {
        $this->OdmCursor->shouldReceive('sort');

        $cachableOdmCursor = new CachableOdmCursor($this->OdmCursor, '_stubModelForCachable');
        $result = $cachableOdmCursor->sort(array('name'));
    }

    public function testToJson()
    {
        $cachableOdmCursor = new CachableOdmCursor($this->OdmCursor, '_stubModelForCachable');
        $result = $cachableOdmCursor->toJson();

        $shouldBe = json_encode(array($this->objA->attributes, $this->objB->attributes));

        $this->assertEquals($shouldBe, $result);
    }
}

class _stubModelForCachable extends Model {
    protected $collection = 'test_model';

    public static $returnToWhere;

    public static function where($query = array(), $fields = array(), $cachable = false) {
        return static::$returnToWhere;
    }
}

class _stubOdmCursor {}

{'fraction_non_alphanumeric': 0.13666050523721504, 'fraction_numerical': 0.001971657424522489, 'mean_word_length': 2.9513145082765337, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 135, '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': '6865008', 'n_tokens_mistral': 2578, 'n_tokens_neox': 2545, 'n_words': 374}