Skip to content

Commit 3944d56

Browse files
committed
Fix formatting and simplify API
1 parent 5d7c584 commit 3944d56

File tree

4 files changed

+70
-76
lines changed

4 files changed

+70
-76
lines changed

src/FakePdoTrait.php

+21
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,25 @@ public function rollback()
121121
Server::restoreSnapshot('transaction');
122122
return true;
123123
}
124+
125+
/**
126+
* @param string $statement
127+
* @return int|false
128+
*/
129+
public function exec($statement)
130+
{
131+
$statement = trim($statement);
132+
133+
if (strpos($statement, 'SET ')===0) {
134+
return false;
135+
}
136+
137+
$sth = $this->prepare($statement);
138+
139+
if ($sth->execute()) {
140+
return $sth->rowCount();
141+
}
142+
143+
return false;
144+
}
124145
}

src/Php7/FakePdo.php

+19-37
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,27 @@ class FakePdo extends PDO implements FakePdoInterface
99
{
1010
use FakePdoTrait;
1111

12-
/**
13-
* @param string $statement
14-
* @param array $options
15-
* @return FakePdoStatement
16-
*/
17-
public function prepare($statement, array $options = [])
12+
/**
13+
* @param string $statement
14+
* @param array $options
15+
* @return FakePdoStatement
16+
*/
17+
public function prepare($statement, $options = [])
1818
{
1919
return new FakePdoStatement($this, $statement, $this->real);
2020
}
2121

22-
/**
23-
* @param string $statement
24-
* @return int|false
25-
*/
26-
public function exec($statement)
27-
{
28-
$statement = trim($statement);
29-
if (strpos($statement, 'SET ')===0){
30-
return false;
31-
}
32-
33-
$sth = $this->prepare($statement);
34-
if ($sth->execute()){
35-
return $sth->rowCount();
36-
}
37-
return false;
38-
}
39-
40-
/**
41-
* @param string $statement
42-
* @param int $mode
43-
* @param null $arg3
44-
* @param array $ctorargs
45-
* @return FakePdoStatement
46-
*/
47-
public function query($statement, $mode = PDO::ATTR_DEFAULT_FETCH_MODE, $arg3 = null, array $ctorargs = [])
48-
{
49-
$sth = $this->prepare($statement);
50-
$sth->execute();
51-
return $sth;
52-
}
22+
/**
23+
* @param string $statement
24+
* @param int $mode
25+
* @param null $arg3
26+
* @param array $ctorargs
27+
* @return FakePdoStatement
28+
*/
29+
public function query($statement, $mode = PDO::ATTR_DEFAULT_FETCH_MODE, $arg3 = null, array $ctorargs = [])
30+
{
31+
$sth = $this->prepare($statement);
32+
$sth->execute();
33+
return $sth;
34+
}
5335
}

src/Php8/FakePdo.php

+18-36
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,30 @@
55
use Vimeo\MysqlEngine\FakePdoInterface;
66
use Vimeo\MysqlEngine\FakePdoTrait;
77

8-
class FakePdo extends \PDO implements FakePdoInterface
8+
class FakePdo extends PDO implements FakePdoInterface
99
{
1010
use FakePdoTrait;
1111

12-
/**
13-
* @param string $statement
14-
* @param array $options
15-
* @return FakePdoStatement
16-
*/
12+
/**
13+
* @param string $statement
14+
* @param array $options
15+
* @return FakePdoStatement
16+
*/
1717
public function prepare($statement, array $options = [])
1818
{
1919
return new FakePdoStatement($this, $statement, $this->real);
2020
}
2121

22-
/**
23-
* @param string $statement
24-
* @return int|false
25-
*/
26-
public function exec($statement)
27-
{
28-
$statement = trim($statement);
29-
if (str_starts_with($statement, 'SET ')){
30-
return false;
31-
}
32-
33-
$sth = $this->prepare($statement);
34-
if ($sth->execute()){
35-
return $sth->rowCount();
36-
}
37-
return false;
38-
}
39-
40-
/**
41-
* @param string $statement
42-
* @param int|null $mode
43-
* @param mixed ...$fetchModeArgs
44-
* @return FakePdoStatement
45-
*/
46-
public function query(string $statement, ?int $mode = PDO::ATTR_DEFAULT_FETCH_MODE, mixed ...$fetchModeArgs)
47-
{
48-
$sth = $this->prepare($statement);
49-
$sth->execute();
50-
return $sth;
51-
}
22+
/**
23+
* @param string $statement
24+
* @param int|null $mode
25+
* @param mixed ...$fetchModeArgs
26+
* @return FakePdoStatement
27+
*/
28+
public function query(string $statement, ?int $mode = PDO::ATTR_DEFAULT_FETCH_MODE, mixed ...$fetchModeArgs)
29+
{
30+
$sth = $this->prepare($statement);
31+
$sth->execute();
32+
return $sth;
33+
}
5234
}

tests/SelectProcessorTest.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function testCast()
1616

1717
$this->assertInstanceOf(SelectQuery::class, $select_query);
1818

19-
$conn = new \Vimeo\MysqlEngine\Php8\FakePdo('mysql:foo');
19+
$conn = self::getPdo('mysql:foo');
2020

2121
$this->assertSame(
2222
[['a' => 3]],
@@ -37,7 +37,7 @@ public function testSubqueryCalculation()
3737

3838
$this->assertInstanceOf(SelectQuery::class, $select_query);
3939

40-
$conn = new \Vimeo\MysqlEngine\Php8\FakePdo('mysql:foo');
40+
$conn = self::getPdo('mysql:foo');
4141

4242
$this->assertSame(
4343
[['a' => 5]],
@@ -58,7 +58,7 @@ public function testStringDecimalIntComparison()
5858

5959
$this->assertInstanceOf(SelectQuery::class, $select_query);
6060

61-
$conn = new \Vimeo\MysqlEngine\Php8\FakePdo('mysql:foo');
61+
$conn = self::getPdo('mysql:foo');
6262

6363
$this->assertSame(
6464
[['a' => 0]],
@@ -70,4 +70,13 @@ public function testStringDecimalIntComparison()
7070
)->rows
7171
);
7272
}
73+
74+
private static function getPdo(string $connection_string) : \PDO
75+
{
76+
if (\PHP_MAJOR_VERSION === 8) {
77+
return new \Vimeo\MysqlEngine\Php8\FakePdo($connection_string);
78+
}
79+
80+
return new \Vimeo\MysqlEngine\Php7\FakePdo($connection_string);
81+
}
7382
}

0 commit comments

Comments
 (0)