Skip to content

Commit

Permalink
fix Request test
Browse files Browse the repository at this point in the history
  • Loading branch information
panyongwei committed May 27, 2019
2 parents 39d8c14 + 6c2d12b commit fd1541c
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 23 deletions.
14 changes: 13 additions & 1 deletion src/Concern/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ public function getAttribute(string $key): array
return [$pro, $value];
}

/**
* Get an attribute value from the model.
*
* @param string $key
* @return mixed
* @throws DbException
*/
public function getAttributeValue(string $key)
{
return $this->getAttribute($key)[1];
}

/**
* Get an not hidden attribute from the model.
*
Expand Down Expand Up @@ -390,7 +402,7 @@ public function only(array $attributes)
$results = [];

foreach ($attributes as $attribute) {
$results[$attribute] = $this->getAttribute($attribute);
$results[$attribute] = $this->getAttributeValue($attribute);
}

return $results;
Expand Down
23 changes: 15 additions & 8 deletions src/Connection/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function initialize(Pool $pool, Database $database)
{
$this->pool = $pool;
$this->database = $database;
$this->lastTime = time();

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

/**
* Close connection
*/
public function close(): void
{
if (!empty($this->pdo)) {
$this->pdo = null;
}

if (!empty($this->readPdo)) {
$this->readPdo = null;
}
}

/**
* Reconnect
*/
Expand Down Expand Up @@ -213,14 +228,6 @@ public function release(bool $force = false): void
}
}

/**
* @return int
*/
public function getLastTime(): int
{
return time();
}

/**
* @return Grammar
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ public function firstOr(array $columns = ['*'], Closure $callback = null)
public function value(string $column)
{
if ($result = $this->first([$column])) {
return $result->getAttribute($column)[1];
return $result->getAttributeValue($column);
}

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

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

unset($results);
} while ($countResults == $count);
Expand Down
4 changes: 2 additions & 2 deletions src/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function diff($items)

foreach ($this->items as $item) {
/* @var Model $item */
if (!isset($dictionary[$item->getKey()[1]])) {
if (!isset($dictionary[$item->getKey()])) {
$diff->add($item);
}
}
Expand All @@ -199,7 +199,7 @@ public function intersect($items)

foreach ($this->items as $item) {
/* @var Model $item */
if (isset($dictionary[$item->getKey()[1]])) {
if (isset($dictionary[$item->getKey()])) {
$intersect->add($item);
}
}
Expand Down
14 changes: 5 additions & 9 deletions src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ protected function incrementOrDecrement(string $column, $amount, array $extra, s
$this->incrementOrDecrementAttributeValue($column, $amount, $extra, $method);

return $query->where(
$this->getKeyName(), $this->getKey()[1]
$this->getKeyName(), $this->getKey()
)->{$method}($column, $amount, $extra);
}

Expand All @@ -369,11 +369,7 @@ protected function incrementOrDecrement(string $column, $amount, array $extra, s
protected function incrementOrDecrementAttributeValue(string $column, $amount, $extra, $method)
{
$columnValue = $method === 'increment' ? $amount : $amount * -1;
if (!property_exists($this, $column)) {
$this->setAttribute($column, $this->getAttribute($column)[1] + $columnValue);
} else {
$this->{$column} = $this->{$column} + $columnValue;
}
$this->setAttribute($column, $this->getAttributeValue($column) + $columnValue);

$this->fill($extra);

Expand Down Expand Up @@ -893,12 +889,12 @@ public function getIncrementing()
/**
* Get the value of the model's primary key.
*
* @return array
* @return string
* @throws DbException
*/
public function getKey()
{
return $this->getAttribute($this->getKeyName());
return $this->getAttributeValue($this->getKeyName());
}

/**
Expand Down Expand Up @@ -959,7 +955,7 @@ public function offsetExists($offset)
*/
public function offsetGet($offset)
{
return $this->getAttribute($offset);
return $this->getAttributeValue($offset);
}

/**
Expand Down
54 changes: 54 additions & 0 deletions src/Listener/WorkerStopAndErrorListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types=1);


namespace Swoft\Db\Listener;

use Swoft\Bean\BeanFactory;
use Swoft\Db\Pool;
use Swoft\Event\Annotation\Mapping\Subscriber;
use Swoft\Event\EventInterface;
use Swoft\Event\EventSubscriberInterface;
use Swoft\Log\Helper\CLog;
use Swoft\Server\Swoole\SwooleEvent;
use Swoft\SwoftEvent;
use Swoole\Event;

/**
* Class WorkerStopListener
*
* @since 2.0
*
* @Subscriber()
*/
class WorkerStopAndErrorListener implements EventSubscriberInterface
{
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
SwooleEvent::WORKER_STOP => 'handle',
SwoftEvent::WORKER_SHUTDOWN => 'handle',
];
}

/**
* @param EventInterface $event
*/
public function handle(EventInterface $event): void
{
go(function () use ($event){
$pools = BeanFactory::getBeans(Pool::class);

/* @var Pool $pool */
foreach ($pools as $pool) {
$count = $pool->close();

CLog::info('Close %d database connection on %s!', $count, $event->getName());
}
});

Event::wait();
}
}
2 changes: 1 addition & 1 deletion src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2403,7 +2403,7 @@ public function paginate(int $page = 1, int $perPage = 15, array $columns = ['*'
// Run a pagination count query
$count = $this->getCountForPagination($columns);
// Get paginate records
$list = $this->forPage($page, $perPage)->addSelect($columns)->get();
$list = $this->forPage($page, $perPage)->addSelect($columns)->get()->toArray();

return [
'count' => $count,
Expand Down
26 changes: 26 additions & 0 deletions test/testing/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,36 @@ class User extends Model

/**
* @Column(name="user_desc", prop="udesc")
*
* @var string|null
*/
private $userDesc;

/**
* this key is hump
*
* @Column()
*
* @var string|null
*/
private $testHump;

/**
* @return null|string
*/
public function getTestHump(): ?string
{
return $this->testHump;
}

/**
* @param null|string $testHump
*/
public function setTestHump(?string $testHump): void
{
$this->testHump = $testHump;
}

/**
* @return int|null
*/
Expand Down
13 changes: 13 additions & 0 deletions test/unit/Eloquent/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,17 @@ public function testPaginate()
$this->assertEquals($res['page'], $page);
$this->assertEquals($res['perPage'], $perPage);
}

public function testFull()
{
$expect = "testHump,哈";
$attributes = ['testHump' => $expect];

$user = User::new($attributes);
$this->assertEquals($expect, $user->getTestHump());

$userArray = User::new()->fill($attributes)->toArray();
$this->assertArrayHasKey('testHump', $userArray);
$this->assertEquals($expect, $userArray['testHump']);
}
}

0 comments on commit fd1541c

Please sign in to comment.