/tests/units/classes/php/mocker.php

https://github.com/agallou/atoum · PHP · 190 lines · 163 code · 27 blank · 0 comment · 0 complexity · 93a298f3932b1ef3e748c75044e0a771 MD5 · raw file

  1. <?php
  2. namespace mageekguy\atoum\tests\units\php;
  3. require_once __DIR__ . '/../../runner.php';
  4. use
  5. mageekguy\atoum,
  6. mageekguy\atoum\php
  7. ;
  8. function doesSomething() {}
  9. class mocker extends atoum\test
  10. {
  11. public function test__construct()
  12. {
  13. $this
  14. ->given($this->newTestedInstance)
  15. ->then
  16. ->string($this->testedInstance->getDefaultNamespace())->isEmpty()
  17. ;
  18. }
  19. public function test__set()
  20. {
  21. $this
  22. ->given($this->newTestedInstance)
  23. ->if($this->testedInstance->{$functionName = __NAMESPACE__ . '\version_compare'} = $returnValue = uniqid())
  24. ->then
  25. ->string(version_compare(uniqid(), uniqid()))->isEqualTo($returnValue)
  26. ->if($this->testedInstance->{$functionName = __NAMESPACE__ . '\version_compare'} = $otherReturnValue = uniqid())
  27. ->then
  28. ->string(version_compare(uniqid(), uniqid()))->isEqualTo($otherReturnValue)
  29. ->if($this->testedInstance->{$otherFunctionName = __NAMESPACE__ . '\file_get_contents'} = $fileContents = uniqid())
  30. ->then
  31. ->string(version_compare(uniqid(), uniqid()))->isEqualTo($otherReturnValue)
  32. ->string(file_get_contents(uniqid()))->isEqualTo($fileContents)
  33. ;
  34. }
  35. public function test__get()
  36. {
  37. $this
  38. ->given($this->newTestedInstance)
  39. ->then
  40. ->object($this->testedInstance->{$functionName = __NAMESPACE__ . '\version_compare'})->isInstanceOf('mageekguy\atoum\test\adapter\invoker')
  41. ->boolean(function_exists($functionName))->isTrue()
  42. ;
  43. }
  44. public function test__isset()
  45. {
  46. $this
  47. ->given($this->newTestedInstance)
  48. ->then
  49. ->boolean(isset($this->testedInstance->{$functionName = __NAMESPACE__ . '\version_compare'}))->isFalse()
  50. ->if($this->testedInstance->generate($functionName))
  51. ->then
  52. ->boolean(isset($this->testedInstance->{$functionName}))->isTrue()
  53. ;
  54. }
  55. public function test__unset()
  56. {
  57. $this
  58. ->given($mocker = $this->newTestedInstance)
  59. ->if($functionName = __NAMESPACE__ . '\version_compare')
  60. ->when(function() use ($mocker, $functionName) { unset($mocker->{$functionName}); })
  61. ->then
  62. ->boolean(function_exists($functionName))->isFalse()
  63. ->if($this->testedInstance->{$functionName} = uniqid())
  64. ->when(function() use ($mocker, $functionName) { unset($mocker->{$functionName}); })
  65. ->then
  66. ->integer(version_compare('5.4.0', '5.3.0'))->isEqualTo(1)
  67. ->integer(version_compare('5.3.0', '5.4.0'))->isEqualTo(-1)
  68. ;
  69. }
  70. public function testSetDefaultNamespace()
  71. {
  72. $this
  73. ->given($this->newTestedInstance)
  74. ->then
  75. ->object($this->testedInstance->setDefaultNamespace($defaultNamespace = uniqid()))->isTestedInstance
  76. ->string($this->testedInstance->getDefaultNamespace())->isEqualTo($defaultNamespace . '\\')
  77. ->object($this->testedInstance->setDefaultNamespace($defaultNamespace = uniqid() . '\\'))->isTestedInstance
  78. ->string($this->testedInstance->getDefaultNamespace())->isEqualTo($defaultNamespace)
  79. ->object($this->testedInstance->setDefaultNamespace('\\' . ($defaultNamespace = uniqid())))->isTestedInstance
  80. ->string($this->testedInstance->getDefaultNamespace())->isEqualTo($defaultNamespace . '\\')
  81. ->object($this->testedInstance->setDefaultNamespace('\\' . ($defaultNamespace = uniqid() . '\\')))->isTestedInstance
  82. ->string($this->testedInstance->getDefaultNamespace())->isEqualTo($defaultNamespace)
  83. ->object($this->testedInstance->setDefaultNamespace(''))->isTestedInstance
  84. ->string($this->testedInstance->getDefaultNamespace())->isEmpty()
  85. ;
  86. }
  87. public function testUseClassNamespace()
  88. {
  89. $this
  90. ->given($this->newTestedInstance)
  91. ->then
  92. ->object($this->testedInstance->useClassNamespace(__CLASS__))->isTestedInstance
  93. ->string($this->testedInstance->getDefaultNamespace())->isEqualTo(__NAMESPACE__ . '\\')
  94. ;
  95. }
  96. public function testResetCalls()
  97. {
  98. $this
  99. ->given($this->newTestedInstance)
  100. ->if(
  101. $this->mockGenerator
  102. ->orphanize('__construct')
  103. ->orphanize('resetCalls'),
  104. php\mocker::setAdapter($adapter = new \mock\atoum\test\adapter())
  105. )
  106. ->then
  107. ->object($this->testedInstance->resetCalls())->isTestedInstance
  108. ->mock($adapter)->call('resetCalls')->once
  109. ->object($this->testedInstance->resetCalls($functionName = uniqid()))->isTestedInstance
  110. ->mock($adapter)->call('resetCalls')->withArguments($functionName)->once
  111. ->if($this->testedInstance->setDefaultNamespace($defaultNamespace = uniqid()))
  112. ->then
  113. ->object($this->testedInstance->resetCalls($functionName = uniqid()))->isTestedInstance
  114. ->mock($adapter)->call('resetCalls')->withArguments($defaultNamespace . '\\' . $functionName)->once
  115. ;
  116. }
  117. public function testSetAdapter()
  118. {
  119. $this
  120. ->variable(php\mocker::setAdapter($adapter = new atoum\php\mocker\adapter()))->isNull()
  121. ->object(php\mocker::getAdapter())->isIdenticalTo($adapter)
  122. ->variable(php\mocker::setAdapter())->isNull()
  123. ->object(php\mocker::getAdapter())
  124. ->isNotIdenticalTo($adapter)
  125. ->isEqualTo(new atoum\php\mocker\adapter())
  126. ;
  127. }
  128. public function testGenerate()
  129. {
  130. $this
  131. ->given($mocker = $this->newTestedInstance)
  132. ->then
  133. ->object($this->testedInstance->generate($functionName = __NAMESPACE__ . '\version_compare'))->isIdenticalTo($this->testedInstance)
  134. ->boolean(function_exists($functionName))->isTrue()
  135. ->boolean(version_compare('5.4.0', '5.3.0'))->isFalse()
  136. ->integer(\version_compare('5.4.0', '5.3.0'))->isEqualTo(1)
  137. ->boolean(version_compare('5.3.0', '5.4.0'))->isTrue()
  138. ->integer(\version_compare('5.3.0', '5.4.0'))->isEqualTo(-1)
  139. ->exception(function() use ($mocker) { $mocker->generate(__NAMESPACE__ . '\doesSomething'); })
  140. ->isInstanceof('mageekguy\atoum\exceptions\logic\invalidArgument')
  141. ->hasMessage('Function \'' . __NAMESPACE__ . '\doesSomething\' already exists')
  142. ->if($this->testedInstance->{$functionName} = $returnValue = uniqid())
  143. ->then
  144. ->string(version_compare(uniqid(), uniqid()))->isEqualTo($returnValue)
  145. ->object($this->testedInstance->generate($functionName = __NAMESPACE__ . '\version_compare'))->isIdenticalTo($this->testedInstance)
  146. ->boolean(version_compare('5.4.0', '5.3.0'))->isFalse()
  147. ->boolean(version_compare('5.3.0', '5.4.0'))->isTrue()
  148. ->object($this->testedInstance->generate($unknownFunctionName = __NAMESPACE__ . '\\foo'))->isIdenticalTo($this->testedInstance)
  149. ->variable(foo())->isNull()
  150. ->if($this->testedInstance->{$unknownFunctionName} = $fooReturnValue = uniqid())
  151. ->then
  152. ->string(foo())->isEqualTo($fooReturnValue)
  153. ->if($this->testedInstance->{$functionName} = $returnValue = uniqid())
  154. ->when(function() use ($mocker, $functionName) { unset($mocker->{$functionName}); })
  155. ->then
  156. ->boolean(version_compare('5.4.0', '5.3.0'))->isFalse()
  157. ->boolean(version_compare('5.3.0', '5.4.0'))->isTrue()
  158. ->when(function() use ($mocker, $unknownFunctionName) { unset($mocker->{$unknownFunctionName}); })
  159. ->then
  160. ->variable(foo())->isNull()
  161. ;
  162. }
  163. }