Compare commits
17 Commits
d67916e2ff
...
1.0.4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
35188860ce | ||
|
|
c8d6e911a6 | ||
|
|
03d26d9167 | ||
|
|
737090c6a8 | ||
|
|
43729579fa | ||
|
|
bdb0a88e58 | ||
|
|
523d307f81 | ||
|
|
fd54281ce3 | ||
|
|
97c8a8a92c | ||
|
|
80a611d5ac | ||
|
|
9db5e9b2bd | ||
| 39eb008118 | |||
| aa551c697b | |||
|
|
ff515202c9 | ||
|
|
72ba193a1e | ||
|
|
4d7f0d6d83 | ||
|
|
a6c51f5f17 |
@@ -4,7 +4,8 @@
|
||||
"type": "library",
|
||||
"require": {
|
||||
"vlucas/phpdotenv": "^5.5",
|
||||
"evenement/evenement": "^3.0"
|
||||
"evenement/evenement": "^3.0",
|
||||
"symfony/var-dumper": "^6.3"
|
||||
},
|
||||
"require-dev": {
|
||||
"squizlabs/php_codesniffer": "^3.4",
|
||||
|
||||
220
composer.lock
generated
220
composer.lock
generated
@@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "7b31fc4374d54dc20d8f35625a8f74a7",
|
||||
"content-hash": "ef49a4d31601e74941ecce68ae0ff3ff",
|
||||
"packages": [
|
||||
{
|
||||
"name": "evenement/evenement",
|
||||
@@ -190,6 +190,73 @@
|
||||
],
|
||||
"time": "2023-02-25T19:38:58+00:00"
|
||||
},
|
||||
{
|
||||
"name": "symfony/deprecation-contracts",
|
||||
"version": "v3.3.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/symfony/deprecation-contracts.git",
|
||||
"reference": "7c3aff79d10325257a001fcf92d991f24fc967cf"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/7c3aff79d10325257a001fcf92d991f24fc967cf",
|
||||
"reference": "7c3aff79d10325257a001fcf92d991f24fc967cf",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.1"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-main": "3.4-dev"
|
||||
},
|
||||
"thanks": {
|
||||
"name": "symfony/contracts",
|
||||
"url": "https://github.com/symfony/contracts"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"files": [
|
||||
"function.php"
|
||||
]
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Nicolas Grekas",
|
||||
"email": "p@tchwork.com"
|
||||
},
|
||||
{
|
||||
"name": "Symfony Community",
|
||||
"homepage": "https://symfony.com/contributors"
|
||||
}
|
||||
],
|
||||
"description": "A generic function and convention to trigger deprecation notices",
|
||||
"homepage": "https://symfony.com",
|
||||
"support": {
|
||||
"source": "https://github.com/symfony/deprecation-contracts/tree/v3.3.0"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://symfony.com/sponsor",
|
||||
"type": "custom"
|
||||
},
|
||||
{
|
||||
"url": "https://github.com/fabpot",
|
||||
"type": "github"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2023-05-23T14:45:45+00:00"
|
||||
},
|
||||
{
|
||||
"name": "symfony/polyfill-ctype",
|
||||
"version": "v1.28.0",
|
||||
@@ -438,6 +505,90 @@
|
||||
],
|
||||
"time": "2023-01-26T09:26:14+00:00"
|
||||
},
|
||||
{
|
||||
"name": "symfony/var-dumper",
|
||||
"version": "v6.3.6",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/symfony/var-dumper.git",
|
||||
"reference": "999ede244507c32b8e43aebaa10e9fce20de7c97"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/symfony/var-dumper/zipball/999ede244507c32b8e43aebaa10e9fce20de7c97",
|
||||
"reference": "999ede244507c32b8e43aebaa10e9fce20de7c97",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.1",
|
||||
"symfony/deprecation-contracts": "^2.5|^3",
|
||||
"symfony/polyfill-mbstring": "~1.0"
|
||||
},
|
||||
"conflict": {
|
||||
"symfony/console": "<5.4"
|
||||
},
|
||||
"require-dev": {
|
||||
"ext-iconv": "*",
|
||||
"symfony/console": "^5.4|^6.0",
|
||||
"symfony/http-kernel": "^5.4|^6.0",
|
||||
"symfony/process": "^5.4|^6.0",
|
||||
"symfony/uid": "^5.4|^6.0",
|
||||
"twig/twig": "^2.13|^3.0.4"
|
||||
},
|
||||
"bin": [
|
||||
"Resources/bin/var-dump-server"
|
||||
],
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"files": [
|
||||
"Resources/functions/dump.php"
|
||||
],
|
||||
"psr-4": {
|
||||
"Symfony\\Component\\VarDumper\\": ""
|
||||
},
|
||||
"exclude-from-classmap": [
|
||||
"/Tests/"
|
||||
]
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Nicolas Grekas",
|
||||
"email": "p@tchwork.com"
|
||||
},
|
||||
{
|
||||
"name": "Symfony Community",
|
||||
"homepage": "https://symfony.com/contributors"
|
||||
}
|
||||
],
|
||||
"description": "Provides mechanisms for walking through any arbitrary PHP variable",
|
||||
"homepage": "https://symfony.com",
|
||||
"keywords": [
|
||||
"debug",
|
||||
"dump"
|
||||
],
|
||||
"support": {
|
||||
"source": "https://github.com/symfony/var-dumper/tree/v6.3.6"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://symfony.com/sponsor",
|
||||
"type": "custom"
|
||||
},
|
||||
{
|
||||
"url": "https://github.com/fabpot",
|
||||
"type": "github"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2023-10-12T18:45:56+00:00"
|
||||
},
|
||||
{
|
||||
"name": "vlucas/phpdotenv",
|
||||
"version": "v5.5.0",
|
||||
@@ -3653,73 +3804,6 @@
|
||||
],
|
||||
"time": "2023-08-16T10:10:12+00:00"
|
||||
},
|
||||
{
|
||||
"name": "symfony/deprecation-contracts",
|
||||
"version": "v3.3.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/symfony/deprecation-contracts.git",
|
||||
"reference": "7c3aff79d10325257a001fcf92d991f24fc967cf"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/7c3aff79d10325257a001fcf92d991f24fc967cf",
|
||||
"reference": "7c3aff79d10325257a001fcf92d991f24fc967cf",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.1"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-main": "3.4-dev"
|
||||
},
|
||||
"thanks": {
|
||||
"name": "symfony/contracts",
|
||||
"url": "https://github.com/symfony/contracts"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"files": [
|
||||
"function.php"
|
||||
]
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Nicolas Grekas",
|
||||
"email": "p@tchwork.com"
|
||||
},
|
||||
{
|
||||
"name": "Symfony Community",
|
||||
"homepage": "https://symfony.com/contributors"
|
||||
}
|
||||
],
|
||||
"description": "A generic function and convention to trigger deprecation notices",
|
||||
"homepage": "https://symfony.com",
|
||||
"support": {
|
||||
"source": "https://github.com/symfony/deprecation-contracts/tree/v3.3.0"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://symfony.com/sponsor",
|
||||
"type": "custom"
|
||||
},
|
||||
{
|
||||
"url": "https://github.com/fabpot",
|
||||
"type": "github"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2023-05-23T14:45:45+00:00"
|
||||
},
|
||||
{
|
||||
"name": "symfony/filesystem",
|
||||
"version": "v6.3.1",
|
||||
|
||||
@@ -5,9 +5,10 @@ namespace Kletellier\PdoWrapper;
|
||||
use ArrayIterator;
|
||||
use Countable;
|
||||
use IteratorAggregate;
|
||||
use JsonSerializable;
|
||||
use Traversable;
|
||||
|
||||
class Collection implements IteratorAggregate, Countable
|
||||
class Collection implements IteratorAggregate, Countable, JsonSerializable
|
||||
{
|
||||
private array $elements;
|
||||
|
||||
@@ -57,8 +58,7 @@ class Collection implements IteratorAggregate, Countable
|
||||
|
||||
public function map(callable $fn): Collection
|
||||
{
|
||||
$tmp = array_map($fn, $this->elements);
|
||||
return new Collection($tmp);
|
||||
return new Collection(array_map($fn, $this->elements));
|
||||
}
|
||||
|
||||
public function each(callable $fn): void
|
||||
@@ -68,9 +68,24 @@ class Collection implements IteratorAggregate, Countable
|
||||
|
||||
public function filter(callable $fn): Collection
|
||||
{
|
||||
$tmp = array_filter($this->elements, $fn, ARRAY_FILTER_USE_BOTH);
|
||||
$collect = new Collection($tmp);
|
||||
return $collect;
|
||||
return new Collection(array_filter($this->elements, $fn, ARRAY_FILTER_USE_BOTH));
|
||||
}
|
||||
|
||||
public function pluck(string $key): Collection
|
||||
{
|
||||
return new Collection(array_map(function ($item) use ($key) {
|
||||
if (is_object($item)) {
|
||||
return isset($item->$key) ? $item->$key : null;
|
||||
}
|
||||
if (is_array($item)) {
|
||||
return isset($item[$key]) ? $item[$key] : null;
|
||||
}
|
||||
}, $this->elements));
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return $this->elements;
|
||||
}
|
||||
|
||||
public function empty(): bool
|
||||
@@ -97,4 +112,9 @@ class Collection implements IteratorAggregate, Countable
|
||||
{
|
||||
return new ArrayIterator($this->elements);
|
||||
}
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return $this->elements;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use Dotenv\Dotenv;
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
|
||||
class Db
|
||||
class Connection
|
||||
{
|
||||
protected PDO $pdo;
|
||||
protected string $name;
|
||||
@@ -18,6 +18,7 @@ class Db
|
||||
private string $database;
|
||||
private ?string $port;
|
||||
private ?string $host;
|
||||
private ?string $charset;
|
||||
private string $error;
|
||||
|
||||
private function __construct($name, $config)
|
||||
@@ -40,10 +41,11 @@ class Db
|
||||
$key = (strtolower(trim($this->name)));
|
||||
$this->type = isset($config[$key]["TYPE"]) ? $config[$key]["TYPE"] : null;
|
||||
$this->user = isset($config[$key]["USER"]) ? $config[$key]["USER"] : null;
|
||||
$this->host = isset($config[$key]["HOST"]) ? $config[$config[$key]["HOST"]] : null;
|
||||
$this->host = isset($config[$key]["HOST"]) ? $config[$key]["HOST"] : null;
|
||||
$this->password = isset($config[$key]["PASSWORD"]) ? $config[$key]["PASSWORD"] : null;
|
||||
$this->database = isset($config[$key]["DATABASE"]) ? $config[$key]["DATABASE"] : null;
|
||||
$this->port = isset($config[$key]["PORT"]) ? $config[$key]["PORT"] : null;
|
||||
$this->charset = isset($config[$key]["CHARSET"]) ? $config[$key]["CHARSET"] : null;
|
||||
} catch (\Throwable $th) {
|
||||
$this->error = $th->getMessage();
|
||||
}
|
||||
@@ -70,6 +72,7 @@ class Db
|
||||
$this->password = isset($_ENV[$this->getKey("PASSWORD", $key)]) ? $_ENV[$this->getKey("PASSWORD", $key)] : null;
|
||||
$this->database = isset($_ENV[$this->getKey("DATABASE", $key)]) ? $_ENV[$this->getKey("DATABASE", $key)] : null;
|
||||
$this->port = isset($_ENV[$this->getKey("PORT", $key)]) ? $_ENV[$this->getKey("PORT", $key)] : null;
|
||||
$this->charset = isset($_ENV[$this->getKey("CHARSET", $key)]) ? $_ENV[$this->getKey("CHARSET", $key)] : null;
|
||||
} catch (\Throwable $th) {
|
||||
$this->error = $th->getMessage();
|
||||
}
|
||||
@@ -84,6 +87,7 @@ class Db
|
||||
{
|
||||
$this->getConnString();
|
||||
$this->pdo = new PDO($this->connstring, $this->user, $this->password);
|
||||
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
|
||||
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
}
|
||||
|
||||
@@ -116,10 +120,19 @@ class Db
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
private function getMySQLOptions()
|
||||
{
|
||||
$ret = "";
|
||||
if ($this->charset) {
|
||||
$ret .= ";charset=" . $this->charset;
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
private function getConnString()
|
||||
{
|
||||
$this->connstring = match ($this->type) {
|
||||
"mysql" => "mysql:host=" . $this->host . ":" . $this->port . ";dbname=" . $this->database,
|
||||
"mysql" => "mysql:host=" . $this->host . ":" . $this->port . ";dbname=" . $this->database . $this->getMySQLOptions() ,
|
||||
"sqlite" => "sqlite:" . $this->database,
|
||||
};
|
||||
}
|
||||
@@ -130,13 +143,13 @@ class Db
|
||||
return $stmt->execute();
|
||||
}
|
||||
|
||||
public function getSelectQuery(Query $query): mixed
|
||||
public function getSelectQuery(Query $query, $fetchmode = PDO::FETCH_ASSOC): mixed
|
||||
{
|
||||
$stmt = $this->pdo->prepare($query->getQuery());
|
||||
$stmt = $this->bindParams($stmt, $query->getData());
|
||||
EventDispatcher::emit("on.query", $query);
|
||||
$stmt->execute();
|
||||
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
return $stmt->fetchAll($fetchmode);
|
||||
}
|
||||
|
||||
public function updateQuery(Query $query): bool
|
||||
@@ -160,6 +173,30 @@ class Db
|
||||
}
|
||||
}
|
||||
|
||||
public function beginTransaction(): bool
|
||||
{
|
||||
if ($this->pdo) {
|
||||
return $this->pdo->beginTransaction();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function commit(): bool
|
||||
{
|
||||
if ($this->pdo) {
|
||||
return $this->pdo->commit();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function rollback(): bool
|
||||
{
|
||||
if ($this->pdo) {
|
||||
return $this->pdo->rollBack();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function init($config, array $connections = ["default"])
|
||||
{
|
||||
self::fillInstances($config, $connections);
|
||||
@@ -170,20 +207,19 @@ class Db
|
||||
{
|
||||
foreach ($connections as $connection) {
|
||||
if (!isset(self::$instances[$connection])) {
|
||||
self::$instances[$connection] = new Db($connection, $config);
|
||||
self::$instances[$connection] = new Connection($connection, $config);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function getInstance($connection="")
|
||||
public static function getInstance($connection = "")
|
||||
{
|
||||
if($connection=="" && count(self::$instances)>0)
|
||||
{
|
||||
if ($connection == "" && count(self::$instances) > 0) {
|
||||
$connection = array_keys(self::$instances)[0];
|
||||
}
|
||||
if (isset(self::$instances[$connection])) {
|
||||
$instance = self::$instances[$connection];
|
||||
if ($instance instanceof Db) {
|
||||
if ($instance instanceof Connection) {
|
||||
return self::$instances[$connection];
|
||||
}
|
||||
}
|
||||
@@ -194,4 +230,14 @@ class Db
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
public function getPdo()
|
||||
{
|
||||
return $this->pdo;
|
||||
}
|
||||
|
||||
public function getDriverType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
}
|
||||
@@ -2,24 +2,31 @@
|
||||
|
||||
namespace Kletellier\PdoWrapper;
|
||||
|
||||
class Model
|
||||
use JsonSerializable;
|
||||
|
||||
class Model implements JsonSerializable
|
||||
{
|
||||
protected mixed $values;
|
||||
protected array $updated;
|
||||
protected string $table;
|
||||
protected string $table = "";
|
||||
protected string $connection = "";
|
||||
protected string $pk;
|
||||
private ?Db $db;
|
||||
private bool $new;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->db = null;
|
||||
$this->new = true;
|
||||
$this->values = array();
|
||||
$this->updated = array();
|
||||
$this->pk = "id";
|
||||
$this->table = $this->getDefaultTableName();
|
||||
$this->initTable();
|
||||
}
|
||||
|
||||
private function initTable(): void
|
||||
{
|
||||
if ($this->table == "") {
|
||||
$this->table = $this->getDefaultTableName();
|
||||
}
|
||||
}
|
||||
|
||||
public function getTable(): string
|
||||
@@ -33,6 +40,11 @@ class Model
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getValues(): array
|
||||
{
|
||||
return $this->values;
|
||||
}
|
||||
|
||||
public function getPk(): string
|
||||
{
|
||||
return $this->pk;
|
||||
@@ -63,14 +75,24 @@ class Model
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function find(mixed $key): mixed
|
||||
{
|
||||
$qb = $this->newQueryBuilder();
|
||||
$find = $qb->find($key);
|
||||
if ($find !== null) {
|
||||
$this->fillData($find->getValues());
|
||||
return $this;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function save(): bool
|
||||
{
|
||||
$this->db = Db::getInstance($this->connection);
|
||||
$ret = false;
|
||||
$qb = $this->newQueryBuilder();
|
||||
if ($this->new) {
|
||||
$query = $qb->getPreparedQuery($this->values);
|
||||
$id = $this->db->insertQuery($query);
|
||||
$id = Connection::getInstance($this->connection)->insertQuery($query);
|
||||
if ($id !== false) {
|
||||
$this->values[$this->pk] = $id;
|
||||
$this->new = false;
|
||||
@@ -85,7 +107,7 @@ class Model
|
||||
$data[$updatecol] = $this->values[$updatecol];
|
||||
}
|
||||
$query = $qb->getPreparedQuery($data, false);
|
||||
$ret = $this->db->updateQuery($query);
|
||||
$ret = Connection::getInstance($this->connection)->updateQuery($query);
|
||||
if ($ret) {
|
||||
$this->updated = [];
|
||||
}
|
||||
@@ -108,6 +130,21 @@ class Model
|
||||
}
|
||||
}
|
||||
|
||||
public function __isset($name): bool
|
||||
{
|
||||
if (method_exists($this, $name)) {
|
||||
return true;
|
||||
} else {
|
||||
return isset($this->values[$name]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function q(): QueryBuilder
|
||||
{
|
||||
$ist = new static();
|
||||
return $ist->newQueryBuilder();
|
||||
}
|
||||
|
||||
public function __get($name): mixed
|
||||
{
|
||||
if (isset($this->values[$name])) {
|
||||
@@ -125,4 +162,9 @@ class Model
|
||||
|
||||
return $table;
|
||||
}
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return $this->values;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ namespace Kletellier\PdoWrapper;
|
||||
|
||||
class QueryBuilder
|
||||
{
|
||||
protected ?Db $db;
|
||||
protected array $columns;
|
||||
protected array $wheres;
|
||||
protected array $orders;
|
||||
@@ -22,7 +21,6 @@ class QueryBuilder
|
||||
$this->table = $table;
|
||||
$this->pk = $pk;
|
||||
$this->connection = $connection;
|
||||
$this->db = null;
|
||||
$this->reset();
|
||||
}
|
||||
|
||||
@@ -197,8 +195,7 @@ class QueryBuilder
|
||||
|
||||
private function getData(): mixed
|
||||
{
|
||||
$this->db = Db::getInstance($this->connection);
|
||||
return $this->db->getSelectQuery($this->getQuery());
|
||||
return Connection::getInstance($this->connection)->getSelectQuery($this->getQuery());
|
||||
}
|
||||
|
||||
private function getConstructedQuery(): Query
|
||||
|
||||
@@ -50,5 +50,29 @@ test("collection", function () {
|
||||
return $item + $carry;
|
||||
},$init);
|
||||
expect($reduce)->toBe(55);
|
||||
|
||||
$array = $coll->toArray();
|
||||
expect($array)->toBeArray();
|
||||
expect(count($array))->toBe(11);
|
||||
|
||||
$obj = new \stdClass();
|
||||
$obj->key = "test";
|
||||
$obj->val = 1;
|
||||
|
||||
$obj2 = new \stdClass();
|
||||
$obj2->key = "test2";
|
||||
$obj2->val = 2;
|
||||
|
||||
$collo = new Collection([$obj,$obj2]);
|
||||
expect($collo)->toBeInstanceOf(Collection::class);
|
||||
expect(count($collo))->toBe(2);
|
||||
|
||||
$json = json_encode($collo);
|
||||
expect($json)->toBeJson();
|
||||
|
||||
$pluck = $collo->pluck("key");
|
||||
expect($pluck)->toBeInstanceOf(Collection::class);
|
||||
expect(count($pluck))->toBe(2);
|
||||
expect($pluck->first())->toBe("test");
|
||||
});
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
<?php
|
||||
|
||||
use Kletellier\PdoWrapper\Collection;
|
||||
use Kletellier\PdoWrapper\Db;
|
||||
use Kletellier\PdoWrapper\Connection;
|
||||
use Kletellier\PdoWrapper\EventDispatcher;
|
||||
use Kletellier\PdoWrapper\Model;
|
||||
use Kletellier\PdoWrapper\QueryBuilder;
|
||||
|
||||
test("create db",function () {
|
||||
$config = ["conone" => ["TYPE"=>"sqlite", "DATABASE" => ":memory:" ], "contwo"=>["TYPE"=>"sqlite", "DATABASE" => ":memory:" ]];
|
||||
$db = Db::init($config,["conone","contwo"]);
|
||||
$db = Connection::init($config,["conone","contwo"]);
|
||||
$ret = $db->executeRawQuery("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, col1 VARCHAR, date1 DATETIME);");
|
||||
expect($ret)->toBe(true);
|
||||
|
||||
$db2 = Db::getInstance("contwo");
|
||||
$db2 = Connection::getInstance("contwo");
|
||||
$ret = $db2->executeRawQuery("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, col1 VARCHAR, date1 DATETIME);");
|
||||
expect($ret)->toBe(true);
|
||||
});
|
||||
@@ -35,6 +35,32 @@ test("insert row",function () {
|
||||
expect($ret2)->toBe(true);
|
||||
});
|
||||
|
||||
test("model find", function() {
|
||||
$row = new Model();
|
||||
$row->setTable("test");
|
||||
|
||||
$res = $row->find(1);
|
||||
expect($res->id)->toBe(1);
|
||||
expect($row->col1)->toBe("test");
|
||||
|
||||
$res = $row->find(15);
|
||||
expect($res)->toBe(null);
|
||||
|
||||
$row->col1 = "test2";
|
||||
$res = $row->save();
|
||||
expect($res)->toBe(true);
|
||||
|
||||
$row->col1 = "test";
|
||||
$res = $row->save();
|
||||
expect($res)->toBe(true);
|
||||
|
||||
$json = json_encode($row);
|
||||
expect($json)->toBeJson();
|
||||
|
||||
$obj = json_decode($json);
|
||||
expect($obj->col1)->toBe("test");
|
||||
});
|
||||
|
||||
test("querybuilder", function() {
|
||||
$qb = new QueryBuilder("test");
|
||||
$row = $qb->find(1);
|
||||
@@ -74,6 +100,20 @@ test("querybuilder", function() {
|
||||
expect(count($rows))->toBe(2);
|
||||
});
|
||||
|
||||
test("extending model",function()
|
||||
{
|
||||
$class = "Test";
|
||||
$code = "class $class extends \Kletellier\PdoWrapper\Model {}";
|
||||
eval($code);
|
||||
|
||||
$qbs = Test::q();
|
||||
expect($qbs)->toBeInstanceOf(QueryBuilder::class);
|
||||
|
||||
$row = $qbs->find(1);
|
||||
expect($row->id)->toBe(1);
|
||||
expect($row->col1)->toBe("test");
|
||||
});
|
||||
|
||||
test("order by", function(){
|
||||
$qb = new QueryBuilder("test");
|
||||
$rows = $qb->orderBy("id","DESC")->get();
|
||||
@@ -99,7 +139,18 @@ test("group by", function(){
|
||||
expect($rows->first()->Nb)->toBe(1);
|
||||
});
|
||||
|
||||
test("isset",function()
|
||||
{
|
||||
$q = new QueryBuilder("test");
|
||||
$row = $q->where("id",1)->get()->first();
|
||||
|
||||
expect($row->id)->toBe(1);
|
||||
expect($row->col1)->toBe("test");
|
||||
|
||||
expect(isset($row->col1))->toBe(true);
|
||||
expect(isset($row->col2))->toBe(false);
|
||||
expect(isset($row->find))->toBe(true);
|
||||
});
|
||||
|
||||
test("update row", function() {
|
||||
$qb = new QueryBuilder("test","id");
|
||||
|
||||
Reference in New Issue
Block a user