Skip to content

Commit

Permalink
Merge pull request #76 from PhpGt/organise-tests
Browse files Browse the repository at this point in the history
Refactor tests
  • Loading branch information
g105b authored Dec 5, 2017
2 parents 0d7891b + 6abf61b commit ba65fb6
Show file tree
Hide file tree
Showing 61 changed files with 2,477 additions and 2,452 deletions.
24 changes: 24 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: 2

jobs:
build:
docker:
- image: circleci/php:7.1-cli
working_directory: ~/repo
steps:
- checkout
- restore_cache:
keys:
- v1-dependencies-{{ checksum "composer.json" }}
- v1-dependencies-
- run: composer install -n --prefer-dist
- save_cache:
paths:
- ./vendor
key: v1-dependencies-{{ checksum "composer.json" }}
- run: mkdir test/unit/_junit
- run: vendor/bin/phpunit -c test/unit/phpunit.xml --log-junit test/unit/_junit/junit.xml -d memory_limit=512M
- store_test_results:
path: test/unit/_junit
- store_artifacts:
path: test/unit/_coverage
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea/
/vendor/
/test/unit/report/
composer.phar
composer.phar
6 changes: 3 additions & 3 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
build:
environment:
php:
version: 7.1
version: 7.1.0
tests:
override:
-
command: 'vendor/bin/phpunit --coverage-clover=coverage'
command: 'vendor/bin/phpunit test/unit --coverage-clover coverage --whitelist src'
coverage:
file: 'coverage'
format: 'php-clover'
Expand All @@ -18,4 +18,4 @@ checks:
filter:
excluded_paths:
- test/*
- vendor/*
- vendor/*
1 change: 0 additions & 1 deletion bin/db-migrate
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env php
<?php
use Gt\Database\Connection\Settings;
use Gt\Database\Connection\DefaultSettings;
use Gt\Database\Migration\Config;
use Gt\Database\Migration\Migrator;
/**
Expand Down
8 changes: 0 additions & 8 deletions circle.yml

This file was deleted.

6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@

"autoload": {
"psr-4": {
"Gt\\Database\\": "./src",
"Gt\\Database\\": "./src"
}
},
"autoload-dev": {
"psr-4": {
"Gt\\Database\\Test\\": "./test/unit"
}
},
Expand Down
22 changes: 0 additions & 22 deletions phpunit.xml

This file was deleted.

229 changes: 120 additions & 109 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,120 +17,131 @@
* required as the default name will be used.
*/
class Client {
/** @var QueryCollectionFactory[] */
protected $queryCollectionFactoryArray;
/** @var Driver[] */
protected $driverArray;
/** @var Connection */
protected $currentConnectionName;

public function __construct(SettingsInterface...$connectionSettings) {
if(empty($connectionSettings)) {
$connectionSettings[DefaultSettings::DEFAULT_NAME]
= new DefaultSettings();
}

$this->storeConnectionDriverFromSettings($connectionSettings);
$this->storeQueryCollectionFactoryFromSettings($connectionSettings);
}

public function fetch(string $queryName, ...$bindings):?Row {
$result = $this->query($queryName, $bindings);

return $result->fetch();
}

public function fetchAll(string $queryName, ...$bindings):ResultSet {
return $this->query($queryName, $bindings);
}

public function insert(string $queryName, ...$bindings):int {
$result = $this->query($queryName, $bindings);

return $result->lastInsertId();
}

public function delete(string $queryName, ...$bindings):int {
$result = $this->query($queryName, $bindings);

return $result->affectedRows();
}

public function update(string $queryName, ...$bindings):int {
$result = $this->query($queryName, $bindings);

return $result->affectedRows();
}

public function query(string $queryName, ...$bindings):ResultSet {
while(isset($bindings[0])
&& is_array($bindings[0])) {
$bindings = $bindings[0];
}

$queryCollectionName = substr(
$queryName,
0,
strrpos($queryName, "/")
);
$queryFile = substr(
$queryName,
strrpos($queryName, "/") + 1
);

$connectionName = $this->currentConnectionName ?? DefaultSettings::DEFAULT_NAME;
$queryCollection = $this->queryCollection(
$queryCollectionName,
$connectionName
);

return $queryCollection->query($queryFile, $bindings);
}

public function setCurrentConnectionName(string $connectionName) {
$this->currentConnectionName = $this->getNamedConnection(
$connectionName
);
}

public function executeSql(
string $query,
array $bindings = [],
string $connectionName = DefaultSettings::DEFAULT_NAME
):ResultSet {
$connection = $this->getNamedConnection($connectionName);
$statement = $connection->prepare($query);
$statement->execute($bindings);

return new ResultSet($statement, $connection->lastInsertId());
}

protected function getNamedConnection(string $connectionName):Connection {
$driver = $this->driverArray[$connectionName];

return $driver->getConnection();
}

/** @var QueryCollectionFactory[] */
protected $queryCollectionFactoryArray;
/** @var Driver[] */
protected $driverArray;
/** @var Connection */
protected $currentConnectionName;

public function __construct(SettingsInterface...$connectionSettings) {
if(empty($connectionSettings)) {
$connectionSettings[DefaultSettings::DEFAULT_NAME]
= new DefaultSettings();
protected function storeConnectionDriverFromSettings(array $settingsArray) {
foreach($settingsArray as $settings) {
$connectionName = $settings->getConnectionName();
$this->driverArray[$connectionName] = new Driver($settings);
}
}

$this->storeConnectionDriverFromSettings($connectionSettings);
$this->storeQueryCollectionFactoryFromSettings($connectionSettings);
}

public function fetch(string $queryName, ...$bindings):?Row {
$result = $this->query($queryName, $bindings);
return $result->fetch();
}

public function fetchAll(string $queryName, ...$bindings):ResultSet {
return $this->query($queryName, $bindings);
}

public function insert(string $queryName, ...$bindings):int {
$result = $this->query($queryName, $bindings);
return $result->lastInsertId();
}

public function delete(string $queryName, ...$bindings):int {
$result = $this->query($queryName, $bindings);
return $result->affectedRows();
}

public function update(string $queryName, ...$bindings):int {
$result = $this->query($queryName, $bindings);
return $result->affectedRows();
}

public function query(string $queryName, ...$bindings):ResultSet {
while(isset($bindings[0])
&& is_array($bindings[0])) {
$bindings = $bindings[0];
protected function storeQueryCollectionFactoryFromSettings(array $settingsArray) {
foreach($settingsArray as $settings) {
$connectionName = $settings->getConnectionName();
$this->queryCollectionFactoryArray[$connectionName] =
new QueryCollectionFactory($this->driverArray[$connectionName]);
}
}

$queryCollectionName = substr(
$queryName,
0,
strrpos($queryName, "/")
);
$queryFile = substr(
$queryName,
strrpos($queryName, "/") + 1
);

$connectionName = $this->currentConnectionName ?? DefaultSettings::DEFAULT_NAME;
$queryCollection = $this->queryCollection($queryCollectionName, $connectionName);
return $queryCollection->query($queryFile, $bindings);
}

public function setCurrentConnectionName(string $connectionName) {
$this->currentConnectionName = $this->getNamedConnection($connectionName);
}

public function executeSql(
string $query,
array $bindings = [],
string $connectionName = DefaultSettings::DEFAULT_NAME
):ResultSet {
$connection = $this->getNamedConnection($connectionName);
$statement = $connection->prepare($query);
$statement->execute($bindings);
return new ResultSet($statement, $connection->lastInsertId());
}

protected function getNamedConnection(string $connectionName):Connection {
$driver = $this->driverArray[$connectionName];
return $driver->getConnection();
}

protected function storeConnectionDriverFromSettings(array $settingsArray) {
foreach ($settingsArray as $settings) {
$connectionName = $settings->getConnectionName();
$this->driverArray[$connectionName] = new Driver($settings);
public function queryCollection(
string $queryCollectionName,
string $connectionName = DefaultSettings::DEFAULT_NAME
):QueryCollection {
return $this->queryCollectionFactoryArray[$connectionName]->create($queryCollectionName);
}
}

protected function storeQueryCollectionFactoryFromSettings(array $settingsArray) {
foreach ($settingsArray as $settings) {
$connectionName = $settings->getConnectionName();
$this->queryCollectionFactoryArray[$connectionName] =
new QueryCollectionFactory($this->driverArray[$connectionName]);
public function getDriver(
string $connectionName = DefaultSettings::DEFAULT_NAME
):Driver {
return $this->driverArray[$connectionName];
}

protected function getFirstConnectionName():string {
reset($this->driverArray);

return key($this->driverArray);
}
}

public function queryCollection(
string $queryCollectionName,
string $connectionName = DefaultSettings::DEFAULT_NAME
):QueryCollection {
return $this->queryCollectionFactoryArray[$connectionName]->create($queryCollectionName);
}

public function getDriver(
string $connectionName = DefaultSettings::DEFAULT_NAME
):Driver {
return $this->driverArray[$connectionName];
}

protected function getFirstConnectionName():string {
reset($this->driverArray);
return key($this->driverArray);
}

}#
}
4 changes: 3 additions & 1 deletion src/Connection/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

namespace Gt\Database\Connection;

class Connection extends \PDO {}#
use PDO;

class Connection extends PDO {}
4 changes: 3 additions & 1 deletion src/Connection/ConnectionNotConfiguredException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
namespace Gt\Database\Connection;

class ConnectionNotConfiguredException extends \Gt\Database\DatabaseException {}#
use Gt\Database\DatabaseException;

class ConnectionNotConfiguredException extends DatabaseException {}
Loading

0 comments on commit ba65fb6

Please sign in to comment.