菜谱项目

PhoneNumberTest.php 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Faker\Test\Provider\en_US;
  3. use Faker\Generator;
  4. use Faker\Provider\en_US\PhoneNumber;
  5. class PhoneNumberTest extends \PHPUnit_Framework_TestCase
  6. {
  7. /**
  8. * @var Generator
  9. */
  10. private $faker;
  11. public function setUp()
  12. {
  13. $faker = new Generator();
  14. $faker->addProvider(new PhoneNumber($faker));
  15. $this->faker = $faker;
  16. }
  17. public function testPhoneNumber()
  18. {
  19. for ($i = 0; $i < 100; $i++) {
  20. $number = $this->faker->phoneNumber;
  21. $baseNumber = preg_replace('/ *x.*$/', '', $number); // Remove possible extension
  22. $digits = array_values(array_filter(str_split($baseNumber), 'ctype_digit'));
  23. // Prefix '1' allowed
  24. if (count($digits) === 11) {
  25. $this->assertEquals('1', $digits[0]);
  26. $digits = array_slice($digits, 1);
  27. }
  28. // 10 digits
  29. $this->assertEquals(10, count($digits));
  30. // Last two digits of area code cannot be identical
  31. $this->assertNotEquals($digits[1], $digits[2]);
  32. // Last two digits of exchange code cannot be 1
  33. if ($digits[4] === 1) {
  34. $this->assertNotEquals($digits[4], $digits[5]);
  35. }
  36. // Test format
  37. $this->assertRegExp('/^(\+?1)?([ -.]*\d{3}[ -.]*| *\(\d{3}\) *)\d{3}[-.]?\d{4}$/', $baseNumber);
  38. }
  39. }
  40. public function testTollFreeAreaCode()
  41. {
  42. $this->assertContains($this->faker->tollFreeAreaCode, array(800, 822, 833, 844, 855, 866, 877, 888, 880, 887, 889));
  43. }
  44. public function testTollFreePhoneNumber()
  45. {
  46. for ($i = 0; $i < 100; $i++) {
  47. $number = $this->faker->tollFreePhoneNumber;
  48. $digits = array_values(array_filter(str_split($number), 'ctype_digit'));
  49. // Prefix '1' allowed
  50. if (count($digits) === 11) {
  51. $this->assertEquals('1', $digits[0]);
  52. $digits = array_slice($digits, 1);
  53. }
  54. // 10 digits
  55. $this->assertEquals(10, count($digits));
  56. $areaCode = $digits[0] . $digits[1] . $digits[2];
  57. $this->assertContains($areaCode, array('800', '822', '833', '844', '855', '866', '877', '888', '880', '887', '889'));
  58. // Last two digits of exchange code cannot be 1
  59. if ($digits[4] === 1) {
  60. $this->assertNotEquals($digits[4], $digits[5]);
  61. }
  62. // Test format
  63. $this->assertRegExp('/^(\+?1)?([ -.]*\d{3}[ -.]*| *\(\d{3}\) *)\d{3}[-.]?\d{4}$/', $number);
  64. }
  65. }
  66. }