PageRenderTime 26ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/units/classes/tools/variable/analyzer.php

https://github.com/agallou/atoum
PHP | 223 lines | 183 code | 40 blank | 0 comment | 1 complexity | 5fc5fffabb631722c066308f99422692 MD5 | raw file
  1. <?php
  2. namespace mageekguy\atoum\tests\units\tools\variable;
  3. require_once __DIR__ . '/../../../runner.php';
  4. use
  5. mageekguy\atoum
  6. ;
  7. class analyzer extends atoum\test
  8. {
  9. public function testGetType()
  10. {
  11. $this
  12. ->given($this->newTestedInstance)
  13. ->then
  14. ->if($this->function->gettype = 'boolean')
  15. ->then
  16. ->string($this->testedInstance->getTypeOf(true))->isEqualTo('boolean(true)')
  17. ->string($this->testedInstance->getTypeOf(false))->isEqualTo('boolean(false)')
  18. ->if($this->function->gettype = 'integer')
  19. ->then
  20. ->string($this->testedInstance->getTypeOf($integer = uniqid()))->isEqualTo('integer(' . $integer . ')')
  21. ->if($this->function->gettype = 'double')
  22. ->then
  23. ->string($this->testedInstance->getTypeOf($float = uniqid()))->isEqualTo('float(' . $float . ')')
  24. ->if($this->function->gettype = 'NULL')
  25. ->then
  26. ->string($this->testedInstance->getTypeOf($float = uniqid()))->isEqualTo('null')
  27. ->if(
  28. $this->function->gettype = 'object',
  29. $this->function->get_class = $class = uniqid()
  30. )
  31. ->then
  32. ->string($this->testedInstance->getTypeOf(uniqid()))->isEqualTo('object(' . $class . ')')
  33. ->if(
  34. $this->function->gettype = 'resource',
  35. $this->function->get_resource_type = $type = uniqid()
  36. )
  37. ->then
  38. ->string($this->testedInstance->getTypeOf($resource = uniqid()))->isEqualTo($resource . ' of type ' . $type)
  39. ->if($this->function->gettype = 'string')
  40. ->then
  41. ->string($this->testedInstance->getTypeOf($string = uniqid()))->isEqualTo('string(' . strlen($string) . ') \'' . $string . '\'')
  42. ->if(
  43. $this->function->gettype = 'array',
  44. $this->function->sizeof = $size = rand(1, PHP_INT_MAX)
  45. )
  46. ->then
  47. ->string($this->testedInstance->getTypeOf(uniqid()))->isEqualTo('array(' . $size . ')')
  48. ;
  49. }
  50. public function testDump()
  51. {
  52. $this
  53. ->given($this->newTestedInstance)
  54. ->if(
  55. $this->function->ob_start->doesNothing(),
  56. $this->function->var_dump->doesNothing(),
  57. $this->function->ob_get_clean = (' ' . ($dump = uniqid()) . ' ' . PHP_EOL)
  58. )
  59. ->then
  60. ->string($this->testedInstance->dump($this))->isEqualTo($dump)
  61. ;
  62. }
  63. public function testIsObject()
  64. {
  65. $this
  66. ->given($this->newTestedInstance)
  67. ->if($this->function->is_object = false)
  68. ->then
  69. ->boolean($this->testedInstance->isObject($mixed = uniqid()))->isFalse
  70. ->function('is_object')->wasCalledWithArguments($mixed)->once
  71. ->if($this->function->is_object = true)
  72. ->then
  73. ->boolean($this->testedInstance->isObject($mixed = uniqid()))->isTrue
  74. ->function('is_object')->wasCalledWithArguments($mixed)->once
  75. ;
  76. }
  77. public function testIsBoolean()
  78. {
  79. $this
  80. ->given($this->newTestedInstance)
  81. ->if($this->function->is_bool[1] = false)
  82. ->then
  83. ->boolean($this->testedInstance->isBoolean($mixed = uniqid()))->isFalse
  84. ->function('is_bool')->wasCalledWithIdenticalArguments($mixed)->once
  85. ->if($this->function->is_bool[3] = true)
  86. ->then
  87. ->boolean($this->testedInstance->isBoolean($mixed = uniqid()))->isTrue
  88. ->function('is_bool')->wasCalledWithIdenticalArguments($mixed)->once
  89. ;
  90. }
  91. public function testIsInteger()
  92. {
  93. $this
  94. ->given($this->newTestedInstance)
  95. ->if($this->function->is_int = false)
  96. ->then
  97. ->boolean($this->testedInstance->isInteger($mixed = uniqid()))->isFalse
  98. ->function('is_int')->wasCalledWithArguments($mixed)->once
  99. ->if($this->function->is_int = true)
  100. ->then
  101. ->boolean($this->testedInstance->isInteger($mixed = uniqid()))->isTrue
  102. ->function('is_int')->wasCalledWithArguments($mixed)->once
  103. ;
  104. }
  105. public function testIsFloat()
  106. {
  107. $this
  108. ->given($this->newTestedInstance)
  109. ->if($this->function->is_float = false)
  110. ->then
  111. ->boolean($this->testedInstance->isFloat($mixed = uniqid()))->isFalse
  112. ->function('is_float')->wasCalledWithArguments($mixed)->once
  113. ->if($this->function->is_float = true)
  114. ->then
  115. ->boolean($this->testedInstance->isFloat($mixed = uniqid()))->isTrue
  116. ->function('is_float')->wasCalledWithArguments($mixed)->once
  117. ;
  118. }
  119. public function testIsString()
  120. {
  121. $this
  122. ->given($this->newTestedInstance)
  123. ->if($this->function->is_string = false)
  124. ->then
  125. ->boolean($this->testedInstance->isString($mixed = uniqid()))->isFalse
  126. ->function('is_string')->wasCalledWithArguments($mixed)->once
  127. ->if($this->function->is_string = true)
  128. ->then
  129. ->boolean($this->testedInstance->isString($mixed = uniqid()))->isTrue
  130. ->function('is_string')->wasCalledWithArguments($mixed)->once
  131. ;
  132. }
  133. public function testIsArray()
  134. {
  135. $this
  136. ->given($this->newTestedInstance)
  137. ->if($this->function->is_array = false)
  138. ->then
  139. ->boolean($this->testedInstance->isArray($mixed = uniqid()))->isFalse
  140. ->function('is_array')->wasCalledWithArguments($mixed)->once
  141. ->if($this->function->is_array = true)
  142. ->then
  143. ->boolean($this->testedInstance->isArray($mixed = uniqid()))->isTrue
  144. ->function('is_array')->wasCalledWithArguments($mixed)->once
  145. ;
  146. }
  147. public function testIsUtf8()
  148. {
  149. $this
  150. ->given($this->newTestedInstance)
  151. ->if(
  152. $this->function->is_string = false,
  153. $this->function->preg_match = 0
  154. )
  155. ->then
  156. ->boolean($this->testedInstance->isUtf8($mixed = uniqid()))->isFalse
  157. ->function('is_string')->wasCalledWithArguments($mixed)->once
  158. ->function('preg_match')->wasCalledWithArguments('/^.*$/us', $mixed)->never
  159. ->if($this->function->is_string = true)
  160. ->then
  161. ->boolean($this->testedInstance->isUtf8($mixed = uniqid()))->isFalse
  162. ->function('is_string')->wasCalledWithArguments($mixed)->once
  163. ->function('preg_match')->wasCalledWithArguments('/^.*$/us', $mixed)->once
  164. ->if($this->function->preg_match = 1)
  165. ->then
  166. ->boolean($this->testedInstance->isUtf8($mixed = uniqid()))->isTrue
  167. ->function('is_string')->wasCalledWithArguments($mixed)->once
  168. ->function('preg_match')->wasCalledWithArguments('/^.*$/us', $mixed)->once
  169. ;
  170. }
  171. private function getRandomUtf8String()
  172. {
  173. $characters = 'àâäéèêëîïôöùüŷÿ';
  174. $characters = mb_convert_encoding($characters, 'UTF-8', mb_detect_encoding($characters));
  175. $charactersLength = mb_strlen($characters, 'UTF-8');
  176. $utf8String = '';
  177. for($i = 0; $i < 16; $i++)
  178. {
  179. $utf8String .= mb_substr($characters, rand(0, $charactersLength - 1), 1, 'UTF-8');
  180. }
  181. return $utf8String;
  182. }
  183. }