DatabaseSeederTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Container\Container;
  5. use Illuminate\Database\Seeder;
  6. use Mockery as m;
  7. use Mockery\Mock;
  8. use PHPUnit\Framework\TestCase;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. class TestSeeder extends Seeder
  11. {
  12. public function run()
  13. {
  14. //
  15. }
  16. }
  17. class TestDepsSeeder extends Seeder
  18. {
  19. public function run(Mock $someDependency, $someParam = '')
  20. {
  21. //
  22. }
  23. }
  24. class DatabaseSeederTest extends TestCase
  25. {
  26. protected function tearDown(): void
  27. {
  28. m::close();
  29. }
  30. public function testCallResolveTheClassAndCallsRun()
  31. {
  32. $seeder = new TestSeeder;
  33. $seeder->setContainer($container = m::mock(Container::class));
  34. $output = m::mock(OutputInterface::class);
  35. $output->shouldReceive('writeln')->once();
  36. $command = m::mock(Command::class);
  37. $command->shouldReceive('getOutput')->once()->andReturn($output);
  38. $seeder->setCommand($command);
  39. $container->shouldReceive('make')->once()->with('ClassName')->andReturn($child = m::mock(Seeder::class));
  40. $child->shouldReceive('setContainer')->once()->with($container)->andReturn($child);
  41. $child->shouldReceive('setCommand')->once()->with($command)->andReturn($child);
  42. $child->shouldReceive('__invoke')->once();
  43. $command->shouldReceive('getOutput')->once()->andReturn($output);
  44. $output->shouldReceive('writeln')->once();
  45. $seeder->call('ClassName');
  46. }
  47. public function testSetContainer()
  48. {
  49. $seeder = new TestSeeder;
  50. $container = m::mock(Container::class);
  51. $this->assertEquals($seeder->setContainer($container), $seeder);
  52. }
  53. public function testSetCommand()
  54. {
  55. $seeder = new TestSeeder;
  56. $command = m::mock(Command::class);
  57. $this->assertEquals($seeder->setCommand($command), $seeder);
  58. }
  59. public function testInjectDependenciesOnRunMethod()
  60. {
  61. $container = m::mock(Container::class);
  62. $container->shouldReceive('call');
  63. $seeder = new TestDepsSeeder;
  64. $seeder->setContainer($container);
  65. $seeder->__invoke();
  66. $container->shouldHaveReceived('call')->once()->with([$seeder, 'run'], []);
  67. }
  68. public function testSendParamsOnCallMethodWithDeps()
  69. {
  70. $container = m::mock(Container::class);
  71. $container->shouldReceive('call');
  72. $seeder = new TestDepsSeeder;
  73. $seeder->setContainer($container);
  74. $seeder->__invoke(['test1', 'test2']);
  75. $container->shouldHaveReceived('call')->once()->with([$seeder, 'run'], ['test1', 'test2']);
  76. }
  77. }