From 909b0f2c34f40bf12748f746bce211a94d8031fd Mon Sep 17 00:00:00 2001 From: Joao Gilberto Magalhaes Date: Sat, 11 Apr 2020 01:01:27 -0500 Subject: [PATCH 1/6] Change constructor to receive the dbdriver object --- .travis.yml | 1 + src/UsersDBDataset.php | 15 ++++++++------- tests/UsersDBDataset2ByUserNameTest.php | 10 ++++++---- tests/UsersDBDatasetByUsernameTest.php | 12 +++++++----- tests/UsersDBDatasetDefinitionTest.php | 12 +++++++----- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/.travis.yml b/.travis.yml index 29ddb6d..b19cfcf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: php php: + - "7.3" - "7.2" - "7.1" - "7.0" diff --git a/src/UsersDBDataset.php b/src/UsersDBDataset.php index 34c26ea..33df4ff 100644 --- a/src/UsersDBDataset.php +++ b/src/UsersDBDataset.php @@ -3,6 +3,7 @@ namespace ByJG\Authenticate; use ByJG\AnyDataset\Core\IteratorFilter; +use ByJG\AnyDataset\Db\DbDriverInterface; use ByJG\AnyDataset\Db\Factory; use ByJG\AnyDataset\Db\IteratorFilterSqlFormatter; use ByJG\Authenticate\Definition\UserDefinition; @@ -35,15 +36,16 @@ class UsersDBDataset extends UsersBase /** * UsersDBDataset constructor * - * @param string $connectionString - * @param UserDefinition $userTable - * @param UserPropertiesDefinition $propertiesTable + * @param \ByJG\AnyDataset\Db\DbDriverInterface $dbDriver + * @param \ByJG\Authenticate\Definition\UserDefinition $userTable + * @param \ByJG\Authenticate\Definition\UserPropertiesDefinition $propertiesTable + * * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException * @throws \ByJG\MicroOrm\Exception\OrmModelInvalidException * @throws \ByJG\Serializer\Exception\InvalidArgumentException */ public function __construct( - $connectionString, + $dbDriver, UserDefinition $userTable = null, UserPropertiesDefinition $propertiesTable = null ) { @@ -55,7 +57,6 @@ public function __construct( $propertiesTable = new UserPropertiesDefinition(); } - $provider = Factory::getDbRelationalInstance($connectionString); $userMapper = new Mapper( $userTable->model(), $userTable->table(), @@ -72,7 +73,7 @@ public function __construct( $userTable->getClosureForSelect($property) ); } - $this->userRepository = new Repository($provider, $userMapper); + $this->userRepository = new Repository($dbDriver, $userMapper); $propertiesMapper = new Mapper( UserPropertiesModel::class, @@ -83,7 +84,7 @@ public function __construct( $propertiesMapper->addFieldMap('name', $propertiesTable->getName()); $propertiesMapper->addFieldMap('value', $propertiesTable->getValue()); $propertiesMapper->addFieldMap('userid', $propertiesTable->getUserid()); - $this->propertiesRepository = new Repository($provider, $propertiesMapper); + $this->propertiesRepository = new Repository($dbDriver, $propertiesMapper); $this->userTable = $userTable; $this->propertiesTable = $propertiesTable; diff --git a/tests/UsersDBDataset2ByUserNameTest.php b/tests/UsersDBDataset2ByUserNameTest.php index 820946d..9d4e096 100644 --- a/tests/UsersDBDataset2ByUserNameTest.php +++ b/tests/UsersDBDataset2ByUserNameTest.php @@ -11,12 +11,14 @@ class UsersDBDataset2ByUserNameTest extends UsersDBDatasetByUsernameTest { + protected $db; + public function __setUp($loginField) { $this->prefix = ""; - $db = Factory::getDbRelationalInstance(self::CONNECTION_STRING); - $db->execute('create table mytable ( + $this->db = Factory::getDbRelationalInstance(self::CONNECTION_STRING); + $this->db->execute('create table mytable ( myuserid integer primary key autoincrement, myname varchar(45), myemail varchar(200), @@ -26,7 +28,7 @@ public function __setUp($loginField) myadmin char(1));' ); - $db->execute('create table theirproperty ( + $this->db->execute('create table theirproperty ( theirid integer primary key autoincrement, theiruserid integer, theirname varchar(45), @@ -51,7 +53,7 @@ public function __setUp($loginField) $this->propertyDefinition = new UserPropertiesDefinition('theirproperty', 'theirid', 'theirname', 'theirvalue', 'theiruserid'); $this->object = new UsersDBDataset( - self::CONNECTION_STRING, + $this->db, $this->userDefinition, $this->propertyDefinition ); diff --git a/tests/UsersDBDatasetByUsernameTest.php b/tests/UsersDBDatasetByUsernameTest.php index 93286b3..7d9854b 100644 --- a/tests/UsersDBDatasetByUsernameTest.php +++ b/tests/UsersDBDatasetByUsernameTest.php @@ -15,12 +15,14 @@ class UsersDBDatasetByUsernameTest extends UsersAnyDatasetByUsernameTest const CONNECTION_STRING='sqlite:///tmp/teste.db'; + protected $db; + public function __setUp($loginField) { $this->prefix = ""; - $db = Factory::getDbRelationalInstance(self::CONNECTION_STRING); - $db->execute('create table users ( + $this->db = Factory::getDbRelationalInstance(self::CONNECTION_STRING); + $this->db->execute('create table users ( userid integer primary key autoincrement, name varchar(45), email varchar(200), @@ -30,7 +32,7 @@ public function __setUp($loginField) admin char(1));' ); - $db->execute('create table users_property ( + $this->db->execute('create table users_property ( id integer primary key autoincrement, userid integer, name varchar(45), @@ -40,7 +42,7 @@ public function __setUp($loginField) $this->userDefinition = new UserDefinition('users', UserModel::class, $loginField); $this->propertyDefinition = new UserPropertiesDefinition(); $this->object = new UsersDBDataset( - self::CONNECTION_STRING, + $this->db, $this->userDefinition, $this->propertyDefinition ); @@ -126,7 +128,7 @@ public function testWithUpdateValue() // Test it! $newObject = new UsersDBDataset( - self::CONNECTION_STRING, + $this->db, $this->userDefinition, $this->propertyDefinition ); diff --git a/tests/UsersDBDatasetDefinitionTest.php b/tests/UsersDBDatasetDefinitionTest.php index 96e7bfb..3e060f6 100644 --- a/tests/UsersDBDatasetDefinitionTest.php +++ b/tests/UsersDBDatasetDefinitionTest.php @@ -32,6 +32,8 @@ public function setOtherfield($otherfield) class UsersDBDatasetDefinitionTest extends UsersDBDatasetByUsernameTest { + protected $db; + /** * @param $loginField * @throws \ByJG\AnyDataset\Exception\NotFoundException @@ -43,8 +45,8 @@ public function __setUp($loginField) { $this->prefix = ""; - $db = Factory::getDbRelationalInstance(self::CONNECTION_STRING); - $db->execute('create table mytable ( + $this->db = Factory::getDbRelationalInstance(self::CONNECTION_STRING); + $this->db->execute('create table mytable ( myuserid integer primary key autoincrement, myname varchar(45), myemail varchar(200), @@ -55,7 +57,7 @@ public function __setUp($loginField) myadmin char(1));' ); - $db->execute('create table theirproperty ( + $this->db->execute('create table theirproperty ( theirid integer primary key autoincrement, theiruserid integer, theirname varchar(45), @@ -81,7 +83,7 @@ public function __setUp($loginField) $this->propertyDefinition = new UserPropertiesDefinition('theirproperty', 'theirid', 'theirname', 'theirvalue', 'theiruserid'); $this->object = new UsersDBDataset( - self::CONNECTION_STRING, + $this->db, $this->userDefinition, $this->propertyDefinition ); @@ -180,7 +182,7 @@ public function testWithUpdateValue() // Test it! $newObject = new UsersDBDataset( - self::CONNECTION_STRING, + $this->db, $this->userDefinition, $this->propertyDefinition ); From 7ac5214fa2a0b7a8404ab703c6134385c3b01350 Mon Sep 17 00:00:00 2001 From: Joao Gilberto Magalhaes Date: Sat, 11 Apr 2020 12:09:21 -0500 Subject: [PATCH 2/6] Changed constructors interface --- src/UsersAnyDataset.php | 20 +++++++------------- src/UsersDBDataset.php | 2 +- src/UsersMoodleDataset.php | 7 ++++--- tests/UsersAnyDataset2ByUsernameTest.php | 4 +++- tests/UsersAnyDatasetByUsernameTest.php | 4 +++- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/UsersAnyDataset.php b/src/UsersAnyDataset.php index 50916ce..43b64fa 100644 --- a/src/UsersAnyDataset.php +++ b/src/UsersAnyDataset.php @@ -21,29 +21,23 @@ class UsersAnyDataset extends UsersBase */ protected $anyDataSet; - /** - * Internal Users file name - * - * @var string - */ - protected $usersFile; - /** * AnyDataset constructor * - * @param string $file + * @param AnyDataset $anyDataset * @param UserDefinition $userTable * @param UserPropertiesDefinition $propertiesTable + * @throws \ByJG\AnyDataset\Core\Exception\DatabaseException * @throws \ByJG\Serializer\Exception\InvalidArgumentException * @throws \ByJG\Util\Exception\XmlUtilException */ public function __construct( - $file, + AnyDataset $anyDataset, UserDefinition $userTable = null, UserPropertiesDefinition $propertiesTable = null ) { - $this->usersFile = $file; - $this->anyDataSet = new AnyDataset($this->usersFile); + $this->anyDataSet = $anyDataset; + $this->anyDataSet->save(); $this->userTable = $userTable; if (!$userTable->existsClosure('update', 'userid')) { $userTable->defineClosureForUpdate('userid', function ($value, $instance) { @@ -90,7 +84,7 @@ public function save(UserModel $model) $this->anyDataSet->addField($value->getName(), $value->getValue()); } - $this->anyDataSet->save($this->usersFile); + $this->anyDataSet->save(); } /** @@ -131,7 +125,7 @@ public function removeByLoginField($login) if ($iterator->hasNext()) { $oldRow = $iterator->moveNext(); $this->anyDataSet->removeRow($oldRow); - $this->anyDataSet->save($this->usersFile); + $this->anyDataSet->save(); return true; } diff --git a/src/UsersDBDataset.php b/src/UsersDBDataset.php index 33df4ff..71af0fd 100644 --- a/src/UsersDBDataset.php +++ b/src/UsersDBDataset.php @@ -45,7 +45,7 @@ class UsersDBDataset extends UsersBase * @throws \ByJG\Serializer\Exception\InvalidArgumentException */ public function __construct( - $dbDriver, + DbDriverInterface $dbDriver, UserDefinition $userTable = null, UserPropertiesDefinition $propertiesTable = null ) { diff --git a/src/UsersMoodleDataset.php b/src/UsersMoodleDataset.php index b72b4de..14b251b 100644 --- a/src/UsersMoodleDataset.php +++ b/src/UsersMoodleDataset.php @@ -7,6 +7,7 @@ */ define('AUTH_PASSWORD_NOT_CACHED', 'not cached'); // String used in password field when password is not stored. +use ByJG\AnyDataset\Db\DbDriverInterface; use ByJG\Authenticate\Definition\UserDefinition; use ByJG\Authenticate\Definition\UserPropertiesDefinition; use ByJG\Authenticate\Exception\NotImplementedException; @@ -25,15 +26,15 @@ class UsersMoodleDataset extends UsersDBDataset /** * DBDataset constructor * - * @param string $connectionString + * @param DbDriverInterface $dbDriver * @param string $siteSalt * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException * @throws \ByJG\MicroOrm\Exception\OrmModelInvalidException * @throws \ByJG\Serializer\Exception\InvalidArgumentException */ - public function __construct($connectionString, $siteSalt = "") + public function __construct(DbDriverInterface $dbDriver, $siteSalt = "") { - parent::__construct($connectionString); + parent::__construct($dbDriver); $this->siteSalt = $siteSalt; } diff --git a/tests/UsersAnyDataset2ByUsernameTest.php b/tests/UsersAnyDataset2ByUsernameTest.php index f584f3f..655dd3e 100644 --- a/tests/UsersAnyDataset2ByUsernameTest.php +++ b/tests/UsersAnyDataset2ByUsernameTest.php @@ -2,6 +2,7 @@ namespace ByJG\Authenticate; +use ByJG\AnyDataset\Core\AnyDataset; use ByJG\Authenticate\Definition\UserDefinition; use ByJG\Authenticate\Definition\UserPropertiesDefinition; use ByJG\Authenticate\Model\UserModel; @@ -38,8 +39,9 @@ public function __setUp($loginField) 'theiruserid' ); + $anydataset = new AnyDataset('php://memory'); $this->object = new UsersAnyDataset( - 'php://memory', + $anydataset, $this->userDefinition, $this->propertyDefinition ); diff --git a/tests/UsersAnyDatasetByUsernameTest.php b/tests/UsersAnyDatasetByUsernameTest.php index 4deb707..679ff5b 100644 --- a/tests/UsersAnyDatasetByUsernameTest.php +++ b/tests/UsersAnyDatasetByUsernameTest.php @@ -2,6 +2,7 @@ namespace ByJG\Authenticate; +use ByJG\AnyDataset\Core\AnyDataset; use ByJG\Authenticate\Definition\UserDefinition; use ByJG\Authenticate\Definition\UserPropertiesDefinition; use ByJG\Authenticate\Model\UserModel; @@ -36,8 +37,9 @@ public function __setUp($loginField) $this->userDefinition = new UserDefinition('users', UserModel::class, $loginField); $this->propertyDefinition = new UserPropertiesDefinition(); + $anydataSet = new AnyDataset('php://memory'); $this->object = new UsersAnyDataset( - 'php://memory', + $anydataSet, $this->userDefinition, $this->propertyDefinition ); From 599802173b2923b3f88b5e4f61bd7b0d720b8625 Mon Sep 17 00:00:00 2001 From: Joao Gilberto Magalhaes Date: Wed, 22 Apr 2020 23:32:04 -0500 Subject: [PATCH 3/6] Changed how to get the properties from Database --- src/UsersDBDataset.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/UsersDBDataset.php b/src/UsersDBDataset.php index 71af0fd..2dae71d 100644 --- a/src/UsersDBDataset.php +++ b/src/UsersDBDataset.php @@ -298,6 +298,29 @@ public function removeAllProperties($propertyName, $value = null) return true; } + public function getProperty($userId, $propertyName) + { + $query = Query::getInstance() + ->table($this->getUserPropertiesDefinition()->table()) + ->where("{$this->getUserPropertiesDefinition()->getUserid()} = :id", ['id' =>$userId]) + ->where("{$this->getUserPropertiesDefinition()->getName()} = :name", ['name' =>$propertyName]); + + $result = []; + foreach ($this->propertiesRepository->getByQuery($query) as $model) { + $result[] = $model->getValue(); + } + + if (count($result) === 0) { + return null; + } + + if (count($result) === 1) { + return $result[0]; + } + + return $result; + } + /** * Return all property's fields from this user * From 1525d9481220128d005316ca205efbdd96a73e30 Mon Sep 17 00:00:00 2001 From: Joao Gilberto Magalhaes Date: Sun, 26 Apr 2020 23:27:59 -0500 Subject: [PATCH 4/6] Upgrade Anydataset Version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2f98e3a..9baa53c 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "prefer-stable": true, "require": { "php": ">=5.6.0", - "byjg/micro-orm": "4.0.*", + "byjg/micro-orm": "4.1.0.x-dev", "byjg/cache-engine": "4.0.*", "byjg/jwt-wrapper": "2.0.*" }, From 48d2d21b94cd7128be4421a6723213b15e8cec68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joao=20Gilberto=20Magalh=C3=A3es?= Date: Sat, 11 Jul 2020 00:57:38 -0500 Subject: [PATCH 5/6] Upgrade Micro-Orm Version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9baa53c..1c2966e 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "prefer-stable": true, "require": { "php": ">=5.6.0", - "byjg/micro-orm": "4.1.0.x-dev", + "byjg/micro-orm": "4.1.*", "byjg/cache-engine": "4.0.*", "byjg/jwt-wrapper": "2.0.*" }, From 7a0956af1c00d8adba0930f6390dc95dd0601a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joao=20Gilberto=20Magalh=C3=A3es?= Date: Sat, 11 Jul 2020 01:05:54 -0500 Subject: [PATCH 6/6] Adjusts on tests --- tests/UsersAnyDatasetByUsernameTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/UsersAnyDatasetByUsernameTest.php b/tests/UsersAnyDatasetByUsernameTest.php index 679ff5b..403ab69 100644 --- a/tests/UsersAnyDatasetByUsernameTest.php +++ b/tests/UsersAnyDatasetByUsernameTest.php @@ -85,6 +85,10 @@ public function testAddUserError() public function testAddProperty() { + // Check state + $user = $this->object->getById($this->prefix . '2'); + $this->assertEmpty($user->get('city')); + // Add one property $this->object->addProperty($this->prefix . '2', 'city', 'Rio de Janeiro'); $user = $this->object->getById($this->prefix . '2');