Test.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\console\command\create;
  9. use lithium\core\Libraries;
  10. use lithium\util\Inflector;
  11. use lithium\analysis\Inspector;
  12. use lithium\core\ClassNotFoundException;
  13. /**
  14. * Generate a Test class in the `--library` namespace
  15. *
  16. * `li3 create test model Posts`
  17. * `li3 create --library=li3_plugin test model Posts`
  18. *
  19. */
  20. class Test extends \lithium\console\command\Create {
  21. /**
  22. * Get the namespace for the test case.
  23. *
  24. * @param string $request
  25. * @param array $options
  26. * @return string
  27. */
  28. protected function _namespace($request, $options = array()) {
  29. $request->params['command'] = $request->action;
  30. return parent::_namespace($request, array('prepend' => 'tests.cases.'));
  31. }
  32. /**
  33. * Get the class used by the test case.
  34. *
  35. * @param string $request
  36. * @return string
  37. */
  38. protected function _use($request) {
  39. return parent::_namespace($request) . '\\' . $this->_name($request);
  40. }
  41. /**
  42. * Get the class name for the test case.
  43. *
  44. * @param string $request
  45. * @return string
  46. */
  47. protected function _class($request) {
  48. $name = $this->_name($request);
  49. return Inflector::classify("{$name}Test");
  50. }
  51. /**
  52. * Get the methods to test.
  53. *
  54. * @param string $request
  55. * @return string
  56. */
  57. protected function _methods($request) {
  58. $use = $this->_use($request);
  59. $path = Libraries::path($use);
  60. if (!file_exists($path)) {
  61. return "";
  62. }
  63. $methods = (array) Inspector::methods($use, 'extents');
  64. $testMethods = array();
  65. foreach (array_keys($methods) as $method) {
  66. $testMethods[] = "\tpublic function test" . ucwords($method) . "() {}";
  67. }
  68. return join("\n", $testMethods);
  69. }
  70. /**
  71. * Get the class to be tested
  72. *
  73. * @param string $request
  74. * @return string
  75. */
  76. protected function _name($request) {
  77. $type = $request->action;
  78. $name = $request->args();
  79. try {
  80. $command = $this->_instance($type);
  81. } catch (ClassNotFoundException $e) {
  82. $command = null;
  83. }
  84. if ($command) {
  85. $request->params['action'] = $name;
  86. $name = $command->invokeMethod('_class', array($request));
  87. }
  88. $request->params['action'] = $type;
  89. return $name;
  90. }
  91. }
  92. ?>