SwiftUnitTestCase.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. require_once 'Swift/Tests/IdenticalBinaryExpectation.php';
  3. /**
  4. * A base test case with some custom expectations.
  5. * @package Swift
  6. * @subpackage Tests
  7. * @author Chris Corbyn
  8. */
  9. class Swift_Tests_SwiftUnitTestCase extends UnitTestCase
  10. {
  11. /** An instance of the Yay_Mockery class */
  12. private $_mockery;
  13. /**
  14. * Decorates SimpleTest's implementation to auto-validate mock objects.
  15. */
  16. public function after($method)
  17. {
  18. try {
  19. $this->_mockery()->assertIsSatisfied();
  20. } catch (Yay_NotSatisfiedException $e) {
  21. $this->fail($e->getMessage());
  22. }
  23. $this->_mockery = null;
  24. return parent::after($method);
  25. }
  26. /**
  27. * Assert two binary strings are an exact match.
  28. * @param string $a
  29. * @param string $b
  30. * @param string $s formatted message
  31. */
  32. public function assertIdenticalBinary($a, $b, $s = '%s')
  33. {
  34. return $this->assert(new Swift_Tests_IdenticalBinaryExpectation($a), $b, $s);
  35. }
  36. // -- Protected methods
  37. /**
  38. * Returns a singleton-per-test method for Yay_Mockery.
  39. * @return Yay_Mockery
  40. */
  41. protected function _mockery()
  42. {
  43. if (!isset($this->_mockery)) {
  44. $this->_mockery = new Yay_Mockery();
  45. }
  46. return $this->_mockery;
  47. }
  48. /**
  49. * Create a mock object.
  50. * @param string $class
  51. * @return Yay_Mock
  52. */
  53. protected function _mock($class)
  54. {
  55. return $this->_mockery()->mock($class);
  56. }
  57. /**
  58. * Add mock expectations.
  59. * @param Yay_Expectations $expectations
  60. */
  61. protected function _checking($expectations)
  62. {
  63. return $this->_mockery()->checking($expectations);
  64. }
  65. /**
  66. * Create a mock object which does nothing.
  67. * @param string $class
  68. * @return Yay_Mock
  69. */
  70. protected function _stub($class)
  71. {
  72. $stub = $this->_mockery()->mock($class);
  73. $this->_mockery()->checking(Yay_Expectations::create()
  74. -> ignoring($stub)
  75. );
  76. return $stub;
  77. }
  78. protected function _states($machineName)
  79. {
  80. return $this->_mockery()->states($machineName);
  81. }
  82. protected function _sequence($sequenceName)
  83. {
  84. return $this->_mockery()->sequence($sequenceName);
  85. }
  86. }