From c853767eb9d0cb68bc44171e6701cf1fa53fd8b7 Mon Sep 17 00:00:00 2001 From: Sunel Tr Date: Sun, 2 Sep 2018 21:49:32 +0530 Subject: [PATCH] test case on where queries --- tests/Feature/TestCase.php | 9 + tests/Feature/WhereQueryTest.php | 161 +++++++++++++++++- ...59_create_car_entity_attributes_071258.php | 15 ++ 3 files changed, 183 insertions(+), 2 deletions(-) diff --git a/tests/Feature/TestCase.php b/tests/Feature/TestCase.php index 75d40a4..7348054 100644 --- a/tests/Feature/TestCase.php +++ b/tests/Feature/TestCase.php @@ -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 diff --git a/tests/Feature/WhereQueryTest.php b/tests/Feature/WhereQueryTest.php index febf5ed..a86554d 100644 --- a/tests/Feature/WhereQueryTest.php +++ b/tests/Feature/WhereQueryTest.php @@ -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') ]); } @@ -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') ]); } } \ No newline at end of file diff --git a/tests/migrations/2018_08_08_071259_create_car_entity_attributes_071258.php b/tests/migrations/2018_08_08_071259_create_car_entity_attributes_071258.php index 178fe30..80224a2 100644 --- a/tests/migrations/2018_08_08_071259_create_car_entity_attributes_071258.php +++ b/tests/migrations/2018_08_08_071259_create_car_entity_attributes_071258.php @@ -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 + ]); }