Repository navigation
Expand file tree
/
Copy pathManagerTest.php
More file actions
173 lines (151 loc) · 6.73 KB
/
Copy pathManagerTest.php
File metadata and controls
173 lines (151 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\ModelCache;
use Hyperf\Config\Config;
use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Database\Schema\Column;
use Hyperf\DbConnection\Collector\TableCollector;
use Hyperf\ModelCache;
use Hyperf\ModelCache\Handler\HandlerInterface;
use Hyperf\Support\Reflection\ClassInvoker;
use HyperfTest\ModelCache\Stub\ManagerStub;
use HyperfTest\ModelCache\Stub\ModelStub;
use HyperfTest\ModelCache\Stub\NonHandler;
use HyperfTest\ModelCache\Stub\StdoutLogger;
use Mockery;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use function Hyperf\Coroutine\parallel;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class ManagerTest extends TestCase
{
protected function tearDown(): void
{
Mockery::close();
}
public function testFormatModel()
{
$container = Mockery::mock(ContainerInterface::class);
$container->shouldReceive('get')->once()->with(StdoutLoggerInterface::class)->andReturn(new StdoutLogger());
$container->shouldReceive('get')->once()->with(ConfigInterface::class)->andReturn(new Config($this->getConfig()));
$container->shouldReceive('make')->with(ContainerInterface::class)->andReturn($container);
$container->shouldReceive('get')->with(EventDispatcherInterface::class)->andReturn(null);
$container->shouldReceive('get')->with(TableCollector::class)->andReturn(new TableCollector());
ApplicationContext::setContainer($container);
$manager = new ManagerStub($container);
$model = new ModelStub();
$json = ['id' => 1, 'name' => 'Hyperf'];
$model->fill(['id' => 1, 'json_data' => $json, 'str' => null, 'float_num' => 0.1]);
$this->assertSame(['id' => 1, 'json_data' => json_encode($json), 'str' => null, 'float_num' => 0.1], $model->getAttributes());
$this->assertSame(['id' => 1, 'json_data' => ['id' => 1, 'name' => 'Hyperf'], 'str' => null, 'float_num' => 0.1], $model->toArray());
$res = $manager->formatModel($model);
$this->assertSame(['id' => 1, 'json_data' => json_encode($json), 'str' => null, 'float_num' => 0.1], $res);
}
public function testGetCacheTTL()
{
$container = Mockery::mock(ContainerInterface::class);
$container->shouldReceive('get')->once()->with(StdoutLoggerInterface::class)->andReturn(new StdoutLogger());
$container->shouldReceive('get')->once()->with(ConfigInterface::class)->andReturn(new Config($this->getConfig()));
$container->shouldReceive('make')->with(ContainerInterface::class)->andReturn($container);
$container->shouldReceive('get')->with(EventDispatcherInterface::class)->andReturn(null);
$container->shouldReceive('get')->with(TableCollector::class)->andReturn(new TableCollector());
ApplicationContext::setContainer($container);
$handler = Mockery::mock(HandlerInterface::class);
$handler->shouldReceive('getConfig')->andReturnUsing(function () {
return new ModelCache\Config([
'ttl' => 1000,
], 'default');
});
$manager = new ManagerStub($container);
$model = new ModelStub();
$this->assertSame(1000, $manager->getCacheTTL($model, $handler));
$model = new class extends ModelStub implements ModelCache\CacheableInterface {
use ModelCache\Cacheable;
public function getCacheTTL(): ?int
{
return 100;
}
};
$this->assertSame(100, $manager->getCacheTTL($model, $handler));
}
public function testInvalidCacheManager()
{
parallel([static function () {
$manager = ModelCache\InvalidCacheManager::instance();
$model = Mockery::mock(ModelCache\CacheableInterface::class);
$model->shouldReceive('deleteCache')->once()->andReturn(true);
$manager->push($model);
$manager->delete();
$manager->delete();
}]);
$this->assertInstanceOf(ModelCache\InvalidCacheManager::class, ModelCache\InvalidCacheManager::instance());
}
public function testGetAttributes()
{
$container = Mockery::mock(ContainerInterface::class);
$container->shouldReceive('get')->with(StdoutLoggerInterface::class)->andReturn(new StdoutLogger());
$container->shouldReceive('get')->with(ConfigInterface::class)->andReturn(new Config([
'databases' => [
'default' => [
'prefix' => 'tb_',
'cache' => [
'handler' => NonHandler::class,
'cache_key' => 'mc:%s:m:%s:%s:%s',
'prefix' => 'default',
'pool' => 'default',
'ttl' => 3600 * 24,
'empty_model_ttl' => 3600,
'load_script' => true,
'use_default_value' => true,
],
],
],
]));
$container->shouldReceive('make')->with(ContainerInterface::class)->andReturn($container);
$container->shouldReceive('get')->with(EventDispatcherInterface::class)->andReturn(null);
$container->shouldReceive('get')->with(TableCollector::class)->andReturn($collector = new TableCollector());
$collector->add('default', new Column('', 'tb_model', 'str', 1, 'empty', true, 'varchar', ''));
ApplicationContext::setContainer($container);
$manager = new ClassInvoker(new ManagerStub($container));
$handler = $manager->handlers['default'];
$model = new class extends ModelStub {
protected ?string $table = 'model';
};
$data = $manager->getAttributes($handler->getConfig(), $model, ['id' => 1]);
$this->assertSame(['str' => 'empty', 'id' => 1], $data);
}
protected function getConfig(): array
{
return [
'databases' => [
'default' => [
'cache' => [
'handler' => NonHandler::class,
'cache_key' => 'mc:%s:m:%s:%s:%s',
'prefix' => 'default',
'pool' => 'default',
'ttl' => 3600 * 24,
'empty_model_ttl' => 3600,
'load_script' => true,
],
],
],
];
}
}