<?php
/**
 * BuilderTest.php file
 *
 * @author     Dmitriy Tyurin <fobia3d@gmail.com>
 * @copyright  Copyright (c) 2016 Dmitriy Tyurin
 */

namespace Fobia\Database\SphinxConnection\Test;


use Fobia\Database\SphinxConnection\SphinxConnection;

class BuilderTest extends TestCase
{
    /**
     * @var \Fobia\Database\SphinxConnection\Eloquent\Query\Builder
     */
    protected $q;

    protected $id = 9999;

    public function setUp()
    {
        parent::setUp();
        $this->setUpDatabase($this->app);

        $this->q = $this->db->table('products');
    }
    public function tearDown()
    {
        try {
            $this->db->table('products')->where('id', $this->id)->delete();
        } catch (\Exception $e) {}

        parent::tearDown(); // TODO: Change the autogenerated stub
    }

    public function test_intType()
    {
        $q = $this->db->table('products');
        $q->where('id', 1);
        $this->assertQuery("select * FROM products where id = 1", $q);
    }

    public function test_stringType()
    {
        $q = $this->db->table('products');
        $q->where('id', '1');
        $this->assertQuery("select * FROM products where id = '1'", $q);
    }

    public function test_mvaType()
    {
        $q = $this->db->table('products');
        $q->where('id', 1);
        $q->update(['tags' => '[1,2,3]']);
        $this->assertQuery("update products set tags = (1,2,3) where id = 1", $this->getQuery());
    }

    public function test_select()
    {
        $q = $this->q->select('id');
        $this->assertQuery("select id FROM products", $q);

        $q = $this->q->select('id', 'name');
        $this->assertQuery("select id, name FROM products", $q);
    }


    public function test_insert()
    {
        $str = '\'"\"\\name';
        $this->q->insert([
            'id' => $this->id,
            'name' => $str
        ]);

        $this->assertQuery("insert into products (id, name) values (9999, '\'\\\"\\\\\\\"\\\\name')", $this->getQuery());

        $m = $this->db->table('products')->find($this->id);
        $this->assertEquals($str, $m->name);
    }

    public function test_match()
    {
        $q = $this->q->match('text match');
        $this->assertQuery("select * FROM products WHERE MATCH('(@text match )')", $q);
    }


    public function test_withinGroupOrderBy()
    {
        $q = $this->q->select('id');
        $q = $q->withinGroupOrderBy('name');
        $this->assertQuery('SELECT id FROM products WITHIN GROUP ORDER BY name ASC', $q);

        $q = $q->withinGroupOrderBy('id', 'desc');
        $this->assertQuery('SELECT id FROM products WITHIN GROUP ORDER BY name ASC, id DESC', $q);
    }

    /**
     * @expectedException \RuntimeException
     */
    public function test_withinGroupOrderBy_ex()
    {
        $q = $this->q->select('id');
        $q = $q->withinGroupOrderBy('name', 'a');
    }




    public function test_scopeOptions()
    {
        $q = $this->q->options('ranker', 'bm25');
        $this->assertQuery('select * from products OPTION ranker = bm25', $q);

        $q->options('max_matches', '3000');
        $this->assertQuery('select * from products OPTION ranker = bm25,max_matches=3000', $q);

        $q->options('field_weights', '(title=10, body=3)');
        $this->assertQuery('select * from products OPTION ranker = bm25,max_matches=3000,
            field_weights=(title=10, body=3)', $q);

        $q->options('agent_query_timeout', '10000');
        $this->assertQuery('select * from products OPTION ranker = bm25,max_matches=3000,
            field_weights=(title=10, body=3) , agent_query_timeout=10000', $q);
        $q->get();
    }

    public function test_scopeOptions2()
    {
        $q = $this->q->options('field_weights', ['title' => 10, 'body' => 3]);
        $this->assertQuery('select * from products OPTION field_weights=(title=10, body=3)', $q);

        $q->options('comment', 'my comment');
        $this->assertQuery('select * from products OPTION field_weights=(title=10, body=3), comment=\'my comment\'',
            $q);
    }
}
