|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace TZachi\PhalconRepository\Tests\Functional; |
|
6
|
|
|
|
|
7
|
|
|
use Phalcon\Annotations\AdapterInterface as AnnotationsAdapterInterface; |
|
8
|
|
|
use Phalcon\Annotations\Factory as AnnotationsFactory; |
|
9
|
|
|
use Phalcon\Db\Adapter\Pdo as PdoDbAdapter; |
|
10
|
|
|
use Phalcon\Db\Adapter\Pdo\Sqlite as SqliteDbAdapter; |
|
11
|
|
|
use Phalcon\Di; |
|
12
|
|
|
use Phalcon\Mvc\Model\Manager as ModelsManager; |
|
13
|
|
|
use Phalcon\Mvc\Model\MetaData\Memory; |
|
14
|
|
|
use Phalcon\Mvc\Model\MetaData\Strategy\Annotations as AnnotationsStrategy; |
|
15
|
|
|
use PHPUnit\Framework\TestCase as PHPUnitTestCase; |
|
16
|
|
|
use RuntimeException; |
|
17
|
|
|
use function dirname; |
|
18
|
|
|
use function sprintf; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Base test case for functional tests |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class TestCase extends PHPUnitTestCase |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var PdoDbAdapter|null |
|
27
|
|
|
*/ |
|
28
|
|
|
protected static $sharedConnection; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Di|null |
|
32
|
|
|
*/ |
|
33
|
|
|
protected static $sharedDi; |
|
34
|
|
|
|
|
35
|
|
|
protected static function setUpDi(): void |
|
36
|
|
|
{ |
|
37
|
|
|
if (self::$sharedConnection === null) { |
|
38
|
|
|
self::$sharedConnection = new SqliteDbAdapter(['dbname' => ':memory:']); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if (self::$sharedDi !== null) { |
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$di = new Di(); |
|
46
|
|
|
$di->setShared('db', self::$sharedConnection); |
|
47
|
|
|
$di->setShared( |
|
48
|
|
|
'modelsManager', |
|
49
|
|
|
function (): ModelsManager { |
|
50
|
|
|
return new ModelsManager(); |
|
51
|
|
|
} |
|
52
|
|
|
); |
|
53
|
|
|
$di->setShared( |
|
54
|
|
|
'annotations', |
|
55
|
|
|
function (): AnnotationsAdapterInterface { |
|
56
|
|
|
return AnnotationsFactory::load(['adapter' => 'memory']); |
|
57
|
|
|
} |
|
58
|
|
|
); |
|
59
|
|
|
$di->setShared( |
|
60
|
|
|
'modelsMetadata', |
|
61
|
|
|
function (): Memory { |
|
62
|
|
|
$metadata = new Memory(); |
|
63
|
|
|
$metadata->setStrategy(new AnnotationsStrategy()); |
|
64
|
|
|
|
|
65
|
|
|
return $metadata; |
|
66
|
|
|
} |
|
67
|
|
|
); |
|
68
|
|
|
Di::setDefault($di); |
|
69
|
|
|
|
|
70
|
|
|
self::$sharedDi = $di; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected static function resetModelsMetadata(): void |
|
74
|
|
|
{ |
|
75
|
|
|
if (self::$sharedDi === null) { |
|
76
|
|
|
throw new RuntimeException( |
|
77
|
|
|
sprintf('Please call %s::setUpDi before calling %s', self::class, __METHOD__) |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @var Memory $metadata |
|
83
|
|
|
*/ |
|
84
|
|
|
$metadata = self::$sharedDi->get('modelsMetadata'); |
|
85
|
|
|
$metadata->reset(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected static function resetTable(string $tableName): bool |
|
89
|
|
|
{ |
|
90
|
|
|
if (self::$sharedConnection === null) { |
|
91
|
|
|
throw new RuntimeException( |
|
92
|
|
|
sprintf('Please call %s::setUpDi before calling %s', self::class, __METHOD__) |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
self::$sharedConnection->dropTable($tableName); |
|
97
|
|
|
|
|
98
|
|
|
return self::$sharedConnection->createTable( |
|
99
|
|
|
$tableName, |
|
100
|
|
|
null, |
|
101
|
|
|
require dirname(__DIR__) . '/migrations/' . $tableName . '.php' |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|