mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-02 03:37:44 +08:00
Don't format decimal
to float
for command gen:model
by default. (#2979)
* Don't format `decimal` to `float` for command `gen:model` by default. * Update CHANGELOG-2.1.md * Added test case.
This commit is contained in:
parent
4081346d37
commit
c74e5d8368
@ -39,6 +39,7 @@
|
||||
- [#2871](https://github.com/hyperf/hyperf/pull/2871) Use `(string) $body` instead of `$body->getContents()` for getting contents from `StreamInterface`, because method `getContents()` only returns the remaining contents in a string.
|
||||
- [#2909](https://github.com/hyperf/hyperf/pull/2909) Allow setting repeated middlewares.
|
||||
- [#2935](https://github.com/hyperf/hyperf/pull/2935) Changed the string format for default exception formatter.
|
||||
- [#2979](https://github.com/hyperf/hyperf/pull/2979) Don't format `decimal` to `float` for command `gen:model` by default.
|
||||
|
||||
## Deprecated
|
||||
|
||||
|
@ -125,7 +125,7 @@ class ModelUpdateVisitor extends NodeVisitorAbstract
|
||||
$items = [];
|
||||
$casts = $this->class->getCasts();
|
||||
foreach ($node->default->items as $item) {
|
||||
$caster = $this->class->getCasts()[$item->key->value] ?? null;
|
||||
$caster = $casts[$item->key->value] ?? null;
|
||||
if ($caster && $this->isCaster($caster)) {
|
||||
$items[] = $item;
|
||||
}
|
||||
@ -350,11 +350,6 @@ class ModelUpdateVisitor extends NodeVisitorAbstract
|
||||
case 'int':
|
||||
case 'bigint':
|
||||
return 'integer';
|
||||
case 'decimal':
|
||||
case 'float':
|
||||
case 'double':
|
||||
case 'real':
|
||||
return 'float';
|
||||
case 'bool':
|
||||
case 'boolean':
|
||||
return 'boolean';
|
||||
|
118
src/database/tests/GenModelTest.php
Normal file
118
src/database/tests/GenModelTest.php
Normal file
@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace HyperfTest\Database;
|
||||
|
||||
use Hyperf\Database\Commands\Ast\ModelUpdateVisitor;
|
||||
use Hyperf\Database\ConnectionResolverInterface;
|
||||
use Hyperf\Database\Schema\MySqlBuilder;
|
||||
use HyperfTest\Database\Stubs\ContainerStub;
|
||||
use HyperfTest\Database\Stubs\Model\UserExtEmpty;
|
||||
use Mockery;
|
||||
use PhpParser\NodeTraverser;
|
||||
use PhpParser\ParserFactory;
|
||||
use PhpParser\PrettyPrinter\Standard;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class GenModelTest extends TestCase
|
||||
{
|
||||
protected $license = '<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/';
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Mockery::close();
|
||||
}
|
||||
|
||||
public function testDatabaseFormat()
|
||||
{
|
||||
$container = ContainerStub::getContainer();
|
||||
$container->shouldReceive('get')->with(EventDispatcherInterface::class)->andReturnUsing(function () {
|
||||
$dispatcher = Mockery::mock(EventDispatcherInterface::class);
|
||||
$dispatcher->shouldReceive('dispatch')->withAnyArgs()->andReturn(null);
|
||||
return $dispatcher;
|
||||
});
|
||||
$connection = $container->get(ConnectionResolverInterface::class)->connection();
|
||||
/** @var MySqlBuilder $builder */
|
||||
$builder = $connection->getSchemaBuilder('default');
|
||||
$columns = $this->formatColumns($builder->getColumnTypeListing('user_ext'));
|
||||
foreach ($columns as $i => $column) {
|
||||
if ($column['column_name'] === 'created_at') {
|
||||
$columns[$i]['cast'] = 'datetime';
|
||||
}
|
||||
}
|
||||
|
||||
$astParser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
|
||||
$stms = $astParser->parse(file_get_contents(__DIR__ . '/Stubs/Model/UserExtEmpty.php'));
|
||||
$traverser = new NodeTraverser();
|
||||
$visitor = new ModelUpdateVisitor(UserExtEmpty::class, $columns, ContainerStub::getModelOption());
|
||||
$traverser->addVisitor($visitor);
|
||||
$stms = $traverser->traverse($stms);
|
||||
$code = (new Standard())->prettyPrintFile($stms);
|
||||
$this->assertEquals($this->license . '
|
||||
namespace HyperfTest\Database\Stubs\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $count
|
||||
* @property string $float_num
|
||||
* @property string $str
|
||||
* @property string $json
|
||||
* @property \Carbon\Carbon $created_at
|
||||
* @property string $updated_at
|
||||
*/
|
||||
class UserExtEmpty extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = \'user_ext\';
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [\'id\', \'count\', \'float_num\', \'str\', \'json\', \'created_at\', \'updated_at\'];
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [\'id\' => \'integer\', \'count\' => \'integer\', \'created_at\' => \'datetime\'];
|
||||
}', $code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format column's key to lower case.
|
||||
*/
|
||||
protected function formatColumns(array $columns): array
|
||||
{
|
||||
return array_map(function ($item) {
|
||||
return array_change_key_case($item, CASE_LOWER);
|
||||
}, $columns);
|
||||
}
|
||||
}
|
@ -1811,7 +1811,7 @@ class ModelTest extends TestCase
|
||||
|
||||
$called = false;
|
||||
|
||||
ModelStub::withoutTouching(function () use (&$called, $model) {
|
||||
ModelStub::withoutTouching(function () use (&$called) {
|
||||
$called = true;
|
||||
});
|
||||
|
||||
@ -1824,7 +1824,7 @@ class ModelTest extends TestCase
|
||||
|
||||
$called = false;
|
||||
|
||||
Model::withoutTouchingOn([ModelStub::class], function () use (&$called, $model) {
|
||||
Model::withoutTouchingOn([ModelStub::class], function () use (&$called) {
|
||||
$called = true;
|
||||
});
|
||||
|
||||
|
@ -11,6 +11,7 @@ declare(strict_types=1);
|
||||
*/
|
||||
namespace HyperfTest\Database\Stubs;
|
||||
|
||||
use Hyperf\Database\Commands\ModelOption;
|
||||
use Hyperf\Database\ConnectionResolver;
|
||||
use Hyperf\Database\ConnectionResolverInterface;
|
||||
use Hyperf\Database\Connectors\ConnectionFactory;
|
||||
@ -52,4 +53,18 @@ class ContainerStub
|
||||
|
||||
return $container;
|
||||
}
|
||||
|
||||
public static function getModelOption()
|
||||
{
|
||||
$option = new ModelOption();
|
||||
$option->setWithComments(false)
|
||||
->setRefreshFillable(true)
|
||||
->setForceCasts(true)
|
||||
->setInheritance('Model')
|
||||
->setPath(__DIR__ . '/../Stubs/Model')
|
||||
->setPool('default')
|
||||
->setPrefix('')
|
||||
->setWithIde(false);
|
||||
return $option;
|
||||
}
|
||||
}
|
||||
|
36
src/database/tests/Stubs/Model/UserExtEmpty.php
Normal file
36
src/database/tests/Stubs/Model/UserExtEmpty.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace HyperfTest\Database\Stubs\Model;
|
||||
|
||||
class UserExtEmpty extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'user_ext';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [];
|
||||
}
|
Loading…
Reference in New Issue
Block a user