/tests/units/classes/asserters/mock/call/mock.php

https://github.com/tharkun/atoum · PHP · 175 lines · 162 code · 13 blank · 0 comment · 0 complexity · ab3b56374f2e56203c25575e00399ae6 MD5 · raw file

  1. <?php
  2. namespace mageekguy\atoum\tests\units\asserters\mock\call;
  3. require_once __DIR__ . '/../../../../runner.php';
  4. use
  5. mageekguy\atoum,
  6. mageekguy\atoum\asserter,
  7. mageekguy\atoum\asserters,
  8. mageekguy\atoum\asserters\mock\call\mock as testedClass
  9. ;
  10. class dummy
  11. {
  12. public function foo() {}
  13. }
  14. class mock extends atoum\test
  15. {
  16. public function testClass()
  17. {
  18. $this->testedClass->extends('mageekguy\atoum\php\call');
  19. }
  20. public function test__construct()
  21. {
  22. $this
  23. ->if($call = new testedClass(
  24. $mockAsserter = new asserters\mock(new asserter\generator()),
  25. $mockAggregator = new \mock\mageekguy\atoum\tests\units\asserters\mock\call\dummy(),
  26. $function = uniqid()
  27. )
  28. )
  29. ->then
  30. ->object($call->getMockAsserter())->isIdenticalTo($mockAsserter)
  31. ->object($call->getObject())->isIdenticalTo($mockAggregator)
  32. ->string($call->getFunction())->isEqualTo($function)
  33. ->variable($call->getArguments())->isNull()
  34. ;
  35. }
  36. public function test__call()
  37. {
  38. $this
  39. ->if($call = new testedClass(
  40. $mockAsserter = new \mock\mageekguy\atoum\asserters\mock(new asserter\generator()),
  41. new \mock\mageekguy\atoum\tests\units\asserters\mock\call\dummy(),
  42. uniqid()
  43. )
  44. )
  45. ->and($mockAsserter->getMockController()->call = $mockAsserter)
  46. ->then
  47. ->object($call->call($arg = uniqid()))->isIdenticalTo($mockAsserter)
  48. ->mock($mockAsserter)
  49. ->call('call')->withArguments($arg)->once()
  50. ->if($unknownMethod = uniqid())
  51. ->then
  52. ->exception(function() use ($call, $unknownMethod) {
  53. $call->{$unknownMethod}();
  54. }
  55. )
  56. ->isInstanceOf('mageekguy\atoum\exceptions\logic\invalidArgument')
  57. ->hasMessage('Method ' . get_class($mockAsserter) . '::' . $unknownMethod . '() does not exist')
  58. ;
  59. }
  60. public function test__toString()
  61. {
  62. $this
  63. ->if($call = new testedClass(
  64. new asserters\mock(new asserter\generator()),
  65. $mockAggregator = new \mock\mageekguy\atoum\tests\units\asserters\mock\call\dummy(),
  66. $function = uniqid()
  67. )
  68. )
  69. ->then
  70. ->castToString($call)->isEqualTo(get_class($mockAggregator) . '::' . $function . '()')
  71. ;
  72. }
  73. public function testWithArguments()
  74. {
  75. $this
  76. ->if($call = new testedClass(
  77. new asserters\mock(new asserter\generator()),
  78. new \mock\mageekguy\atoum\tests\units\asserters\mock\call\dummy(),
  79. uniqid()
  80. )
  81. )
  82. ->then
  83. ->object($call->withArguments($arg = uniqid()))->isIdenticalTo($call)
  84. ->array($call->getArguments())->isEqualTo(array($arg))
  85. ->object($call->withArguments($arg1 = uniqid(), $arg2 = uniqid()))->isIdenticalTo($call)
  86. ->array($call->getArguments())->isEqualTo(array($arg1, $arg2))
  87. ;
  88. }
  89. public function testWithAtLeastArguments()
  90. {
  91. $this
  92. ->if($call = new testedClass(
  93. new asserters\mock(new asserter\generator()),
  94. new \mock\mageekguy\atoum\tests\units\asserters\mock\call\dummy(),
  95. uniqid()
  96. )
  97. )
  98. ->then
  99. ->object($call->withAtLeastArguments($arguments = array(1 => uniqid(), 3 => uniqid())))->isIdenticalTo($call)
  100. ->array($call->getArguments())->isEqualTo(array($arguments))
  101. ->object($call->withAtLeastArguments($otherArguments = array(1 => uniqid(), 3 => uniqid())))->isIdenticalTo($call)
  102. ->array($call->getArguments())->isEqualTo(array($otherArguments))
  103. ;
  104. }
  105. public function testOn()
  106. {
  107. $this
  108. ->if($call = new testedClass(
  109. new asserters\mock(new asserter\generator()),
  110. new \mock\mageekguy\atoum\tests\units\asserters\mock\call\dummy(),
  111. uniqid()
  112. )
  113. )
  114. ->and($mockAggregator = new \mock\mageekguy\atoum\tests\units\asserters\mock\call\dummy())
  115. ->then
  116. ->object($call->on($mockAggregator))->isIdenticalTo($call)
  117. ->object($call->getObject())->isIdenticalTo($mockAggregator)
  118. ;
  119. }
  120. public function testGetFirstCall()
  121. {
  122. $this
  123. ->if($call = new testedClass(
  124. new asserters\mock(new asserter\generator()),
  125. $mock = new \mock\mageekguy\atoum\tests\units\asserters\mock\call\dummy(),
  126. 'foo'
  127. )
  128. )
  129. ->then
  130. ->variable($call->getFirstCall())->isNull()
  131. ->if($otherMock = new \mock\mageekguy\atoum\tests\units\asserters\mock\call\dummy())
  132. ->and($otherMock->foo())
  133. ->then
  134. ->variable($call->getFirstCall())->isNull()
  135. ->when(function() use ($mock) { $mock->foo(); })
  136. ->integer($call->getFirstCall())->isEqualTo(2)
  137. ->when(function() use ($mock) { $mock->foo(); })
  138. ->integer($call->getFirstCall())->isEqualTo(2)
  139. ;
  140. }
  141. public function testGetLastCall()
  142. {
  143. $this
  144. ->if($call = new testedClass(
  145. new asserters\mock(new asserter\generator()),
  146. $mock = new \mock\mageekguy\atoum\tests\units\asserters\mock\call\dummy(),
  147. 'foo'
  148. )
  149. )
  150. ->then
  151. ->variable($call->getLastCall())->isNull()
  152. ->if($otherMock = new \mock\mageekguy\atoum\tests\units\asserters\mock\call\dummy())
  153. ->and($otherMock->foo())
  154. ->then
  155. ->variable($call->getLastCall())->isNull()
  156. ->when(function() use ($mock) { $mock->foo(); })
  157. ->integer($call->getLastCall())->isEqualTo(2)
  158. ->when(function() use ($mock) { $mock->foo(); })
  159. ->integer($call->getLastCall())->isEqualTo(3)
  160. ;
  161. }
  162. }