Skip to content

Commit

Permalink
test case on where queries
Browse files Browse the repository at this point in the history
  • Loading branch information
sunel committed Sep 2, 2018
1 parent ed56fdc commit c853767
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 2 deletions.
9 changes: 9 additions & 0 deletions tests/Feature/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@

class Cars extends \Eav\Model {
const ENTITY = 'car';

/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'purchased_at'
];
}

abstract class TestCase extends Testbench
Expand Down
161 changes: 159 additions & 2 deletions tests/Feature/WhereQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,169 @@ public function it_can_add_where_null_statement()
$this->assertEquals($product->first()->sku, 'PDO1HJK92');
}

/** @test */
public function it_can_add_or_where_null_statement()
{
$eloquent = $this->product();
$eloquent2 = $this->product_2();

$product = Cars::whereAttribute('sku', 'UNKNOWN')
->orWhereNullAttribute('description')
->get(['*', 'description', 'sku']);

$this->assertTrue($product->isNotEmpty());
$this->assertEquals($product->count(), 1);
$this->assertEquals($product->first()->sku, 'PDO1HJK92');
}

/** @test */
public function it_can_add_where_not_null_statement()
{
$eloquent = $this->product();
$eloquent2 = $this->product_2();

$product = Cars::whereNotNullAttribute('description')
->get(['*', 'description', 'sku']);

$this->assertTrue($product->isNotEmpty());
$this->assertEquals($product->count(), 1);
$this->assertEquals($product->first()->sku, 'PDOBEEAM112');
}

/** @test */
public function it_can_add_or_where_not_null_statement()
{
$eloquent = $this->product();
$eloquent2 = $this->product_2();

$product = Cars::whereAttribute('sku', 'UNKNOWN')
->orWhereNotNullAttribute('description')
->get(['*', 'description', 'sku']);

$this->assertTrue($product->isNotEmpty());
$this->assertEquals($product->count(), 1);
$this->assertEquals($product->first()->sku, 'PDOBEEAM112');
}

/** @test */
public function it_can_add_where_date_statement()
{
$eloquent = $this->product();
$eloquent2 = $this->product_2();

$product = Cars::whereDateAttribute('purchased_at', '2018-09-02')
->get(['*', 'sku']);

$this->assertTrue($product->isNotEmpty());
$this->assertEquals($product->count(), 1);
$this->assertEquals($product->first()->sku, 'PDO1HJK92');
}

/** @test */
public function it_can_add_or_where_date_statement()
{
$eloquent = $this->product();
$eloquent2 = $this->product_2();

$product = Cars::whereDateAttribute('purchased_at', '2018-09-01')
->orWhereDateAttribute('purchased_at', '=' , '2018-09-02')
->get(['*', 'sku']);

$this->assertTrue($product->isNotEmpty());
$this->assertEquals($product->count(), 1);
$this->assertEquals($product->first()->sku, 'PDO1HJK92');
}

/** @test */
public function it_can_add_where_time_statement()
{
$eloquent = $this->product();
$eloquent2 = $this->product_2();

$product = Cars::whereTimeAttribute('purchased_at', '=', '15:02:01')
->get(['*', 'sku']);

$this->assertTrue($product->isNotEmpty());
$this->assertEquals($product->count(), 1);
$this->assertEquals($product->first()->sku, 'PDO1HJK92');
}

/** @test */
public function it_can_add_or_where_time_statement()
{
$eloquent = $this->product();
$eloquent2 = $this->product_2();

$product = Cars::whereTimeAttribute('purchased_at', '=', '15:04:01')
->orwhereTimeAttribute('purchased_at', '=', '15:03:01')
->get(['*', 'sku']);

$this->assertTrue($product->isNotEmpty());
$this->assertEquals($product->count(), 1);
$this->assertEquals($product->first()->sku, 'PDOBEEAM112');
}

/** @test */
public function it_can_add_where_day_statement()
{
$eloquent = $this->product();
$eloquent2 = $this->product_2();

$product = Cars::whereDayAttribute('purchased_at', '02')
->get(['*', 'sku']);

$this->assertTrue($product->isNotEmpty());
$this->assertEquals($product->count(), 1);
$this->assertEquals($product->first()->sku, 'PDO1HJK92');
}

/** @test */
public function it_can_add_where_month_statement()
{
$eloquent = $this->product();
$eloquent2 = $this->product_2();

$product = Cars::whereMonthAttribute('purchased_at', '09')
->get(['*', 'sku']);

$this->assertTrue($product->isNotEmpty());
$this->assertEquals($product->count(), 1);
$this->assertEquals($product->first()->sku, 'PDO1HJK92');
}

/** @test */
public function it_can_add_where_year_statement()
{
$eloquent = $this->product();
$eloquent2 = $this->product_2();

$product = Cars::whereYearAttribute('purchased_at', '2018')
->get(['*', 'sku']);

$this->assertTrue($product->isNotEmpty());
$this->assertEquals($product->count(), 2);
}

/** @test */
public function it_can_add_order_by_statement()
{
$eloquent = $this->product();
$eloquent2 = $this->product_2();

$product = Cars::orderByAttribute('purchased_at', 'asc')
->get(['*', 'sku']);

$this->assertEquals($product->first()->sku, 'PDOBEEAM112');
}

private function product()
{
return Cars::create([
'name' => 'Flamethrower',
'sku' => 'PDO1HJK92',
'age' => rand(50,100),
'search' => 1
'search' => 1,
'purchased_at' => new \DateTime('2018-09-02T15:02:01.012345Z')
]);
}

Expand All @@ -193,7 +349,8 @@ private function product_2()
'sku' => 'PDOBEEAM112',
'description' => 'Definitely Not a Flamethrower',
'age' => 14,
'search' => 0
'search' => 0,
'purchased_at' => new \DateTime('2018-08-21T15:03:01.012345Z')
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ public function up()
'is_required' => 0,
'required_validate_class' => NULL
]);

Attribute::add([
'attribute_code' => 'purchased_at',
'entity_code' => 'car',
'backend_class' => NULL,
'backend_type' => 'timestamp',
'backend_table' => NULL,
'frontend_class' => NULL,
'frontend_type' => 'text',
'frontend_label' => ucwords(str_replace('_',' ','purchased_at')),
'source_class' => NULL,
'default_value' => '',
'is_required' => 0,
'required_validate_class' => NULL
]);

}

Expand Down

0 comments on commit c853767

Please sign in to comment.