You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
9.3KB

  1. <?php
  2. namespace Faker\Test\Provider;
  3. use Faker\Provider\Base as BaseProvider;
  4. class BaseTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function testRandomDigitReturnsInteger()
  7. {
  8. $this->assertTrue(is_integer(BaseProvider::randomDigit()));
  9. }
  10. public function testRandomDigitReturnsDigit()
  11. {
  12. $this->assertTrue(BaseProvider::randomDigit() >= 0);
  13. $this->assertTrue(BaseProvider::randomDigit() < 10);
  14. }
  15. public function testRandomDigitNotNullReturnsNotNullDigit()
  16. {
  17. $this->assertTrue(BaseProvider::randomDigitNotNull() > 0);
  18. $this->assertTrue(BaseProvider::randomDigitNotNull() < 10);
  19. }
  20. /**
  21. * @expectedException \InvalidArgumentException
  22. */
  23. public function testRandomNumberThrowsExceptionWhenCalledWithAMax()
  24. {
  25. BaseProvider::randomNumber(5, 200);
  26. }
  27. /**
  28. * @expectedException \InvalidArgumentException
  29. */
  30. public function testRandomNumberThrowsExceptionWhenCalledWithATooHighNumberOfDigits()
  31. {
  32. BaseProvider::randomNumber(10);
  33. }
  34. public function testRandomNumberReturnsInteger()
  35. {
  36. $this->assertTrue(is_integer(BaseProvider::randomNumber()));
  37. $this->assertTrue(is_integer(BaseProvider::randomNumber(5, false)));
  38. }
  39. public function testRandomNumberReturnsDigit()
  40. {
  41. $this->assertTrue(BaseProvider::randomNumber(3) >= 0);
  42. $this->assertTrue(BaseProvider::randomNumber(3) < 1000);
  43. }
  44. public function testRandomNumberAcceptsStrictParamToEnforceNumberSize()
  45. {
  46. $this->assertEquals(5, strlen((string) BaseProvider::randomNumber(5, true)));
  47. }
  48. public function testNumberBetween()
  49. {
  50. $min = 5;
  51. $max = 6;
  52. $this->assertGreaterThanOrEqual($min, BaseProvider::numberBetween($min, $max));
  53. $this->assertGreaterThanOrEqual(BaseProvider::numberBetween($min, $max), $max);
  54. }
  55. public function testNumberBetweenAcceptsZeroAsMax()
  56. {
  57. $this->assertEquals(0, BaseProvider::numberBetween(0, 0));
  58. }
  59. public function testRandomFloat()
  60. {
  61. $min = 4;
  62. $max = 10;
  63. $nbMaxDecimals = 8;
  64. $result = BaseProvider::randomFloat($nbMaxDecimals, $min, $max);
  65. $parts = explode('.', $result);
  66. $this->assertInternalType('float', $result);
  67. $this->assertGreaterThanOrEqual($min, $result);
  68. $this->assertLessThanOrEqual($max, $result);
  69. $this->assertLessThanOrEqual($nbMaxDecimals, strlen($parts[1]));
  70. }
  71. public function testRandomLetterReturnsString()
  72. {
  73. $this->assertTrue(is_string(BaseProvider::randomLetter()));
  74. }
  75. public function testRandomLetterReturnsSingleLetter()
  76. {
  77. $this->assertEquals(1, strlen(BaseProvider::randomLetter()));
  78. }
  79. public function testRandomLetterReturnsLowercaseLetter()
  80. {
  81. $lowercaseLetters = 'abcdefghijklmnopqrstuvwxyz';
  82. $this->assertTrue(strpos($lowercaseLetters, BaseProvider::randomLetter()) !== false);
  83. }
  84. public function testRandomElementReturnsNullWhenArrayEmpty()
  85. {
  86. $this->assertNull(BaseProvider::randomElement(array()));
  87. }
  88. public function testRandomElementReturnsElementFromArray()
  89. {
  90. $elements = array('23', 'e', 32, '#');
  91. $this->assertContains(BaseProvider::randomElement($elements), $elements);
  92. }
  93. public function testRandomElementReturnsElementFromAssociativeArray()
  94. {
  95. $elements = array('tata' => '23', 'toto' => 'e', 'tutu' => 32, 'titi' => '#');
  96. $this->assertContains(BaseProvider::randomElement($elements), $elements);
  97. }
  98. public function testNumerifyReturnsSameStringWhenItContainsNoHashSign()
  99. {
  100. $this->assertEquals('fooBar?', BaseProvider::numerify('fooBar?'));
  101. }
  102. public function testNumerifyReturnsStringWithHashSignsReplacedByDigits()
  103. {
  104. $this->assertRegExp('/foo\dBa\dr/', BaseProvider::numerify('foo#Ba#r'));
  105. }
  106. public function testNumerifyReturnsStringWithPercentageSignsReplacedByDigits()
  107. {
  108. $this->assertRegExp('/foo\dBa\dr/', BaseProvider::numerify('foo%Ba%r'));
  109. }
  110. public function testNumerifyReturnsStringWithPercentageSignsReplacedByNotNullDigits()
  111. {
  112. $this->assertNotEquals('0', BaseProvider::numerify('%'));
  113. }
  114. public function testNumerifyCanGenerateALargeNumberOfDigits()
  115. {
  116. $largePattern = str_repeat('#', 20); // definitely larger than PHP_INT_MAX on all systems
  117. $this->assertEquals(20, strlen(BaseProvider::numerify($largePattern)));
  118. }
  119. public function testLexifyReturnsSameStringWhenItContainsNoQuestionMark()
  120. {
  121. $this->assertEquals('fooBar#', BaseProvider::lexify('fooBar#'));
  122. }
  123. public function testLexifyReturnsStringWithQuestionMarksReplacedByLetters()
  124. {
  125. $this->assertRegExp('/foo[a-z]Ba[a-z]r/', BaseProvider::lexify('foo?Ba?r'));
  126. }
  127. public function testBothifyCombinesNumerifyAndLexify()
  128. {
  129. $this->assertRegExp('/foo[a-z]Ba\dr/', BaseProvider::bothify('foo?Ba#r'));
  130. }
  131. public function testOptionalReturnsProviderValueWhenCalledWithWeight1()
  132. {
  133. $faker = new \Faker\Generator();
  134. $faker->addProvider(new \Faker\Provider\Base($faker));
  135. $this->assertNotNull($faker->optional(1)->randomDigit);
  136. }
  137. public function testOptionalReturnsNullWhenCalledWithWeight0()
  138. {
  139. $faker = new \Faker\Generator();
  140. $faker->addProvider(new \Faker\Provider\Base($faker));
  141. $this->assertNull($faker->optional(0)->randomDigit);
  142. }
  143. public function testOptionalAllowsChainingPropertyAccess()
  144. {
  145. $faker = new \Faker\Generator();
  146. $faker->addProvider(new \Faker\Provider\Base($faker));
  147. $faker->addProvider(new \ArrayObject(array(1))); // hack because method_exists forbids stubs
  148. $this->assertEquals(1, $faker->optional(1)->count);
  149. $this->assertNull($faker->optional(0)->count);
  150. }
  151. public function testOptionalAllowsChainingMethodCall()
  152. {
  153. $faker = new \Faker\Generator();
  154. $faker->addProvider(new \Faker\Provider\Base($faker));
  155. $faker->addProvider(new \ArrayObject(array(1))); // hack because method_exists forbids stubs
  156. $this->assertEquals(1, $faker->optional(1)->count());
  157. $this->assertNull($faker->optional(0)->count());
  158. }
  159. public function testOptionalAllowsChainingProviderCallRandomlyReturnNull()
  160. {
  161. $faker = new \Faker\Generator();
  162. $faker->addProvider(new \Faker\Provider\Base($faker));
  163. $values = array();
  164. for ($i=0; $i < 10; $i++) {
  165. $values[]= $faker->optional()->randomDigit;
  166. }
  167. $this->assertContains(null, $values);
  168. }
  169. public function testUniqueAllowsChainingPropertyAccess()
  170. {
  171. $faker = new \Faker\Generator();
  172. $faker->addProvider(new \Faker\Provider\Base($faker));
  173. $faker->addProvider(new \ArrayObject(array(1))); // hack because method_exists forbids stubs
  174. $this->assertEquals(1, $faker->unique()->count);
  175. }
  176. public function testUniqueAllowsChainingMethodCall()
  177. {
  178. $faker = new \Faker\Generator();
  179. $faker->addProvider(new \Faker\Provider\Base($faker));
  180. $faker->addProvider(new \ArrayObject(array(1))); // hack because method_exists forbids stubs
  181. $this->assertEquals(1, $faker->unique()->count());
  182. }
  183. public function testUniqueReturnsOnlyUniqueValues()
  184. {
  185. $faker = new \Faker\Generator();
  186. $faker->addProvider(new \Faker\Provider\Base($faker));
  187. $values = array();
  188. for ($i=0; $i < 10; $i++) {
  189. $values[]= $faker->unique()->randomDigit;
  190. }
  191. sort($values);
  192. $this->assertEquals(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), $values);
  193. }
  194. /**
  195. * @expectedException OverflowException
  196. */
  197. public function testUniqueThrowsExceptionWhenNoUniqueValueCanBeGenerated()
  198. {
  199. $faker = new \Faker\Generator();
  200. $faker->addProvider(new \Faker\Provider\Base($faker));
  201. for ($i=0; $i < 11; $i++) {
  202. $faker->unique()->randomDigit;
  203. }
  204. }
  205. public function testUniqueCanResetUniquesWhenPassedTrueAsArgument()
  206. {
  207. $faker = new \Faker\Generator();
  208. $faker->addProvider(new \Faker\Provider\Base($faker));
  209. $values = array();
  210. for ($i=0; $i < 10; $i++) {
  211. $values[]= $faker->unique()->randomDigit;
  212. }
  213. $values[]= $faker->unique(true)->randomDigit;
  214. for ($i=0; $i < 9; $i++) {
  215. $values[]= $faker->unique()->randomDigit;
  216. }
  217. sort($values);
  218. $this->assertEquals(array(0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9), $values);
  219. }
  220. /**
  221. * @expectedException LengthException
  222. * @expectedExceptionMessage Cannot get 2 elements, only 1 in array
  223. */
  224. public function testRandomElementsThrowsWhenRequestingTooManyKeys()
  225. {
  226. BaseProvider::randomElements(array('foo'), 2);
  227. }
  228. public function testRandomElements()
  229. {
  230. $this->assertCount(1, BaseProvider::randomElements(), 'Should work without any input');
  231. $empty = BaseProvider::randomElements(array(), 0);
  232. $this->assertInternalType('array', $empty);
  233. $this->assertCount(0, $empty);
  234. $shuffled = BaseProvider::randomElements(array('foo', 'bar', 'baz'), 3);
  235. $this->assertContains('foo', $shuffled);
  236. $this->assertContains('bar', $shuffled);
  237. $this->assertContains('baz', $shuffled);
  238. }
  239. }