-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # src/crontab/src/Event/Event.php # src/crontab/src/Event/FailToExecute.php
- Loading branch information
Showing
3 changed files
with
140 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?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\GenerateModelIDEVisitor; | ||
use Hyperf\Database\Commands\ModelData; | ||
use Hyperf\Database\Commands\ModelOption; | ||
use HyperfTest\Database\Stubs\Model\TestGenerateIdeModel; | ||
use PhpParser\Lexer\Emulative; | ||
use PhpParser\NodeTraverser; | ||
use PhpParser\ParserFactory; | ||
use PhpParser\PrettyPrinter\Standard; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @internal | ||
* @coversNothing | ||
*/ | ||
class ModelGenerateTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
$this->lexer = new Emulative([ | ||
'usedAttributes' => [ | ||
'comments', | ||
'startLine', 'endLine', | ||
'startTokenPos', 'endTokenPos', | ||
], | ||
]); | ||
$this->astParser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7, $this->lexer); | ||
$this->printer = new Standard(); | ||
} | ||
|
||
public function testGenerateScope() | ||
{ | ||
$code = file_get_contents(__DIR__ . '/Stubs/Model/TestGenerateIdeModel.php'); | ||
|
||
$option = new ModelOption(); | ||
$data = new ModelData(TestGenerateIdeModel::class, [ | ||
['column_name' => 'name'], | ||
['column_name' => 'age'], | ||
]); | ||
|
||
$stmts = $this->astParser->parse($code); | ||
$traverser = new NodeTraverser(); | ||
$traverser->addVisitor(new GenerateModelIDEVisitor($option, $data)); | ||
$stmts = $traverser->traverse($stmts); | ||
$code = $this->printer->prettyPrintFile($stmts); | ||
|
||
$this->assertNotFalse(strpos($code, 'public static function optionNull(?string $test)')); | ||
$this->assertNotFalse(strpos($code, 'public static function string(string $test)')); | ||
$this->assertNotFalse(strpos($code, 'public static function union(int $appId, string|int $uid)')); | ||
$this->assertNotFalse(strpos($code, 'public static function unionOrNull(int $appId, string|int|null $uid)')); | ||
$this->assertNotFalse(strpos($code, 'public static function singleOrNull(?string $test)')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?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 TestGenerateIdeModel extends Model | ||
{ | ||
/** | ||
* The table associated with the model. | ||
*/ | ||
protected ?string $table = 'test_generate_ide_models'; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
*/ | ||
protected array $fillable = ['id', 'user_id', 'title', 'created_at', 'updated_at']; | ||
|
||
/** | ||
* The attributes that should be cast to native types. | ||
*/ | ||
protected array $casts = ['id' => 'integer', 'user_id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime']; | ||
|
||
public function image() | ||
{ | ||
return $this->morphOne(Image::class, 'imageable'); | ||
} | ||
|
||
public function user() | ||
{ | ||
return $this->belongsTo(User::class, 'user_id', 'id'); | ||
} | ||
|
||
public function scopeOptionNull($query, ?string $test) | ||
{ | ||
} | ||
|
||
public function scopeString($query, string $test) | ||
{ | ||
} | ||
|
||
public function scopeUnion($query, int $appId, string|int $uid) | ||
{ | ||
} | ||
|
||
public function scopeUnionOrNull($query, int $appId, string|int|null $uid) | ||
{ | ||
} | ||
|
||
public function scopeSingleOrNull($query, null|string $test) | ||
{ | ||
} | ||
|
||
// TODO PHP 8.2 起单独支持null类型 | ||
} |