LimeMockInvocation.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /*
  3. * This file is part of the Lime test framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * Represents the invocation of a class or object method with a set of
  13. * parameters.
  14. *
  15. * This class is used internally by LimeMockControl to track the method
  16. * invocations on mock objects.
  17. *
  18. * @package Lime
  19. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  20. * @version SVN: $Id: LimeMockInvocation.php 23880 2009-11-14 10:14:34Z bschussek $
  21. */
  22. class LimeMockInvocation implements LimeMockMethodInterface
  23. {
  24. protected
  25. $method = null,
  26. $parameters = array();
  27. /**
  28. * Constructor.
  29. *
  30. * @param string $method The method name
  31. * @param array $parameters The method parameters
  32. */
  33. public function __construct(LimeMockMethod $method, array $parameters = array())
  34. {
  35. $this->method = $method;
  36. $this->parameters = $parameters;
  37. }
  38. /**
  39. * Returns the class name.
  40. *
  41. * @return string
  42. */
  43. public function getClass()
  44. {
  45. return $this->method->getClass();
  46. }
  47. /**
  48. * Returns the method name.
  49. *
  50. * @return string
  51. */
  52. public function getMethod()
  53. {
  54. return $this->method->getMethod();
  55. }
  56. /**
  57. * Returns the method parameters.
  58. *
  59. * @return array The parameter array
  60. */
  61. public function getParameters()
  62. {
  63. return $this->parameters;
  64. }
  65. /**
  66. * Returns the parameter at the given index.
  67. *
  68. * @param integer $index
  69. * @return mixed
  70. */
  71. public function getParameter($index)
  72. {
  73. if ($index >= count($this->parameters))
  74. {
  75. throw new OutOfRangeException(sprintf('The parameter %s does not exist', $index));
  76. }
  77. return $this->parameters[$index];
  78. }
  79. /**
  80. * Returns a string representation of the method call invocation.
  81. *
  82. * The result looks like a method call in PHP source code.
  83. *
  84. * Example:
  85. * <code>
  86. * $invocation = new LimeMockMethodInvocation('doSomething', array(1, 'foobar'));
  87. * print $invocation;
  88. *
  89. * // => "doSomething(1, 'foobar')"
  90. * </code>
  91. *
  92. * @return string
  93. */
  94. public function __toString()
  95. {
  96. $parameters = $this->parameters;
  97. if (is_array($parameters))
  98. {
  99. foreach ($parameters as $key => $value)
  100. {
  101. if (is_string($value))
  102. {
  103. $value = str_replace(array("\0", "\n", "\t", "\r"), array('\0', '\n', '\t', '\r'), $value);
  104. $value = strlen($value) > 30 ? substr($value, 0, 30).'...' : $value;
  105. $parameters[$key] = '"'.$value.'"';
  106. }
  107. else if (is_object($value))
  108. {
  109. $parameters[$key] = get_class($value);
  110. }
  111. else if (is_array($value))
  112. {
  113. $parameters[$key] = 'array';
  114. }
  115. else
  116. {
  117. $parameters[$key] = var_export($value, true);
  118. }
  119. }
  120. }
  121. return sprintf('%s(%s)', $this->method->getMethod(), implode(', ', (array)$parameters));
  122. }
  123. }