Skip to content

Commit c8753a2

Browse files
committed
- Updated php-version constraint from 7.0 to 7.1
- Fixed compatibility issues with php 8.0 - Upgraded phpunit base version to 6.0
1 parent ecbf0cf commit c8753a2

File tree

14 files changed

+303
-290
lines changed

14 files changed

+303
-290
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
!/.travis.yml
99
!/.scrutinizer.yml
1010
!/composer.json
11+
!/composer-nodev.json
1112
!/README.md
1213
!/tests.xml

.travis.yml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
language: php
22

33
php:
4+
- nightly
45
- 7.4
56
- 7.3
67
- 7.2
78
- 7.1
8-
- 7.0
9+
10+
matrix:
11+
fast_finish: true
12+
allow_failures:
13+
- php: nightly
914

1015
services:
1116
- mysql
1217

13-
before_script:
14-
- composer self-update
15-
- composer update
18+
install:
19+
- |
20+
# Install PHPUnit v9 on PHP 8
21+
if dpkg --compare-versions "${TRAVIS_PHP_VERSION}" ge 8.0; then
22+
travis_retry composer require --no-interaction --prefer-dist --dev --ignore-platform-reqs "phpunit/phpunit" 9.*
23+
# Enable test on PHP 7.1+
24+
else
25+
travis_retry composer install --no-interaction --prefer-dist
26+
fi
1627
1728
script:
1829
- composer run-script phpunit

