<commit_before><?php
use LazyRecord\SqlBuilder;

class AuthorFactory {

    static function create($name) {
        $author = new \tests\Author;
        $author->create(array(
            'name' => $name,
            'email' => 'temp<EMAIL>' . rand(),
            'identity' => rand(),
            'confirmed' => true,
        ));
        return $author;
    }

}

class Collection2Test extends PHPUnit_Framework_ModelTestCase
{

    public function getModels()
    {
        return array( 
            'tests\AuthorSchema', 
            'tests\BookSchema',
            'tests\AuthorBookSchema',
            'tests\NameSchema',
            'tests\AddressSchema',
        );
    }

    public function testLazyAttributes()
    {
        $authors = new \tests\AuthorCollection;
        ok( $authors->_query , 'has lazy attribute' );
    }

    public function testClone()
    {
        $authors = new \tests\AuthorCollection;
        $clone = clone $authors;
        ok( $clone !== $authors );
    }

    public function testCloneWithQuery() 
    {
        ok( $ret = \tests\Address::delete()->execute() );
        ok( $ret->success , $ret->message );

        $a = new \tests\Address;
        ok( $a->create(array('address' => 'Cindy'))->success );
        ok( $a->create(array('address' => 'Hack'))->success );

        $addresses = new \tests\AddressCollection;
        $addresses->where()
            ->equal('address','Cindy');
        $addresses->fetch();
        is(1,$addresses->size());

        $sql1 = $addresses->toSQL();
        
        $clone = clone $addresses;
        $sql2 = $clone->toSQL();

        is($sql1, $sql2);
        is(1,$clone->size());

        $clone->free();
        $clone->where()
            ->equal('address','Hack');
        is(0,$clone->size());
    }

    public function testIterator()
    {
        $authors = new \tests\AuthorCollection;
        ok( $authors );
        foreach( $authors as $a ) {
            ok( $a->id );
        }
    }

    public function testCollection()
    {
        $author = new \tests\Author;
        ok($author);
        foreach( range(1,3) as $i ) {
            $ret = $author->create(array(
                'name' => 'Bar-' . $i,
                'email' => 'bar@bar' . $i,
                'identity' => 'bar' . $i,
                'confirmed' => $i % 2 ? true : false,
            ));
            $this->resultOK( true, $ret );
        }

        foreach( range(1,20) as $i ) {
            $ret = $author->create(array(
                'name' => 'Foo-' . $i,
                'email' => 'foo@foo' . $i,
                'identity' => 'foo' . $i,
                'confirmed' => $i % 2 ? true : false,
            ));
            $this->resultOK( true, $ret );
        }

        $authors2 = new \tests\AuthorCollection;
        $authors2->where()
                ->like('name','Foo%');
        $count = $authors2->queryCount();
        is( 20 , $count );

        $authors = new \tests\AuthorCollection;
        $authors->where()
                ->like('name','Foo%');
        $items = $authors->items();
        $size = $authors->size();

        ok( $size );
        is( 20, $size );
        ok( $items );
        ok( is_array( $items ));
        foreach( $items as $item ) {
            ok( $item->id );
            isa_ok( '\tests\Author', $item );
            $ret = $item->delete();
            ok( $ret->success );
        }
        $size = $authors->free()->size();
        is( 0, $size );

        {
            $authors = new \tests\AuthorCollection;
            foreach( $authors as $a ) {
                $a->delete();
            }
        }
    }


