MockerChainTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\test;
  9. use lithium\test\Mocker;
  10. class MockerChainTest extends \lithium\test\Unit {
  11. public function setUp() {
  12. Mocker::register();
  13. }
  14. public function testStartSuccessful() {
  15. $mock = new \lithium\tests\mocks\test\mockStdClass\Mock();
  16. $chain = Mocker::chain($mock);
  17. $this->assertTrue($chain->success());
  18. }
  19. public function testBasicNotCalled() {
  20. $mock = new \lithium\tests\mocks\test\mockStdClass\Mock();
  21. $chain = Mocker::chain($mock);
  22. $this->assertFalse($chain->called('method1')->success());
  23. }
  24. public function testBasicCalled() {
  25. $mock = new \lithium\tests\mocks\test\mockStdClass\Mock();
  26. $mock->method1();
  27. $chain = Mocker::chain($mock);
  28. $this->assertTrue($chain->called('method1')->success());
  29. }
  30. public function testCalledWith() {
  31. $mock = new \lithium\tests\mocks\test\mockStdClass\Mock();
  32. $mock->method1('foo');
  33. $chain = Mocker::chain($mock);
  34. $this->assertTrue($chain->called('method1')->success());
  35. $this->assertFalse($chain->called('method1')->with('bar')->success());
  36. $this->assertTrue($chain->called('method1')->with('foo')->success());
  37. $this->assertFalse($chain->called('method1')->with('foo', 'bar')->success());
  38. }
  39. public function testMethodCalledBefore() {
  40. $mock = new \lithium\tests\mocks\test\mockStdClass\Mock();
  41. $mock->method1();
  42. $mock->method2();
  43. $mock->method1();
  44. $chain = Mocker::chain($mock);
  45. $this->assertTrue($chain->called('method1')
  46. ->called('method2')
  47. ->called('method1')
  48. ->success());
  49. $this->assertFalse($chain->called('method2')
  50. ->called('method1')
  51. ->called('method1')
  52. ->success());
  53. }
  54. public function testMethodWithParamsCalledBefore() {
  55. $mock = new \lithium\tests\mocks\test\mockStdClass\Mock();
  56. $mock->method1('foo');
  57. $mock->method2('bar');
  58. $mock->method1('baz');
  59. $chain = Mocker::chain($mock);
  60. $this->assertTrue($chain->called('method1')
  61. ->called('method2')->with('bar')
  62. ->called('method1')
  63. ->success());
  64. $this->assertFalse($chain->called('method1')->with('bar')
  65. ->called('method2')->with('bar')
  66. ->called('method1')
  67. ->success());
  68. $this->assertFalse($chain->called('method1')
  69. ->called('method2')->with('bar')
  70. ->called('method1')->with('bar')
  71. ->success());
  72. }
  73. public function testMethodCalledSpecificTimes() {
  74. $mock = new \lithium\tests\mocks\test\mockStdClass\Mock();
  75. $mock->method1();
  76. $mock->method2();
  77. $mock->method1();
  78. $chain = Mocker::chain($mock);
  79. $this->assertFalse($chain->called('method2')->eq(2)->success());
  80. $this->assertTrue($chain->called('method1')->eq(2)->success());
  81. $this->assertTrue($chain->called('method1')->gt(0)->success());
  82. $this->assertTrue($chain->called('method1')->gte(1)->success());
  83. $this->assertTrue($chain->called('method1')->lt(3)->success());
  84. $this->assertTrue($chain->called('method1')->lte(2)->success());
  85. $this->assertFalse($chain->called('method1')->lte(1)->success());
  86. }
  87. public function testMultipleCallsWithArgsAndSpecificCalled() {
  88. $mock = new \lithium\tests\mocks\test\mockStdClass\Mock();
  89. $mock->method1('foo', 'bar');
  90. $mock->method1('foo', 'bar');
  91. $mock->method1('foo', 'bar');
  92. $mock->method2('baz');
  93. $mock->method2('baz');
  94. $mock->method1();
  95. $chain = Mocker::chain($mock);
  96. $this->assertTrue($chain->called('method1')->with('foo', 'bar')->eq(3)->success());
  97. $this->assertTrue($chain->called('method2')->with('baz')->eq(2)->success());
  98. $this->assertTrue($chain->called('method1')->with()->eq(1)->success());
  99. $this->assertTrue($chain->called('method1')->with('foo', 'bar')->eq(3)
  100. ->called('method2')->with('baz')->eq(2)
  101. ->called('method1')->with()->eq(1)->success());
  102. }
  103. public function testRespondsToParentCall() {
  104. $chain = Mocker::chain(array());
  105. $this->assertTrue($chain->respondsTo('applyFilter'));
  106. $this->assertFalse($chain->respondsTo('fooBarBaz'));
  107. }
  108. public function testRespondsToMagic() {
  109. $chain = Mocker::chain(array());
  110. $this->assertTrue($chain->respondsTo('gt'));
  111. $this->assertTrue($chain->respondsTo('lt'));
  112. $this->assertFalse($chain->respondsTo('et'));
  113. }
  114. }
  115. ?>