From 7af827fccd1faf1172005099e986a1cd24b3902e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Nabia=C5=82ek?= Date: Thu, 31 Dec 2020 14:18:52 +0100 Subject: [PATCH] Don't fail in case bindings are null --- CHANGELOG.md | 6 +++++- src/Objects/SqlQuery.php | 14 +++++++------- tests/QueryTest.php | 23 +++++++++++++++++++++++ 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8366b8..fba26e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. +## [2.2.8] - 2020-12-31 +### Fixed +- Fix case when bindings are null + ## [2.2.7] - 2020-09-17 ### Changed - Add support for Laravel 8 @@ -94,4 +98,4 @@ All notable changes to this project will be documented in this file. ## [1.0] - 2016-02-16 ### Added - Log SQL queries -- Log slow SQL queries \ No newline at end of file +- Log slow SQL queries diff --git a/src/Objects/SqlQuery.php b/src/Objects/SqlQuery.php index 6e55802..f4f45e9 100644 --- a/src/Objects/SqlQuery.php +++ b/src/Objects/SqlQuery.php @@ -33,20 +33,20 @@ class SqlQuery * * @param int $number * @param string $sql - * @param array $bindings + * @param array|null $bindings * @param float $time */ - public function __construct($number, $sql, array $bindings, $time) + public function __construct($number, $sql, array $bindings = null, $time) { $this->number = $number; $this->sql = $sql; - $this->bindings = $bindings; + $this->bindings = $bindings ?: []; $this->time = $time; } /** * Get number of query. - * + * * @return int */ public function number() @@ -56,7 +56,7 @@ public function number() /** * Get raw SQL (without bindings). - * + * * @return string */ public function raw() @@ -66,7 +66,7 @@ public function raw() /** * Get bindings. - * + * * @return array */ public function bindings() @@ -76,7 +76,7 @@ public function bindings() /** * Get time. - * + * * @return float */ public function time() diff --git a/tests/QueryTest.php b/tests/QueryTest.php index c21711a..a7e567f 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -54,4 +54,27 @@ public function it_returns_valid_sql_query_object_when_version_is_5_2_0() $this->assertSame($dataObject->bindings, $result->bindings()); $this->assertSame($dataObject->time, $result->time()); } + + /** @test */ + public function it_returns_valid_sql_query_object_when_bindings_are_null() + { + $version = Mockery::mock(Version::class); + $version->shouldReceive('min')->once()->with('5.2.0')->andReturn(true); + + $queryObject = new Query($version); + + $number = 100; + $dataObject = new stdClass(); + $dataObject->sql = 'SELECT * FROM everywhere WHERE user = ?'; + $dataObject->bindings = null; + $dataObject->time = 516.32; + + $result = $queryObject->get($number, $dataObject); + + $this->assertInstanceOf(SqlQuery::class, $result); + $this->assertSame($number, $result->number()); + $this->assertSame($dataObject->sql, $result->raw()); + $this->assertSame([], $result->bindings()); + $this->assertSame($dataObject->time, $result->time()); + } }