Skip to content

Commit fd1541c

Browse files
committed
fix Request test
2 parents 39d8c14 + 6c2d12b commit fd1541c

File tree

9 files changed

+131
-23
lines changed

9 files changed

+131
-23
lines changed

src/Concern/HasAttributes.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ public function getAttribute(string $key): array
114114
return [$pro, $value];
115115
}
116116

117+
/**
118+
* Get an attribute value from the model.
119+
*
120+
* @param string $key
121+
* @return mixed
122+
* @throws DbException
123+
*/
124+
public function getAttributeValue(string $key)
125+
{
126+
return $this->getAttribute($key)[1];
127+
}
128+
117129
/**
118130
* Get an not hidden attribute from the model.
119131
*
@@ -390,7 +402,7 @@ public function only(array $attributes)
390402
$results = [];
391403

392404
foreach ($attributes as $attribute) {
393-
$results[$attribute] = $this->getAttribute($attribute);
405+
$results[$attribute] = $this->getAttributeValue($attribute);
394406
}
395407

396408
return $results;

src/Connection/Connection.php

+15-8
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public function initialize(Pool $pool, Database $database)
102102
{
103103
$this->pool = $pool;
104104
$this->database = $database;
105+
$this->lastTime = time();
105106

106107
// We need to initialize a query grammar and the query post processors
107108
// which are both very important parts of the database abstractions
@@ -177,6 +178,20 @@ public function create(): void
177178
$this->createReadPdo();
178179
}
179180

181+
/**
182+
* Close connection
183+
*/
184+
public function close(): void
185+
{
186+
if (!empty($this->pdo)) {
187+
$this->pdo = null;
188+
}
189+
190+
if (!empty($this->readPdo)) {
191+
$this->readPdo = null;
192+
}
193+
}
194+
180195
/**
181196
* Reconnect
182197
*/
@@ -213,14 +228,6 @@ public function release(bool $force = false): void
213228
}
214229
}
215230

216-
/**
217-
* @return int
218-
*/
219-
public function getLastTime(): int
220-
{
221-
return time();
222-
}
223-
224231
/**
225232
* @return Grammar
226233
*/

