Skip to content

Commit 90ae6be

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents cc3c6ef + 0f5b65c commit 90ae6be

File tree

3 files changed

+23
-31
lines changed

3 files changed

+23
-31
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
},
1313
"require-dev": {
1414
"phpunit/phpunit": "~4.0",
15-
"phake/phake": "1.0.5"
15+
"phake/phake": "1.0.5",
16+
"jimbojsb/pseudo": "dev-master"
1617
},
1718
"autoload": {
1819
"psr-4": {

src/Databases/MySQL.php

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
use Kir\MySQL\Builder\Exception;
66
use Kir\MySQL\Database;
77
use Kir\MySQL\Tools\AliasRegistry;
8-
use PDO;
9-
use PDOStatement;
10-
use Psr\Log\LoggerInterface;
11-
use Psr\Log\NullLogger;
128
use UnexpectedValueException;
139
use Kir\MySQL\Builder\RunnableSelect;
1410

@@ -31,22 +27,12 @@ class MySQL implements Database {
3127
* @var int
3228
*/
3329
private $transactionLevel = 0;
34-
/**
35-
* @var LoggerInterface
36-
*/
37-
private $logger;
3830

3931
/**
40-
* @param PDO $pdo
41-
* @param LoggerInterface $logger
32+
* @param \PDO $pdo
4233
*/
43-
public function __construct(PDO $pdo, LoggerInterface $logger = null) {
44-
if($logger === null) {
45-
$logger = new NullLogger();
46-
}
47-
34+
public function __construct(\PDO $pdo) {
4835
$this->pdo = $pdo;
49-
$this->logger = $logger;
5036
$this->aliasRegistry = new AliasRegistry();
5137
}
5238

@@ -60,13 +46,10 @@ public function getAliasRegistry() {
6046
/**
6147
* @param string $query
6248
* @throws Exception
63-
* @return PDOStatement
49+
* @return \PDOStatement
6450
*/
6551
public function query($query) {
66-
$this->logger->info($query);
67-
$timer = microtime(true);
6852
$stmt = $this->pdo->query($query);
69-
$this->logger->debug(sprintf("Last query duration: %0.5f sec.", microtime(true) - $timer));
7053
if(!$stmt) {
7154
throw new Exception("Could not execute statement:\n{$query}");
7255
}
@@ -76,10 +59,9 @@ public function query($query) {
7659
/**
7760
* @param string $query
7861
* @throws Exception
79-
* @return PDOStatement
62+
* @return \PDOStatement
8063
*/
8164
public function prepare($query) {
82-
$this->logger->info("PREPARE: {$query}");
8365
$stmt = $this->pdo->prepare($query);
8466
if(!$stmt) {
8567
throw new Exception("Could not execute statement:\n{$query}");
@@ -93,11 +75,8 @@ public function prepare($query) {
9375
* @return int
9476
*/
9577
public function exec($query, array $params = array()) {
96-
$this->logger->info($query);
97-
$timer = microtime(true);
9878
$stmt = $this->pdo->prepare($query);
9979
$stmt->execute($params);
100-
$this->logger->debug(sprintf("Last query duration: %0.5f sec.", microtime(true) - $timer));
10180
$result = $stmt->rowCount();
10281
$stmt->closeCursor();
10382
return $result;
@@ -115,12 +94,12 @@ public function getLastInsertId() {
11594
* @return array
11695
*/
11796
public function getTableFields($table) {
97+
$table = $this->select()->aliasReplacer()->replace($table);
11898
if(array_key_exists($table, self::$tableFields)) {
11999
return self::$tableFields[$table];
120100
}
121101
$stmt = $this->pdo->query("DESCRIBE {$table}");
122-
$stmt->execute();
123-
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
102+
$rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
124103
self::$tableFields[$table] = array_map(function ($row) { return $row['Field']; }, $rows);
125104
$stmt->closeCursor();
126105
return self::$tableFields[$table];

tests/Databases/MySQLTest.php

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

4-
use PDO;
4+
use Phake;
5+
use Pseudo\Pdo;
56

67
class MySQLTest extends \PHPUnit_Framework_TestCase {
78
public function testTransactionTries1() {
8-
$pdo = new PDO('sqlite::memory:');
9+
$pdo = new Pdo();
910
$mysql = new MySQL($pdo);
1011

1112
$this->setExpectedException('Exception', '5');
@@ -18,7 +19,7 @@ public function testTransactionTries1() {
1819
}
1920

2021
public function testTransactionTries2() {
21-
$pdo = new PDO('sqlite::memory:');
22+
$pdo = new Pdo();
2223
$mysql = new MySQL($pdo);
2324

2425
$result = $mysql->transaction(5, function () {
@@ -32,4 +33,15 @@ public function testTransactionTries2() {
3233

3334
$this->assertEquals(5, $result);
3435
}
36+
37+
public function testGetTableFields() {
38+
$pdo = new Pdo();
39+
$pdo->mock('DESCRIBE test__table', [['Field' => 'a'], ['Field' => 'b'], ['Field' => 'c']]);
40+
41+
$mysql = new MySQL($pdo);
42+
$mysql->getAliasRegistry()->add('test', 'test__');
43+
$fields = $mysql->getTableFields('test#table');
44+
45+
$this->assertEquals(array('a', 'b', 'c'), $fields);
46+
}
3547
}

0 commit comments

Comments
 (0)