Skip to content

Commit 7d1ea9d

Browse files
authored
Merge pull request #6 from byjg/4.2.0
4.2.0
2 parents 7b249c5 + 7a0956a commit 7d1ea9d

10 files changed

+74
-40
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
language: php
22
php:
3+
- "7.3"
34
- "7.2"
45
- "7.1"
56
- "7.0"

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"prefer-stable": true,
1717
"require": {
1818
"php": ">=5.6.0",
19-
"byjg/micro-orm": "4.0.*",
19+
"byjg/micro-orm": "4.1.*",
2020
"byjg/cache-engine": "4.0.*",
2121
"byjg/jwt-wrapper": "2.0.*"
2222
},

src/UsersAnyDataset.php

+7-13
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,23 @@ class UsersAnyDataset extends UsersBase
2121
*/
2222
protected $anyDataSet;
2323

24-
/**
25-
* Internal Users file name
26-
*
27-
* @var string
28-
*/
29-
protected $usersFile;
30-
3124
/**
3225
* AnyDataset constructor
3326
*
34-
* @param string $file
27+
* @param AnyDataset $anyDataset
3528
* @param UserDefinition $userTable
3629
* @param UserPropertiesDefinition $propertiesTable
30+
* @throws \ByJG\AnyDataset\Core\Exception\DatabaseException
3731
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
3832
* @throws \ByJG\Util\Exception\XmlUtilException
3933
*/
4034
public function __construct(
41-
$file,
35+
AnyDataset $anyDataset,
4236
UserDefinition $userTable = null,
4337
UserPropertiesDefinition $propertiesTable = null
4438
) {
45-
$this->usersFile = $file;
46-
$this->anyDataSet = new AnyDataset($this->usersFile);
39+
$this->anyDataSet = $anyDataset;
40+
$this->anyDataSet->save();
4741
$this->userTable = $userTable;
4842
if (!$userTable->existsClosure('update', 'userid')) {
4943
$userTable->defineClosureForUpdate('userid', function ($value, $instance) {
@@ -90,7 +84,7 @@ public function save(UserModel $model)
9084
$this->anyDataSet->addField($value->getName(), $value->getValue());
9185
}
9286

93-
$this->anyDataSet->save($this->usersFile);
87+
$this->anyDataSet->save();
9488
}
9589

9690
/**
@@ -131,7 +125,7 @@ public function removeByLoginField($login)
131125
if ($iterator->hasNext()) {
132126
$oldRow = $iterator->moveNext();
133127
$this->anyDataSet->removeRow($oldRow);
134-
$this->anyDataSet->save($this->usersFile);
128+
$this->anyDataSet->save();
135129
return true;
136130
}
137131

src/UsersDBDataset.php

+31-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ByJG\Authenticate;
44

55
use ByJG\AnyDataset\Core\IteratorFilter;
6+
use ByJG\AnyDataset\Db\DbDriverInterface;
67
use ByJG\AnyDataset\Db\Factory;
78
use ByJG\AnyDataset\Db\IteratorFilterSqlFormatter;
89
use ByJG\Authenticate\Definition\UserDefinition;
@@ -35,15 +36,16 @@ class UsersDBDataset extends UsersBase
3536
/**
3637
* UsersDBDataset constructor
3738
*
38-
* @param string $connectionString
39-
* @param UserDefinition $userTable
40-
* @param UserPropertiesDefinition $propertiesTable
39+
* @param \ByJG\AnyDataset\Db\DbDriverInterface $dbDriver
40+
* @param \ByJG\Authenticate\Definition\UserDefinition $userTable
41+
* @param \ByJG\Authenticate\Definition\UserPropertiesDefinition $propertiesTable
42+
*
4143
* @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
4244
* @throws \ByJG\MicroOrm\Exception\OrmModelInvalidException
4345
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
4446
*/
4547
public function __construct(
46-
$connectionString,
48+
DbDriverInterface $dbDriver,
4749
UserDefinition $userTable = null,
4850
UserPropertiesDefinition $propertiesTable = null
4951
) {
@@ -55,7 +57,6 @@ public function __construct(
5557
$propertiesTable = new UserPropertiesDefinition();
5658
}
5759

58-
$provider = Factory::getDbRelationalInstance($connectionString);
5960
$userMapper = new Mapper(
6061
$userTable->model(),
6162
$userTable->table(),
@@ -72,7 +73,7 @@ public function __construct(
7273
$userTable->getClosureForSelect($property)
7374
);
7475
}
75-
$this->userRepository = new Repository($provider, $userMapper);
76+
$this->userRepository = new Repository($dbDriver, $userMapper);
7677

7778
$propertiesMapper = new Mapper(
7879
UserPropertiesModel::class,
@@ -83,7 +84,7 @@ public function __construct(
8384
$propertiesMapper->addFieldMap('name', $propertiesTable->getName());
8485
$propertiesMapper->addFieldMap('value', $propertiesTable->getValue());
8586
$propertiesMapper->addFieldMap('userid', $propertiesTable->getUserid());
86-
$this->propertiesRepository = new Repository($provider, $propertiesMapper);
87+
$this->propertiesRepository = new Repository($dbDriver, $propertiesMapper);
8788

8889
$this->userTable = $userTable;
8990
$this->propertiesTable = $propertiesTable;
@@ -297,6 +298,29 @@ public function removeAllProperties($propertyName, $value = null)
297298
return true;
298299
}
299300

301+
public function getProperty($userId, $propertyName)
302+
{
303+
$query = Query::getInstance()
304+
->table($this->getUserPropertiesDefinition()->table())
305+
->where("{$this->getUserPropertiesDefinition()->getUserid()} = :id", ['id' =>$userId])
306+
->where("{$this->getUserPropertiesDefinition()->getName()} = :name", ['name' =>$propertyName]);
307+
308+
$result = [];
309+
foreach ($this->propertiesRepository->getByQuery($query) as $model) {
310+
$result[] = $model->getValue();
311+
}
312+
313+
if (count($result) === 0) {
314+
return null;
315+
}
316+
317+
if (count($result) === 1) {
318+
return $result[0];
319+
}
320+
321+
return $result;
322+
}
323+
300324
/**
301325
* Return all property's fields from this user
302326
*

src/UsersMoodleDataset.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
define('AUTH_PASSWORD_NOT_CACHED', 'not cached'); // String used in password field when password is not stored.
99

10+
use ByJG\AnyDataset\Db\DbDriverInterface;
1011
use ByJG\Authenticate\Definition\UserDefinition;
1112
use ByJG\Authenticate\Definition\UserPropertiesDefinition;
1213
use ByJG\Authenticate\Exception\NotImplementedException;
@@ -25,15 +26,15 @@ class UsersMoodleDataset extends UsersDBDataset
2526
/**
2627
* DBDataset constructor
2728
*
28-
* @param string $connectionString
29+
* @param DbDriverInterface $dbDriver
2930
* @param string $siteSalt
3031
* @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
3132
* @throws \ByJG\MicroOrm\Exception\OrmModelInvalidException
3233
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
3334
*/
34-
public function __construct($connectionString, $siteSalt = "")
35+
public function __construct(DbDriverInterface $dbDriver, $siteSalt = "")
3536
{
36-
parent::__construct($connectionString);
37+
parent::__construct($dbDriver);
3738

3839
$this->siteSalt = $siteSalt;
3940
}

tests/UsersAnyDataset2ByUsernameTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ByJG\Authenticate;
44

5+
use ByJG\AnyDataset\Core\AnyDataset;
56
use ByJG\Authenticate\Definition\UserDefinition;
67
use ByJG\Authenticate\Definition\UserPropertiesDefinition;
78
use ByJG\Authenticate\Model\UserModel;
@@ -38,8 +39,9 @@ public function __setUp($loginField)
3839
'theiruserid'
3940
);
4041

42+
$anydataset = new AnyDataset('php://memory');
4143
$this->object = new UsersAnyDataset(
42-
'php://memory',
44+
$anydataset,
4345
$this->userDefinition,
4446
$this->propertyDefinition
4547
);

tests/UsersAnyDatasetByUsernameTest.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ByJG\Authenticate;
44

5+
use ByJG\AnyDataset\Core\AnyDataset;
56
use ByJG\Authenticate\Definition\UserDefinition;
67
use ByJG\Authenticate\Definition\UserPropertiesDefinition;
78
use ByJG\Authenticate\Model\UserModel;
@@ -36,8 +37,9 @@ public function __setUp($loginField)
3637
$this->userDefinition = new UserDefinition('users', UserModel::class, $loginField);
3738
$this->propertyDefinition = new UserPropertiesDefinition();
3839

40+
$anydataSet = new AnyDataset('php://memory');
3941
$this->object = new UsersAnyDataset(
40-
'php://memory',
42+
$anydataSet,
4143
$this->userDefinition,
4244
$this->propertyDefinition
4345
);
@@ -83,6 +85,10 @@ public function testAddUserError()
8385

8486
public function testAddProperty()
8587
{
88+
// Check state
89+
$user = $this->object->getById($this->prefix . '2');
90+
$this->assertEmpty($user->get('city'));
91+
8692
// Add one property
8793
$this->object->addProperty($this->prefix . '2', 'city', 'Rio de Janeiro');
8894
$user = $this->object->getById($this->prefix . '2');

tests/UsersDBDataset2ByUserNameTest.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

1212
class UsersDBDataset2ByUserNameTest extends UsersDBDatasetByUsernameTest
1313
{
14+
protected $db;
15+
1416
public function __setUp($loginField)
1517
{
1618
$this->prefix = "";
1719

18-
$db = Factory::getDbRelationalInstance(self::CONNECTION_STRING);
19-
$db->execute('create table mytable (
20+
$this->db = Factory::getDbRelationalInstance(self::CONNECTION_STRING);
21+
$this->db->execute('create table mytable (
2022
myuserid integer primary key autoincrement,
2123
myname varchar(45),
2224
myemail varchar(200),
@@ -26,7 +28,7 @@ public function __setUp($loginField)
2628
myadmin char(1));'
2729
);
2830

29-
$db->execute('create table theirproperty (
31+
$this->db->execute('create table theirproperty (
3032
theirid integer primary key autoincrement,
3133
theiruserid integer,
3234
theirname varchar(45),
@@ -51,7 +53,7 @@ public function __setUp($loginField)
5153
$this->propertyDefinition = new UserPropertiesDefinition('theirproperty', 'theirid', 'theirname', 'theirvalue', 'theiruserid');
5254

5355
$this->object = new UsersDBDataset(
54-
self::CONNECTION_STRING,
56+
$this->db,
5557
$this->userDefinition,
5658
$this->propertyDefinition
5759
);

tests/UsersDBDatasetByUsernameTest.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ class UsersDBDatasetByUsernameTest extends UsersAnyDatasetByUsernameTest
1515

1616
const CONNECTION_STRING='sqlite:///tmp/teste.db';
1717

18+
protected $db;
19+
1820
public function __setUp($loginField)
1921
{
2022
$this->prefix = "";
2123

22-
$db = Factory::getDbRelationalInstance(self::CONNECTION_STRING);
23-
$db->execute('create table users (
24+
$this->db = Factory::getDbRelationalInstance(self::CONNECTION_STRING);
25+
$this->db->execute('create table users (
2426
userid integer primary key autoincrement,
2527
name varchar(45),
2628
email varchar(200),
@@ -30,7 +32,7 @@ public function __setUp($loginField)
3032
admin char(1));'
3133
);
3234

33-
$db->execute('create table users_property (
35+
$this->db->execute('create table users_property (
3436
id integer primary key autoincrement,
3537
userid integer,
3638
name varchar(45),
@@ -40,7 +42,7 @@ public function __setUp($loginField)
4042
$this->userDefinition = new UserDefinition('users', UserModel::class, $loginField);
4143
$this->propertyDefinition = new UserPropertiesDefinition();
4244
$this->object = new UsersDBDataset(
43-
self::CONNECTION_STRING,
45+
$this->db,
4446
$this->userDefinition,
4547
$this->propertyDefinition
4648
);
@@ -126,7 +128,7 @@ public function testWithUpdateValue()
126128

127129
// Test it!
128130
$newObject = new UsersDBDataset(
129-
self::CONNECTION_STRING,
131+
$this->db,
130132
$this->userDefinition,
131133
$this->propertyDefinition
132134
);

tests/UsersDBDatasetDefinitionTest.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public function setOtherfield($otherfield)
3232

3333
class UsersDBDatasetDefinitionTest extends UsersDBDatasetByUsernameTest
3434
{
35+
protected $db;
36+
3537
/**
3638
* @param $loginField
3739
* @throws \ByJG\AnyDataset\Exception\NotFoundException
@@ -43,8 +45,8 @@ public function __setUp($loginField)
4345
{
4446
$this->prefix = "";
4547

46-
$db = Factory::getDbRelationalInstance(self::CONNECTION_STRING);
47-
$db->execute('create table mytable (
48+
$this->db = Factory::getDbRelationalInstance(self::CONNECTION_STRING);
49+
$this->db->execute('create table mytable (
4850
myuserid integer primary key autoincrement,
4951
myname varchar(45),
5052
myemail varchar(200),
@@ -55,7 +57,7 @@ public function __setUp($loginField)
5557
myadmin char(1));'
5658
);
5759

58-
$db->execute('create table theirproperty (
60+
$this->db->execute('create table theirproperty (
5961
theirid integer primary key autoincrement,
6062
theiruserid integer,
6163
theirname varchar(45),
@@ -81,7 +83,7 @@ public function __setUp($loginField)
8183
$this->propertyDefinition = new UserPropertiesDefinition('theirproperty', 'theirid', 'theirname', 'theirvalue', 'theiruserid');
8284

8385
$this->object = new UsersDBDataset(
84-
self::CONNECTION_STRING,
86+
$this->db,
8587
$this->userDefinition,
8688
$this->propertyDefinition
8789
);
@@ -180,7 +182,7 @@ public function testWithUpdateValue()
180182

181183
// Test it!
182184
$newObject = new UsersDBDataset(
183-
self::CONNECTION_STRING,
185+
$this->db,
184186
$this->userDefinition,
185187
$this->propertyDefinition
186188
);

0 commit comments

Comments
 (0)