composer.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@
88
"email": "[email protected]"
99
}],
1010
"require": {
11-
"php": ">= 7.0",
11+
"php": ">= 7.1",
1212
"ext-pdo": "*",
1313
"ext-spl": "*",
1414
"ext-ctype": "*",
1515
"psr/log": "~1"
1616
},
1717
"require-dev": {
18-
"phpunit/phpunit": "~5.0",
19-
"phake/phake": "1.0.5",
20-
"phpstan/phpstan": "*@stable",
21-
"rkr/fakepdo": "0.1.*"
18+
"phpunit/phpunit": "~6",
19+
"phpstan/phpstan": "*@stable"
2220
},
2321
"autoload": {
2422
"psr-4": {
@@ -36,7 +34,7 @@
3634
},
3735
"config": {
3836
"platform": {
39-
"php": "7.0.32"
37+
"php": "7.1.10"
4038
}
4139
}
4240
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
namespace Kir\MySQL\Builder\Traits;
3+
4+
use Closure;
5+
use Kir\MySQL\Builder\Expr\OptionalExpression;
6+
7+
trait ConditionDefinition {
8+
/**
9+
* @param Closure $addFn
10+
* @param string|array|object|OptionalExpression $expression
11+
* @param mixed[] $args
12+
* @return $this
13+
*/
14+
protected function addCondition(Closure $addFn, $expression, ...$args) {
15+
if($expression instanceof OptionalExpression) {
16+
if($expression->isValid()) {
17+
$addFn($expression->getExpression(), $expression->getValue());
18+
}
19+
} elseif(is_object($expression)) {
20+
$this->addAsArray($addFn, (array) $expression, $args);
21+
} elseif(is_array($expression)) {
22+
$this->addAsArray($addFn, $expression, $args);
23+
} else {
24+
$addFn($expression, $args);
25+
}
26+
return $this;
27+
}
28+
29+
/**
30+
* @param Closure $addFn
31+
* @param array $expression
32+
* @param array $args
33+
*/
34+
private function addAsArray(Closure $addFn, array $expression, array $args) {
35+
if(count($expression) > 0) {
36+
$addFn($expression, $args);
37+
}
38+
}
39+
}

src/Builder/Traits/HavingBuilder.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,19 @@
66

77
trait HavingBuilder {
88
use AbstractDB;
9+
use ConditionDefinition;
910

10-
/** @var array */
11+
/** @var array[] */
1112
private $having = [];
1213

1314
/**
14-
* @param string|array|OptionalExpression $expression
15-
* @param array<int, mixed> $args
15+
* @param string|array|object|OptionalExpression $expression
16+
* @param mixed[] $args
1617
* @return $this
1718
*/
1819
public function having($expression, ...$args) {
19-
if($expression instanceof OptionalExpression) {
20-
if($expression->isValid()) {
21-
$this->having[] = [$expression->getExpression(), $expression->getValue()];
22-
}
23-
} elseif(is_array($expression) || is_object($expression)) {
24-
if(is_object($expression)) {
25-
$expression = (array) $expression;
26-
}
27-
if(count($expression) > 0) {
28-
$this->having[] = [$expression, array_slice(func_get_args(), 1)];
29-
}
30-
} else {
31-
$this->having[] = [$expression, array_slice(func_get_args(), 1)];
32-
}
33-
return $this;
20+
$fn = function (...$args) { $this->having[] = $args; };
21+
return $this->addCondition($fn, $expression, ...$args);
3422
}
3523

3624
/**

src/Builder/Traits/WhereBuilder.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22
namespace Kir\MySQL\Builder\Traits;
33

4-
use Kir\MySQL\Builder\DBExpr;
54
use Kir\MySQL\Builder\Expr\OptionalExpression;
65
use Kir\MySQL\Builder\Internal\ConditionBuilder;
76

87
trait WhereBuilder {
98
use AbstractDB;
9+
use ConditionDefinition;
1010

11-
/** @var array<int, mixed> */
11+
/** @var array[] */
1212
private $where = [];
1313

1414
/**
@@ -17,21 +17,8 @@ trait WhereBuilder {
1717
* @return $this
1818
*/
1919
public function where($expression, ...$args) {
20-
if($expression instanceof OptionalExpression) {
21-
if($expression->isValid()) {
22-
$this->where[] = [$expression->getExpression(), $expression->getValue()];
23-
}
24-
} elseif(is_array($expression) || is_object($expression)) {
25-
if(is_object($expression)) {
26-
$expression = (array) $expression;
27-
}
28-
if(count($expression) > 0) {
29-
$this->where[] = [$expression, $args];
30-
}
31-
} else {
32-
$this->where[] = [$expression, $args];
33-
}
34-
return $this;
20+
$fn = function (...$args) { $this->where[] = $args; };
21+
return $this->addCondition($fn, $expression, ...$args);
3522
}
3623

3724
/**

tests/Builder/DeleteTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<?php
22
namespace Kir\MySQL\Builder;
33

4-
use Kir\MySQL\Builder\DeleteTest\TestDelete;
4+
use Kir\MySQL\Common\DBTestCase;
55

6-
class DeleteTest extends \PHPUnit_Framework_TestCase {
6+
class DeleteTest extends DBTestCase {
77
public function testAlias() {
8-
$query = TestDelete::create()
8+
$query = $this->delete()
99
->from('t', 'travis#test1')
1010
->where('t.a = 1')
1111
->asString();
1212
self::assertEquals("DELETE t FROM\n\ttravis_test.test1 t\nWHERE\n\t(t.a = 1)\n", $query);
1313
}
1414

1515
public function testMultipleTables() {
16-
$sql = TestDelete::create()
16+
$sql = $this->delete()
1717
->from('t1', 'travis#test1')
1818
->from('t2', 'travis#test2')
1919
->where('t1.a = 1')
@@ -22,15 +22,15 @@ public function testMultipleTables() {
2222
}
2323

2424
public function testJoins() {
25-
$sql = TestDelete::create()
25+
$sql = $this->delete()
2626
->from('t1', 'travis#test1')
2727
->joinInner('t2', 'travis#test2', 't1.id = t2.id')
2828
->asString();
2929
self::assertEquals("DELETE t1 FROM\n\ttravis_test.test1 t1\nINNER JOIN\n\ttravis_test.test2 t2 ON t1.id = t2.id\n", $sql);
3030
}
3131

3232
public function testWhere() {
33-
$sql = TestDelete::create()
33+
$sql = $this->delete()
3434
->from('travis#test1')
3535
->where('field1=?', 1)
3636
->where('field2 != field1')
@@ -39,39 +39,39 @@ public function testWhere() {
3939
}
4040

4141
public function testWhereViaArray() {
42-
$sql = TestDelete::create()
42+
$sql = $this->delete()
4343
->from('travis#test1')
4444
->where(['field1' => 1, 'field2' => 'aaa'])
4545
->asString();
4646
self::assertEquals("DELETE FROM\n\ttravis_test.test1\nWHERE\n\t(`field1`='1')\n\tAND\n\t(`field2`='aaa')\n", $sql);
4747
}
4848

4949
public function testDBExpr() {
50-
$sql = TestDelete::create()
50+
$sql = $this->delete()
5151
->from('travis#test1')
5252
->where('field1=?', new DBExpr('NOW()'))
5353
->asString();
5454
self::assertEquals("DELETE FROM\n\ttravis_test.test1\nWHERE\n\t(field1=NOW())\n", $sql);
5555
}
5656

5757
public function testOrderBy() {
58-
$sql = TestDelete::create()
58+
$sql = $this->delete()
5959
->from('travis#test1')
6060
->orderBy('field1', 'DESC')
6161
->asString();
6262
self::assertEquals("DELETE FROM\n\ttravis_test.test1\nORDER BY\n\tfield1 DESC\n", $sql);
6363
}
6464

6565
public function testLimit() {
66-
$sql = TestDelete::create()
66+
$sql = $this->delete()
6767
->from('travis#test1')
6868
->limit(10)
6969
->asString();
7070
self::assertEquals("DELETE FROM\n\ttravis_test.test1\nLIMIT\n\t10\n", $sql);
7171
}
7272

7373
public function testOffset() {
74-
$sql = TestDelete::create()
74+
$sql = $this->delete()
7575
->from('travis#test1')
7676
->offset(10)
7777
->asString();

0 commit comments

Comments
 (0)