1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace DrMVC\Orm; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
class Test extends TestCase |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
private $pdo; |
12
|
|
|
|
13
|
|
|
private $orm; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Test constructor. |
17
|
|
|
* @param string|null $name |
18
|
|
|
* @param array $data |
19
|
|
|
* @param string $dataName |
20
|
|
|
*/ |
21
|
|
|
public function __construct(string $name = null, array $data = [], string $dataName = '') |
22
|
|
|
{ |
23
|
|
|
parent::__construct($name, $data, $dataName); |
24
|
|
|
$sql = 'CREATE TABLE IF NOT EXISTS `test` ( |
25
|
|
|
`id` INTEGER PRIMARY KEY, |
26
|
|
|
`name` varchar(255), |
27
|
|
|
`email` varchar(255), |
28
|
|
|
`password` varchar(32) |
29
|
|
|
)'; |
30
|
|
|
$this->getPDO()->exec($sql); |
31
|
|
|
$this->orm = new Orm('test', $this->getPDO()); |
32
|
|
|
$this->assertInstanceOf(Orm::class, $this->orm); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getPDO(): PDO |
36
|
|
|
{ |
37
|
|
|
if (!$this->pdo) { |
38
|
|
|
$pdo = new PDO('sqlite:' . __DIR__ . '/test.db'); |
39
|
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
40
|
|
|
$this->pdo = $pdo; |
41
|
|
|
} |
42
|
|
|
return $this->pdo; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function test_insert() |
46
|
|
|
{ |
47
|
|
|
$entity = new Entity(); |
48
|
|
|
|
49
|
|
|
$entity->id = null; |
50
|
|
|
$entity->setName('Kolya'); |
|
|
|
|
51
|
|
|
$entity->email = 'qweqwe'; |
|
|
|
|
52
|
|
|
$entity->setPassword('qwerty'); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$count = $this->orm->saveEntity($entity); |
55
|
|
|
$this->assertEquals(1, $count->rowCount()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function test_getById() |
59
|
|
|
{ |
60
|
|
|
$entity = $this->orm->findById(1); |
61
|
|
|
$this->assertInstanceOf(Entity::class, $entity); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function test_findAll() |
65
|
|
|
{ |
66
|
|
|
$entity = $this->orm->findAll(); |
67
|
|
|
$this->assertInternalType('array', $entity); |
68
|
|
|
$this->assertInstanceOf(Entity::class, $entity[0]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function test_delete() |
72
|
|
|
{ |
73
|
|
|
$entity = $this->orm->findById(1); |
74
|
|
|
$this->assertEquals(1, $this->orm->deleteEntity($entity)); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: