From fc19ffa313e987a4d9b7c5ab01fb8124282434d5 Mon Sep 17 00:00:00 2001 From: Christoffer Nielsen Date: Wed, 24 Aug 2016 10:57:54 +0200 Subject: [PATCH 1/2] Add test to proove issue with numeric aggregation Refs. https://github.com/laravel/framework/pull/14793 --- .../Database/DatabaseEloquentBuilderTest.php | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index 67dc4d1fe4d7..a75cf609a797 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -4,11 +4,47 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Collection as BaseCollection; +use Illuminate\Database\Capsule\Manager as DB; class DatabaseEloquentBuilderTest extends PHPUnit_Framework_TestCase { + public function setUp() + { + $db = new DB; + + $db->addConnection([ + 'driver' => 'sqlite', + 'database' => ':memory:', + ]); + + $db->bootEloquent(); + $db->setAsGlobal(); + + $this->createSchema(); + } + + /** + * Setup the database schema. + * + * @return void + */ + public function createSchema() + { + $this->schema()->create('users', function ($table) { + $table->increments('id'); + $table->string('email')->unique(); + $table->timestamps(); + }); + } + + /** + * Tear down the database schema. + * + * @return void + */ public function tearDown() { + $this->schema()->drop('users'); m::close(); } @@ -511,6 +547,15 @@ public function testDeleteOverride() $this->assertEquals(['foo' => $builder], $builder->delete()); } + public function testAggregatedValuesOfDatetimeField() + { + EloquentBuilderTestUser::create(['id' => 1, 'email' => 'test1@test.test', 'created_at' => '2016-08-10 09:21:00']); + EloquentBuilderTestUser::create(['id' => 2, 'email' => 'test2@test.test', 'created_at' => '2016-08-01 12:00:00']); + + $this->assertEquals('2016-08-10 09:21:00', EloquentBuilderTestUser::max('created_at')); + $this->assertEquals('2016-08-01 12:00:00', EloquentBuilderTestUser::min('created_at')); + } + public function testWithCount() { $model = new EloquentBuilderTestModelParentStub; @@ -724,6 +769,26 @@ protected function getMockQueryBuilder() return $query; } + + /** + * Get a database connection instance. + * + * @return Illuminate\Database\Connection + */ + protected function connection() + { + return Illuminate\Database\Eloquent\Model::getConnectionResolver()->connection(); + } + + /** + * Get a schema builder instance. + * + * @return Illuminate\Database\Schema\Builder + */ + protected function schema() + { + return $this->connection()->getSchemaBuilder(); + } } class EloquentBuilderTestScopeStub extends Illuminate\Database\Eloquent\Model @@ -844,3 +909,10 @@ public function bazes() return $this->hasMany('EloquentBuilderTestModelFarRelatedStub', 'foreign_key', 'id', 'bar'); } } + +class EloquentBuilderTestUser extends Illuminate\Database\Eloquent\Model +{ + protected $table = 'users'; + protected $guarded = []; + public $timestamps = false; +} \ No newline at end of file From 7fcb16e692c30ad45645fecca7158dd0f50dd08c Mon Sep 17 00:00:00 2001 From: Christoffer Nielsen Date: Wed, 24 Aug 2016 11:33:03 +0200 Subject: [PATCH 2/2] Refactor according to comments --- tests/Database/DatabaseEloquentBuilderTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index a75cf609a797..855668bfe222 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -4,13 +4,12 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Collection as BaseCollection; -use Illuminate\Database\Capsule\Manager as DB; class DatabaseEloquentBuilderTest extends PHPUnit_Framework_TestCase { public function setUp() { - $db = new DB; + $db = new Illuminate\Database\Capsule\Manager; $db->addConnection([ 'driver' => 'sqlite', @@ -915,4 +914,4 @@ class EloquentBuilderTestUser extends Illuminate\Database\Eloquent\Model protected $table = 'users'; protected $guarded = []; public $timestamps = false; -} \ No newline at end of file +}