Skip to content

Commit

Permalink
Don't fail in case bindings are null
Browse files Browse the repository at this point in the history
  • Loading branch information
mnabialek committed Dec 31, 2020
1 parent 6c30e7b commit 7af827f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
- Log slow SQL queries
14 changes: 7 additions & 7 deletions src/Objects/SqlQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -56,7 +56,7 @@ public function number()

/**
* Get raw SQL (without bindings).
*
*
* @return string
*/
public function raw()
Expand All @@ -66,7 +66,7 @@ public function raw()

/**
* Get bindings.
*
*
* @return array
*/
public function bindings()
Expand All @@ -76,7 +76,7 @@ public function bindings()

/**
* Get time.
*
*
* @return float
*/
public function time()
Expand Down
23 changes: 23 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 7af827f

Please sign in to comment.