SimpleInvocation.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /*
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. //require 'Yay/Invocation.php';
  15. //require 'Yay/MockGenerator.php';
  16. /**
  17. * The standard implementation of the Invocation interface.
  18. * @author Chris Corbyn <chris@w3style.co.uk>
  19. * @package Yay
  20. */
  21. class Yay_SimpleInvocation implements Yay_Invocation
  22. {
  23. /**
  24. * The Object on which the Inovation occurred.
  25. * @var object
  26. * @access private
  27. */
  28. private $_object;
  29. /**
  30. * The method name invoked.
  31. * @var string
  32. * @access private
  33. */
  34. private $_method;
  35. /**
  36. * The arguments in the Invocation.
  37. * @var array
  38. * @access private
  39. */
  40. private $_arguments;
  41. /**
  42. * Create a new SimpleInvocation with the given details.
  43. * @param object $object
  44. * @param string $method
  45. * @param array &$arguments
  46. */
  47. public function __construct($object, $method, array &$arguments)
  48. {
  49. $this->_object = $object;
  50. //Massage __call() overloading so the interface is tested correctly
  51. if ($method == '__call')
  52. {
  53. $method = array_shift($arguments);
  54. $args =& array_shift($arguments);
  55. $arguments =& $args;
  56. }
  57. $this->_method = $method;
  58. $this->_arguments =& $arguments;
  59. }
  60. /**
  61. * Get the object which this Invocation occured on.
  62. * @return object
  63. */
  64. public function getObject()
  65. {
  66. return $this->_object;
  67. }
  68. /**
  69. * Get the method name of the invoked method.
  70. * @return string
  71. */
  72. public function getMethod()
  73. {
  74. return $this->_method;
  75. }
  76. /**
  77. * Get the argument list in the Invocation.
  78. * @return array
  79. */
  80. public function &getArguments()
  81. {
  82. return $this->_arguments;
  83. }
  84. /**
  85. * Describe this Invocation to $description.
  86. * @param Yay_Description $description
  87. */
  88. public function describeTo(Yay_Description $description)
  89. {
  90. $description->appendText(sprintf(' of %s;', $this->_getInvocationSignature()));
  91. }
  92. // -- Private methods
  93. private function _getInvocationSignature()
  94. {
  95. $class = Yay_MockGenerator::getInstance()
  96. ->reverseNamingScheme(get_class($this->_object));
  97. if (!empty($this->_arguments))
  98. {
  99. $args = array();
  100. foreach ($this->_arguments as $arg)
  101. {
  102. $args[] = $this->_describeArgument($arg, '%s [%s]');
  103. }
  104. $params = implode(', ', $args);
  105. }
  106. else
  107. {
  108. $params = '';
  109. }
  110. return sprintf('%s::%s(%s)', $class, $this->_method, $params);
  111. }
  112. private function _describeArgument($arg, $format)
  113. {
  114. $description = '';
  115. if (is_int($arg))
  116. {
  117. $description = sprintf($format, 'int', $arg);
  118. }
  119. elseif (is_float($arg))
  120. {
  121. $description = sprintf($format, 'float', preg_replace('/^(.{8}).+/', '$1..', $arg));
  122. }
  123. elseif (is_numeric($arg))
  124. {
  125. $description = sprintf($format, 'number', preg_replace('/^(.{8}).+/', '$1..', $arg));
  126. }
  127. elseif (is_string($arg))
  128. {
  129. $description = sprintf($format, 'string', preg_replace('/^(.{8}).+/', '$1..', $arg));
  130. }
  131. elseif (is_object($arg))
  132. {
  133. $description = sprintf($format, 'object', get_class($arg));
  134. }
  135. elseif (is_array($arg))
  136. {
  137. $description = sprintf($format, 'array', count($arg) . ' items');
  138. }
  139. else
  140. {
  141. $description = sprintf($format, gettype($arg), preg_replace('/^(.{8}).+/', '$1..', (string) $arg));
  142. }
  143. return $description;
  144. }
  145. }