PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpspec/phpspec/features/code_generation/developer_generates_method.feature

https://gitlab.com/kimting254/wbms
Gherkin Specification | 417 lines | 328 code | 89 blank | 0 comment | 16 complexity | 1af0fe6c7128f2410843d3df4a89f6b8 MD5 | raw file
  1. Feature: Developer generates a method
  2. As a Developer
  3. I want to automate creating methods
  4. In order to avoid repetitive tasks and interruptions in development flow
  5. Scenario: Generating a method
  6. Given the spec file "spec/CodeGeneration/MethodExample1/MarkdownSpec.php" contains:
  7. """
  8. <?php
  9. namespace spec\CodeGeneration\MethodExample1;
  10. use PhpSpec\ObjectBehavior;
  11. use Prophecy\Argument;
  12. class MarkdownSpec extends ObjectBehavior
  13. {
  14. function it_converts_plain_text_to_html_paragraphs()
  15. {
  16. $this->toHtml('Hi, there')->shouldReturn('<p>Hi, there</p>');
  17. }
  18. }
  19. """
  20. And the class file "src/CodeGeneration/MethodExample1/Markdown.php" contains:
  21. """
  22. <?php
  23. namespace CodeGeneration\MethodExample1;
  24. class Markdown
  25. {
  26. }
  27. """
  28. When I run phpspec and answer "y" when asked if I want to generate the code
  29. Then the class in "src/CodeGeneration/MethodExample1/Markdown.php" should contain:
  30. """
  31. <?php
  32. namespace CodeGeneration\MethodExample1;
  33. class Markdown
  34. {
  35. public function toHtml($argument1)
  36. {
  37. // TODO: write logic here
  38. }
  39. }
  40. """
  41. Scenario: Generating a method in a class with psr4 prefix
  42. Given the spec file "spec/MyNamespace/PrefixSpec.php" contains:
  43. """
  44. <?php
  45. namespace spec\Behat\Tests\MyNamespace;
  46. use PhpSpec\ObjectBehavior;
  47. use Prophecy\Argument;
  48. class PrefixSpec extends ObjectBehavior
  49. {
  50. function it_converts_plain_text_to_html_paragraphs()
  51. {
  52. $this->toHtml('Hi, there')->shouldReturn('<p>Hi, there</p>');
  53. }
  54. }
  55. """
  56. And the config file contains:
  57. """
  58. suites:
  59. behat_suite:
  60. namespace: Behat\Tests\MyNamespace
  61. psr4_prefix: Behat\Tests
  62. """
  63. And the class file "src/MyNamespace/Prefix.php" contains:
  64. """
  65. <?php
  66. namespace Behat\Tests\MyNamespace;
  67. class Prefix
  68. {
  69. }
  70. """
  71. When I run phpspec and answer "y" when asked if I want to generate the code
  72. Then the class in "src/MyNamespace/Prefix.php" should contain:
  73. """
  74. <?php
  75. namespace Behat\Tests\MyNamespace;
  76. class Prefix
  77. {
  78. public function toHtml($argument1)
  79. {
  80. // TODO: write logic here
  81. }
  82. }
  83. """
  84. Scenario: Generating a constructor in a file with existing methods places the constructor first
  85. Given the spec file "spec/MyNamespace/ConstructorSpec.php" contains:
  86. """
  87. <?php
  88. namespace spec\MyNamespace;
  89. use PhpSpec\ObjectBehavior;
  90. use Prophecy\Argument;
  91. class ConstructorSpec extends ObjectBehavior
  92. {
  93. function it_should_do_something_with_a_constructor()
  94. {
  95. $this->beConstructedWith('anArgument');
  96. $this->foo()->shouldReturn('bar');
  97. }
  98. }
  99. """
  100. And the class file "src/MyNamespace/Constructor.php" contains:
  101. """
  102. <?php
  103. namespace MyNamespace;
  104. class Constructor
  105. {
  106. public function foo()
  107. {
  108. return 'bar';
  109. }
  110. }
  111. """
  112. When I run phpspec and answer "y" when asked if I want to generate the code
  113. Then the class in "src/MyNamespace/Constructor.php" should contain:
  114. """
  115. <?php
  116. namespace MyNamespace;
  117. class Constructor
  118. {
  119. public function __construct($argument1)
  120. {
  121. // TODO: write logic here
  122. }
  123. public function foo()
  124. {
  125. return 'bar';
  126. }
  127. }
  128. """
  129. Scenario: Generating a constructor in a file with no methods
  130. Given the spec file "spec/MyNamespace/ConstructorFirstSpec.php" contains:
  131. """
  132. <?php
  133. namespace spec\MyNamespace;
  134. use PhpSpec\ObjectBehavior;
  135. use Prophecy\Argument;
  136. class ConstructorFirstSpec extends ObjectBehavior
  137. {
  138. function it_should_do_something_with_a_constructor()
  139. {
  140. $this->beConstructedWith('anArgument');
  141. $this->foo()->shouldReturn('bar');
  142. }
  143. }
  144. """
  145. And the class file "src/MyNamespace/ConstructorFirst.php" contains:
  146. """
  147. <?php
  148. namespace MyNamespace;
  149. class ConstructorFirst
  150. {
  151. }
  152. """
  153. When I run phpspec and answer "y" when asked if I want to generate the code
  154. Then the class in "src/MyNamespace/ConstructorFirst.php" should contain:
  155. """
  156. <?php
  157. namespace MyNamespace;
  158. class ConstructorFirst
  159. {
  160. public function __construct($argument1)
  161. {
  162. // TODO: write logic here
  163. }
  164. }
  165. """
  166. Scenario: Generating a method in a class with existing methods and new lines
  167. Given the spec file "spec/MyNamespace/ExistingMethodSpec.php" contains:
  168. """
  169. <?php
  170. namespace spec\MyNamespace;
  171. use PhpSpec\ObjectBehavior;
  172. use Prophecy\Argument;
  173. class ExistingMethodSpec extends ObjectBehavior
  174. {
  175. function it_should_do_something()
  176. {
  177. $this->foo()->shouldReturn('bar');
  178. }
  179. }
  180. """
  181. And the class file "src/MyNamespace/ExistingMethod.php" contains:
  182. """
  183. <?php
  184. namespace MyNamespace;
  185. class ExistingMethod
  186. {
  187. public function existing()
  188. {
  189. return 'something';
  190. }
  191. }
  192. """
  193. When I run phpspec and answer "y" when asked if I want to generate the code
  194. Then the class in "src/MyNamespace/ExistingMethod.php" should contain:
  195. """
  196. <?php
  197. namespace MyNamespace;
  198. class ExistingMethod
  199. {
  200. public function existing()
  201. {
  202. return 'something';
  203. }
  204. public function foo()
  205. {
  206. // TODO: write logic here
  207. }
  208. }
  209. """
  210. Scenario: Generating a method in a class with existing methods containing anonymous functions
  211. Given the spec file "spec/MyNamespace/ExistingMethodAnonymousFunctionSpec.php" contains:
  212. """
  213. <?php
  214. namespace spec\MyNamespace;
  215. use PhpSpec\ObjectBehavior;
  216. use Prophecy\Argument;
  217. class ExistingMethodAnonymousFunctionSpec extends ObjectBehavior
  218. {
  219. function it_should_do_something()
  220. {
  221. $this->foo()->shouldReturn('bar');
  222. }
  223. }
  224. """
  225. And the class file "src/MyNamespace/ExistingMethodAnonymousFunction.php" contains:
  226. """
  227. <?php
  228. namespace MyNamespace;
  229. class ExistingMethodAnonymousFunction
  230. {
  231. public function existing()
  232. {
  233. return function () {
  234. return 'something';
  235. };
  236. }
  237. }
  238. """
  239. When I run phpspec and answer "y" when asked if I want to generate the code
  240. Then the class in "src/MyNamespace/ExistingMethodAnonymousFunction.php" should contain:
  241. """
  242. <?php
  243. namespace MyNamespace;
  244. class ExistingMethodAnonymousFunction
  245. {
  246. public function existing()
  247. {
  248. return function () {
  249. return 'something';
  250. };
  251. }
  252. public function foo()
  253. {
  254. // TODO: write logic here
  255. }
  256. }
  257. """
  258. Scenario: Generating a constructor in a file with no methods
  259. Given the spec file "spec/MyNamespace/CommentMethodSpec.php" contains:
  260. """
  261. <?php
  262. namespace spec\MyNamespace;
  263. use PhpSpec\ObjectBehavior;
  264. use Prophecy\Argument;
  265. class CommentMethodSpec extends ObjectBehavior
  266. {
  267. function it_should_do_something()
  268. {
  269. $this->foo()->shouldReturn('bar');
  270. }
  271. }
  272. """
  273. And the class file "src/MyNamespace/CommentMethod.php" contains:
  274. """
  275. <?php
  276. namespace MyNamespace;
  277. class CommentMethod
  278. {
  279. // this is a comment
  280. }
  281. """
  282. When I run phpspec and answer "y" when asked if I want to generate the code
  283. Then the class in "src/MyNamespace/CommentMethod.php" should contain:
  284. """
  285. <?php
  286. namespace MyNamespace;
  287. class CommentMethod
  288. {
  289. // this is a comment
  290. public function foo()
  291. {
  292. // TODO: write logic here
  293. }
  294. }
  295. """
  296. Scenario: Generating a method named with a restricted keyword
  297. Given the spec file "spec/MyNamespace/RestrictedSpec.php" contains:
  298. """
  299. <?php
  300. namespace spec\MyNamespace;
  301. use PhpSpec\ObjectBehavior;
  302. use Prophecy\Argument;
  303. class RestrictedSpec extends ObjectBehavior
  304. {
  305. function it_tries_to_call_wrong_method()
  306. {
  307. $this->throw()->shouldReturn();
  308. }
  309. }
  310. """
  311. And the class file "src/MyNamespace/Restricted.php" contains:
  312. """
  313. <?php
  314. namespace MyNamespace;
  315. class Restricted
  316. {
  317. }
  318. """
  319. When I run phpspec interactively
  320. Then I should see "I cannot generate the method 'throw' for you"
  321. And the class in "src/MyNamespace/Restricted.php" should contain:
  322. """
  323. <?php
  324. namespace MyNamespace;
  325. class Restricted
  326. {
  327. }
  328. """