菜谱项目

PhoneNumberTest.php 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace Faker\Test\Provider\en_ZA;
  3. use Faker\Generator;
  4. use Faker\Provider\en_ZA\PhoneNumber;
  5. class PhoneNumberTest extends \PHPUnit_Framework_TestCase
  6. {
  7. private $faker;
  8. public function setUp()
  9. {
  10. $faker = new Generator();
  11. $faker->addProvider(new PhoneNumber($faker));
  12. $this->faker = $faker;
  13. }
  14. public function testPhoneNumber()
  15. {
  16. for ($i = 0; $i < 10; $i++) {
  17. $number = $this->faker->phoneNumber;
  18. $digits = array_values(array_filter(str_split($number), 'ctype_digit'));
  19. // 10 digits
  20. if($digits[0] = 2 && $digits[1] == 7) {
  21. $this->assertLessThanOrEqual(11, count($digits));
  22. } else {
  23. $this->assertGreaterThanOrEqual(10, count($digits));
  24. }
  25. }
  26. }
  27. public function testTollFreePhoneNumber()
  28. {
  29. for ($i = 0; $i < 10; $i++) {
  30. $number = $this->faker->tollFreeNumber;
  31. $digits = array_values(array_filter(str_split($number), 'ctype_digit'));
  32. if (count($digits) === 11) {
  33. $this->assertEquals('0', $digits[0]);
  34. }
  35. $areaCode = $digits[0] . $digits[1] . $digits[2] . $digits[3];
  36. $this->assertContains($areaCode, array('0800', '0860', '0861', '0862'));
  37. }
  38. }
  39. public function testCellPhoneNumber()
  40. {
  41. for ($i = 0; $i < 10; $i++) {
  42. $number = $this->faker->mobileNumber;
  43. $digits = array_values(array_filter(str_split($number), 'ctype_digit'));
  44. if($digits[0] = 2 && $digits[1] == 7) {
  45. $this->assertLessThanOrEqual(11, count($digits));
  46. } else {
  47. $this->assertGreaterThanOrEqual(10, count($digits));
  48. }
  49. $this->assertRegExp('/^(\+27|27)?(\()?0?([6][0-4]|[7][1-9]|[8][1-9])(\))?( |-|\.|_)?(\d{3})( |-|\.|_)?(\d{4})/', $number);
  50. }
  51. }
  52. }