    function testBooleanType()
    {
        $name = new \tests\Name;
        $ret = $name->create(array( 
            'name' => 'Foo',
            'confirmed' => false,
            'country' => 'Tokyo',
        ));
        ok( $ret->success , $ret );
        is( false, $name->confirmed );

        $ret = $name->load( array( 'name' => 'Foo' ));
        ok( $ret->success , $ret );
        is( false, $name->confirmed );

        $name->update(array( 'confirmed' => true ) );
        is( true, $name->confirmed );

        $name->update(array( 'confirmed' => false ) );
        is( false, $name->confirmed );

        $name->delete();

        ok( $name->create(array( 'name' => 'Foo', 'address' => 'Addr1', 'country' => 'Taiwan' ))->success );
        ok( $name->create(array( 'name' => 'Foo', 'address' => 'Addr1', 'country' => 'Taiwan' ))->success );
        ok( $name->create(array( 'name' => 'Foo', 'address' => 'Addr1', 'country' => 'Taiwan' ))->success );
        ok( $name->create(array( 'name' => 'Foo', 'address' => 'Addr1', 'country' => 'Taiwan' ))->success );
        ok( $name->create(array( 'name' => 'Foo', 'address' => 'Addr1', 'country' => 'Taiwan' ))->success );

        $names = new \tests\NameCollection;
        $names->select( 'name' )->where()
            ->equal('name','Foo')
            ->groupBy('name','address');
        

        ok( $items = $names->items() , 'Test name collection with name,address condition' );
        ok( $size = $names->size() );
        is( 1 , $size );
        is( 'Foo', $items[0]->name );
    }


    function testJoin()
    {
        $authors = new \tests\AuthorCollection;
        ok($authors);

        $address = new \tests\Address;
        $authors->join($address);

        $authors->fetch();
        $sql = $authors->toSQL();

        like( '/addresses.address\s+AS\s+addresses_address/', $sql );
    }

    function testJoinWithAliasAndRelationId() {
        $author = AuthorFactory::create('John');
        ok($author->id);

        $author->addresses[] = array( 'address' => 'Address I' );
        $author->addresses[] = array( 'address' => 'Address II' );

        $authors = new \tests\AuthorCollection;
        ok($authors);
        $authors->join( new \tests\Address ,'LEFT','a', 'addresses');
        $authors->fetch();
        $sql = $authors->toSQL();
        ok($sql);

        $size = $authors->size();
        is(2,$size);
        foreach( $authors as $a ) {
            ok($a->a_address);
            ok($a->a_id);
        }
    }
<commit_msg>Clean up<commit_after><?php
use LazyRecord\SqlBuilder;

class AuthorFactory {

    static function create($name) {
        $author = new \tests\Author;
        $author->create(array(
            'name' => $name,
            'email' => 'temp@temp' . rand(),
            'identity' => rand(),
            'confirmed' => true,
        ));
        return $author;
    }

}

class Collection2Test extends PHPUnit_Framework_ModelTestCase
{

    public function getModels()
    {
        return array( 
            'tests\AuthorSchema', 
            'tests\BookSchema',
            'tests\AuthorBookSchema',
            'tests\NameSchema',
            'tests\AddressSchema',
        );
    }

    public function testLazyAttributes()
    {
        $authors = new \tests\AuthorCollection;
        ok( $authors->_query , 'has lazy attribute' );
    }

    public function testClone()
    {
        $authors = new \tests\AuthorCollection;
        $clone = clone $authors;
        ok( $clone !== $authors );
    }

    public function testCloneWithQuery() 
    {
        ok( $ret = \tests\Address::delete()->execute() );
        ok( $ret->success , $ret->message );

        $a = new \tests\Address;
        ok( $a->create(array('address' => 'Cindy'))->success );
        ok( $a->create(array('address' => 'Hack'))->success );

        $addresses = new \tests\AddressCollection;
        $addresses->where()
            ->equal('address','Cindy');
        $addresses->fetch();
        is(1,$addresses->size());

        $sql1 = $addresses->toSQL();
        
        $clone = clone $addresses;
        $sql2 = $clone->toSQL();

        is($sql1, $sql2);
        is(1,$clone->size());

        $clone->free();
        $clone->where()
            ->equal('address','Hack');
        is(0,$clone->size());
    }

    public function testIterator()
    {
        $authors = new \tests\AuthorCollection;
        ok( $authors );
        foreach( $authors as $a ) {
            ok( $a->id );
        }
    }

