Guest User

Untitled

a guest
Jan 19th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. class ControllerGenerator extends Generator
  2. {
  3. // ...
  4.  
  5. public function generate(BundleInterface $bundle, $controller, array $actions = array())
  6. {
  7. // ...
  8. }
  9. }
  10.  
  11. class GenerateControllerCommand extends ContainerAwareCommand
  12. {
  13. private $generator;
  14.  
  15. /**
  16. * @see Command
  17. */
  18. public function configure()
  19. {
  20. // ...
  21. }
  22.  
  23. public function execute(InputInterface $input, OutputInterface $output)
  24. {
  25. // ...
  26.  
  27. $generator = $this->generator;
  28. $generator->generate($bundle, $controller);
  29.  
  30. // ...
  31. }
  32.  
  33. protected function getGenerator()
  34. {
  35. if (null === $this->generator) {
  36. $this->generator = new ControllerGenerator($this->getContainer()->get('filesystem'), __DIR__.'/../Resources/skeleton/bundle');
  37. }
  38.  
  39. return $this->generator;
  40. }
  41.  
  42. public function setGenerator(ControllerGenerator $generator)
  43. {
  44. $this->generator = $generator;
  45. }
  46. }
  47.  
  48. class GenerateControllerCommandTest extends GenerateCommandTest
  49. {
  50. public function testNonInteractiveCommand()
  51. {
  52. $bundle = 'FooBarBundle';
  53. $controller = 'PostController';
  54.  
  55. $input = array(
  56. 'command' => 'generate:controller',
  57. '--bundle' => $bundle,
  58. '--controller' => $controller,
  59. );
  60.  
  61. $application = $this->getApplication();
  62. $commandTester = $this->getCommandTester($input);
  63. $generator = $this->getGenerator();
  64.  
  65. $generator
  66. ->expects($this->once())
  67. ->method('generate')
  68. ->with($this->getContainer()->get('kernel')->getBundle($bundle), $controller)
  69. ;
  70.  
  71. $commandTester->execute($input, array('interactive' => false));
  72. }
  73.  
  74. protected function getCommandTester($input = '')
  75. {
  76. return new CommandTester($this->getCommand($input));
  77. }
  78.  
  79. protected function getCommand($input = '')
  80. {
  81. return $this->getApplication($input)->find('generate:controller');
  82. }
  83.  
  84. protected function getApplication($input = '')
  85. {
  86. $application = new Application();
  87.  
  88. $command = new GenerateControllerCommand();
  89. $command->setContainer($this->getContainer());
  90. $command->setHelperSet($this->getHelperSet($input));
  91. $command->setGenerator($this->getGenerator());
  92.  
  93. $application->add($command);
  94.  
  95. return $application;
  96. }
  97.  
  98. protected function getGenerator()
  99. {
  100. // get a noop generator
  101. return $this
  102. ->getMockBuilder('SensioBundleGeneratorBundleGeneratorControllerGenerator')
  103. ->disableOriginalConstructor()
  104. ->setMethods(array('generate'))
  105. ->getMock()
  106. ;
  107. }
  108. }
  109.  
  110. $ phpunit TestsCommandGenerateControllerCommandTest
  111.  
  112. PHPUnit 3.7.0 by Sebastian Bergmann.
  113.  
  114. Configuration read from E:Wouterwebwampwwwwjsnipvendorsensiogenerator-bundleSensioBundleGeneratorBundlephpunit.xml.dist
  115.  
  116. F
  117.  
  118. Time: 2 seconds, Memory: 7.25Mb
  119.  
  120. There was 1 failure:
  121.  
  122. 1) SensioBundleGeneratorBundleTestsCommandGenerateControllerCommandTest::testNonInteractiveCommand
  123. Expectation failed for method name is equal to <string:generate> when invoked 1 time(s).
  124. Method was expected to be called 1 times, actually called 0 times.
  125.  
  126. E:Wouterwebwampbinphpphp5.3.5PEARphpunit:46
  127.  
  128. FAILURES!
  129. Tests: 1, Assertions: 7, Failures: 1.
  130.  
  131. $generator = $this->getGenerator();
  132.  
  133. $generator
  134. ->expects($this->once())
  135. ->method('generate')
  136. ->with($this->getContainer()->get('kernel')->getBundle($bundle), $controller)
  137. ;
  138.  
  139. $commandTester->execute($input, array('interactive' => false));
  140. }
  141.  
  142. protected function getApplication() {
  143. //...
  144. $command->setGenerator($this->getGenerator());
  145. //...
  146. }
Add Comment
Please, Sign In to add comment