当前位置:Gxlcms > php框架 > 测试laravel commands的方法详解

测试laravel commands的方法详解

时间:2021-07-01 10:21:17 帮助过:64人阅读

引言

最近使用到laravel的consolo命令行工具,在编写命令,想写一些测试的时候,发现官方文档中并没有提到command的测试方法。花了点时间,翻墙找了资料,实践成功并记录一下,方便更多人。

推荐:《laravel教程》

测试方法

大家都知道Laravel中使用了很多Symfony的成熟组件,Laravel的console组件使用的就是Symfony/console。

幸运的是,Symfony/console 组件中提供了用于command测试的CommandTester, 使用方法如下

  1. ...
  2. use FooCommand;
  3. use Symfony\Component\Console\Application;
  4. use Symfony\Component\Console\Tester\CommandTester;
  5. ...
  6. public function testSample(){
  7. //创建一个console测试应用平台,用来搭载测试的命令
  8. $application = new Application();
  9. //创建待测试的command
  10. $testedCommand = $this->app->make(FooCommand::class);
  11. //设置命令执行需要的laravel依赖
  12. $testedCommand->setLaravel(app());
  13. //添加待测试的command到测试应用上
  14. //同时command 也绑定 application
  15. $application->add($testedCommand);
  16. //实例化命令测试类
  17. $commandTester = new CommandTester($testedCommand);
  18. //命令输入流,对应每次交互需要提供的输入内容
  19. $commandTester->setInputs([
  20. //...
  21. ]);
  22. //执行命令
  23. $commandTester->execute(['command' => $testedCommand->getName()]);
  24. //对命令执行结果进行断言测试,主要是依靠正则判断
  25. //$commandTester->getDisplay() 方法可以获取命令执行后的输出结果
  26. $this->assertRegExp("/some reg/", $commandTester->getDisplay());
  27. }

示例

我们现在有一个手动创建新用户的命令createUser,作用就是手动创建一个用户。

需要交互式让用户输入name,email,password,comfirm password,这些数据。

待测试的command

  1. <?php
  2. namespace App\Console\Commands;
  3. use App\User;
  4. use Illuminate\Auth\Events\Registered;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\Validator;
  7. class CreateUser extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'createUser';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'create new user for system manually';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $this->line($this->description);
  38. // 获取输入的数据
  39. $data = [
  40. 'name' => $this->ask('What\'s your name?'),
  41. 'email' => $this->ask('What\'s your email?'),
  42. 'password' => $this->secret('What\'s your password?'),
  43. 'password_confirmation' => $this->secret('Pleas confirm your password.')
  44. ];
  45. // 验证输入内容
  46. $validator = $this->makeValidator($data);
  47. if ($validator->fails()) {
  48. foreach ($validator->errors()->toArray() as $error) {
  49. foreach ($error as $message) {
  50. $this->error($message);
  51. }
  52. }
  53. return;
  54. }
  55. // 向用户确认输入信息
  56. if (!$this->confirm('Confirm your info: ' . PHP_EOL . 'name:' . $data['name'] . PHP_EOL . 'email:' . $data['email'] . PHP_EOL . 'is this correct?')) {
  57. return;
  58. }
  59. // 注册
  60. $user = $this->create($data);
  61. event(new Registered($user));
  62. $this->line('User ' . $user->name . ' successfully registered');
  63. }
  64. /**
  65. * Get a validator for an incoming registration request.
  66. *
  67. * @param array $data
  68. * @return \Illuminate\Contracts\Validation\Validator
  69. */
  70. protected function makeValidator($data)
  71. {
  72. return Validator::make($data, [
  73. 'name' => 'required|string|max:255|unique:users',
  74. 'email' => 'required|string|email|max:255|unique:users',
  75. 'password' => 'required|string|min:6|confirmed'
  76. ]);
  77. }
  78. /**
  79. * Create a new user instance after a valid registration.
  80. *
  81. * @param array $data
  82. * @return \App\User
  83. */
  84. protected function create($data)
  85. {
  86. return User::create([
  87. 'name' => $data['name'],
  88. 'email' => $data['email'],
  89. 'password' => bcrypt($data['password'])
  90. ]);
  91. }
  92. }

正确的结果

如果正确输入信息的话,会得到如下输出

  1. $ path-to-your-app/app# php artisan createUser
  2. create new user for system manually
  3. What's your name?:
  4. > vestin
  5. What's your email?:
  6. > correct@abc.com
  7. What's your password?:
  8. >
  9. Pleas confirm your password.:
  10. >
  11. Confirm your info:
  12. name:vestin
  13. email:correct@abc.com
  14. is this correct? (yes/no) [no]:
  15. > yes
  16. User vestin successfully registered

想要测试的内容

我想要测试两块内容:

1.数据输入验证测试

● email有效性测试

● password两次输入是否相同的测试

2.正确创建用户测试

● 编写单元测试

  1. <?php
  2. namespace Tests\Unit\command;
  3. use App\Console\Commands\CreateUser;
  4. use Symfony\Component\Console\Application;
  5. use Symfony\Component\Console\Tester\CommandTester;
  6. use Tests\TestCase;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. class CreateUserTest extends TestCase
  9. {
  10. use RefreshDatabase;
  11. /**
  12. * 测试数据验证
  13. *
  14. * @return void
  15. */
  16. public function testValidation()
  17. {
  18. $application = new Application();
  19. $testedCommand = $this->app->make(CreateUser::class);
  20. $testedCommand->setLaravel(app());
  21. $application->add($testedCommand);
  22. $commandTester = new CommandTester($testedCommand);
  23. $commandTester->setInputs(['Vestin', 'badEmail@abc', '123456', '654321']);
  24. $commandTester->execute(['command' => $testedCommand->getName()]);
  25. // assert
  26. $this->assertRegExp("/The email must be a valid email address/", $commandTester->getDisplay());
  27. $commandTester->setInputs(['vestin', 'correct@abc.com', '123456', '654321']);
  28. $commandTester->execute(['command' => $testedCommand->getName()]);
  29. // assert
  30. $this->assertRegExp("/The password confirmation does not match/", $commandTester->getDisplay());
  31. }
  32. /**
  33. * 测试成功注册用户
  34. *
  35. * @return void
  36. */
  37. public function testSuccess()
  38. {
  39. $application = new Application();
  40. $testedCommand = $this->app->make(CreateUser::class);
  41. $testedCommand->setLaravel(app());
  42. $application->add($testedCommand);
  43. $commandTester = new CommandTester($testedCommand);
  44. $commandTester->setInputs(['Vestin', 'correct@abc.com', '123456', '123456', 'y']);
  45. $commandTester->execute(['command' => $testedCommand->getName()]);
  46. // assert
  47. $this->assertRegExp("/User Vestin successfully registered/", $commandTester->getDisplay());
  48. $this->assertDatabaseHas('users', [
  49. 'email' => 'correct@abc.com',
  50. 'name' => 'Vestin'
  51. ]);
  52. }
  53. }

执行测试

  1. $ path-to-your-app/app# ./vendor/bin/phpunit
  2. PHPUnit 6.4.3 by Sebastian Bergmann and contributors.
  3. .. 3 / 3 (100%)
  4. Time: 659 ms, Memory: 14.00MB

以上就是测试laravel commands的方法详解的详细内容,更多请关注Gxlcms其它相关文章!

人气教程排行