src/Eloquent/Builder.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ public function firstOr(array $columns = ['*'], Closure $callback = null)
566566
public function value(string $column)
567567
{
568568
if ($result = $this->first([$column])) {
569-
return $result->getAttribute($column)[1];
569+
return $result->getAttributeValue($column);
570570
}
571571

572572
return null;
@@ -666,7 +666,7 @@ public function chunkById(int $count, callable $callback, string $column = null,
666666

667667
/* @var Model $last */
668668
$last = $results->last();
669-
$lastId = $last->getAttribute($alias)[1];
669+
$lastId = $last->getAttributeValue($alias);
670670

671671
unset($results);
672672
} while ($countResults == $count);

src/Eloquent/Collection.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public function diff($items)
175175

176176
foreach ($this->items as $item) {
177177
/* @var Model $item */
178-
if (!isset($dictionary[$item->getKey()[1]])) {
178+
if (!isset($dictionary[$item->getKey()])) {
179179
$diff->add($item);
180180
}
181181
}
@@ -199,7 +199,7 @@ public function intersect($items)
199199

200200
foreach ($this->items as $item) {
201201
/* @var Model $item */
202-
if (isset($dictionary[$item->getKey()[1]])) {
202+
if (isset($dictionary[$item->getKey()])) {
203203
$intersect->add($item);
204204
}
205205
}

src/Eloquent/Model.php

+5-9
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ protected function incrementOrDecrement(string $column, $amount, array $extra, s
342342
$this->incrementOrDecrementAttributeValue($column, $amount, $extra, $method);
343343

344344
return $query->where(
345-
$this->getKeyName(), $this->getKey()[1]
345+
$this->getKeyName(), $this->getKey()
346346
)->{$method}($column, $amount, $extra);
347347
}
348348

@@ -369,11 +369,7 @@ protected function incrementOrDecrement(string $column, $amount, array $extra, s
369369
protected function incrementOrDecrementAttributeValue(string $column, $amount, $extra, $method)
370370
{
371371
$columnValue = $method === 'increment' ? $amount : $amount * -1;
372-
if (!property_exists($this, $column)) {
373-
$this->setAttribute($column, $this->getAttribute($column)[1] + $columnValue);
374-
} else {
375-
$this->{$column} = $this->{$column} + $columnValue;
376-
}
372+
$this->setAttribute($column, $this->getAttributeValue($column) + $columnValue);
377373

378374
$this->fill($extra);
379375

@@ -893,12 +889,12 @@ public function getIncrementing()
893889
/**
894890
* Get the value of the model's primary key.
895891
*
896-
* @return array
892+
* @return string
897893
* @throws DbException
898894
*/
899895
public function getKey()
900896
{
901-
return $this->getAttribute($this->getKeyName());
897+
return $this->getAttributeValue($this->getKeyName());
902898
}
903899

904900
/**
@@ -959,7 +955,7 @@ public function offsetExists($offset)
959955
*/
960956
public function offsetGet($offset)
961957
{
962-
return $this->getAttribute($offset);
958+
return $this->getAttributeValue($offset);
963959
}
964960

965961
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php declare(strict_types=1);
2+
3+
4+
namespace Swoft\Db\Listener;
5+
6+
use Swoft\Bean\BeanFactory;
7+
use Swoft\Db\Pool;
8+
use Swoft\Event\Annotation\Mapping\Subscriber;
9+
use Swoft\Event\EventInterface;
10+
use Swoft\Event\EventSubscriberInterface;
11+
use Swoft\Log\Helper\CLog;
12+
use Swoft\Server\Swoole\SwooleEvent;
13+
use Swoft\SwoftEvent;
14+
use Swoole\Event;
15+
16+
/**
17+
* Class WorkerStopListener
18+
*
19+
* @since 2.0
20+
*
21+
* @Subscriber()
22+
*/
23+
class WorkerStopAndErrorListener implements EventSubscriberInterface
24+
{
25+
/**
26+
* @return array
27+
*/
28+
public static function getSubscribedEvents(): array
29+
{
30+
return [
31+
SwooleEvent::WORKER_STOP => 'handle',
32+
SwoftEvent::WORKER_SHUTDOWN => 'handle',
33+
];
34+
}
35+
36+
/**
37+
* @param EventInterface $event
38+
*/
39+
public function handle(EventInterface $event): void
40+
{
41+
go(function () use ($event){
42+
$pools = BeanFactory::getBeans(Pool::class);
43+
44+
/* @var Pool $pool */
45+
foreach ($pools as $pool) {
46+
$count = $pool->close();
47+
48+
CLog::info('Close %d database connection on %s!', $count, $event->getName());
49+
}
50+
});
51+
52+
Event::wait();
53+
}
54+
}

src/Query/Builder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2403,7 +2403,7 @@ public function paginate(int $page = 1, int $perPage = 15, array $columns = ['*'
24032403
// Run a pagination count query
24042404
$count = $this->getCountForPagination($columns);
24052405
// Get paginate records
2406-
$list = $this->forPage($page, $perPage)->addSelect($columns)->get();
2406+
$list = $this->forPage($page, $perPage)->addSelect($columns)->get()->toArray();
24072407

24082408
return [
24092409
'count' => $count,

test/testing/Entity/User.php

+26
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,36 @@ class User extends Model
4646

4747
/**
4848
* @Column(name="user_desc", prop="udesc")
49+
*
4950
* @var string|null
5051
*/
5152
private $userDesc;
5253

54+
/**
55+
* this key is hump
56+
*
57+
* @Column()
58+
*
59+
* @var string|null
60+
*/
61+
private $testHump;
62+
63+
/**
64+
* @return null|string
65+
*/
66+
public function getTestHump(): ?string
67+
{
68+
return $this->testHump;
69+
}
70+
71+
/**
72+
* @param null|string $testHump
73+
*/
74+
public function setTestHump(?string $testHump): void
75+
{
76+
$this->testHump = $testHump;
77+
}
78+
5379
/**
5480
* @return int|null
5581
*/

test/unit/Eloquent/ModelTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,17 @@ public function testPaginate()
380380
$this->assertEquals($res['page'], $page);
381381
$this->assertEquals($res['perPage'], $perPage);
382382
}
383+
384+
public function testFull()
385+
{
386+
$expect = "testHump,哈";
387+
$attributes = ['testHump' => $expect];
388+
389+
$user = User::new($attributes);
390+
$this->assertEquals($expect, $user->getTestHump());
391+
392+
$userArray = User::new()->fill($attributes)->toArray();
393+
$this->assertArrayHasKey('testHump', $userArray);
394+
$this->assertEquals($expect, $userArray['testHump']);
395+
}
383396
}

0 commit comments

Comments
 (0)