    public function testCollection()
    {
        $author = new \tests\Author;
        ok($author);
        foreach( range(1,3) as $i ) {
            $ret = $author->create(array(
                'name' => 'Bar-' . $i,
                'email' => 'bar@<EMAIL>' . $i,
                'identity' => 'bar' . $i,
                'confirmed' => $i % 2 ? true : false,
            ));
            $this->resultOK( true, $ret );
        }

        foreach( range(1,20) as $i ) {
            $ret = $author->create(array(
                'name' => 'Foo-' . $i,
                'email' => 'foo@foo' . $i,
                'identity' => 'foo' . $i,
                'confirmed' => $i % 2 ? true : false,
            ));
            $this->resultOK( true, $ret );
        }

        $authors2 = new \tests\AuthorCollection;
        $authors2->where()
                ->like('name','Foo%');
        $count = $authors2->queryCount();
        is( 20 , $count );

        $authors = new \tests\AuthorCollection;
        $authors->where()
                ->like('name','Foo%');
        $items = $authors->items();
        $size = $authors->size();

        ok( $size );
        is( 20, $size );
        ok( $items );
        ok( is_array( $items ));
        foreach( $items as $item ) {
            ok( $item->id );
            isa_ok( '\tests\Author', $item );
            $ret = $item->delete();
            ok( $ret->success );
        }
        $size = $authors->free()->size();
        is( 0, $size );

        {
            $authors = new \tests\AuthorCollection;
            foreach( $authors as $a ) {
                $a->delete();
            }
        }
    }


    function testBooleanType()
    {
        $name = new \tests\Name;
        $ret = $name->create(array( 
            'name' => 'Foo',
            'confirmed' => false,
            'country' => 'Tokyo',
        ));
        ok( $ret->success , $ret );
        is( false, $name->confirmed );

        $ret = $name->load( array( 'name' => 'Foo' ));
        ok( $ret->success , $ret );
        is( false, $name->confirmed );

        $name->update(array( 'confirmed' => true ) );
        is( true, $name->confirmed );

        $name->update(array( 'confirmed' => false ) );
        is( false, $name->confirmed );

        $name->delete();

        ok( $name->create(array( 'name' => 'Foo', 'address' => 'Addr1', 'country' => 'Taiwan' ))->success );
        ok( $name->create(array( 'name' => 'Foo', 'address' => 'Addr1', 'country' => 'Taiwan' ))->success );
        ok( $name->create(array( 'name' => 'Foo', 'address' => 'Addr1', 'country' => 'Taiwan' ))->success );
        ok( $name->create(array( 'name' => 'Foo', 'address' => 'Addr1', 'country' => 'Taiwan' ))->success );
        ok( $name->create(array( 'name' => 'Foo', 'address' => 'Addr1', 'country' => 'Taiwan' ))->success );

        $names = new \tests\NameCollection;
        $names->select( 'name' )->where()
            ->equal('name','Foo')
            ->groupBy('name','address');
        

        ok( $items = $names->items() , 'Test name collection with name,address condition' );
        ok( $size = $names->size() );
        is( 1 , $size );
        is( 'Foo', $items[0]->name );
    }


    function testJoin()
    {
        $authors = new \tests\AuthorCollection;
        ok($authors);

        $authors->join( new \tests\Address );

        $authors->fetch();
        $sql = $authors->toSQL();

        like( '/addresses.address\s+AS\s+addresses_address/', $sql );
    }

    function testJoinWithAliasAndRelationId() {
        $author = AuthorFactory::create('John');
        ok($author->id);

        $author->addresses[] = array( 'address' => 'Address I' );
        $author->addresses[] = array( 'address' => 'Address II' );

        $authors = new \tests\AuthorCollection;
        ok($authors);
        $authors->join( new \tests\Address ,'LEFT','a', 'addresses');
        $authors->fetch();
        $sql = $authors->toSQL();
        ok($sql);

        $size = $authors->size();
        is(2,$size);
        foreach( $authors as $a ) {
            ok($a->a_address);
            ok($a->a_id);
        }
    }

{'fraction_non_alphanumeric': 0.1891385767790262, 'fraction_numerical': 0.0050480377788633774, 'mean_word_length': 1.7374637842656564, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 235, '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': '2253262', 'n_tokens_mistral': 3900, 'n_tokens_neox': 3673, 'n_words': 776}