Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

abalsh / Garlix

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Members
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Switch branch/tag
  • Garlix
  • ..
  • en_US
  • PhoneNumberTest.php
Find file
BlameHistoryPermalink
  • Florian Shllaku's avatar
    Laravel has been added and the porting has been configured · 20bcaf2c
    Florian Shllaku committed 6 years ago
    20bcaf2c
PhoneNumberTest.php 2.59 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
<?php

namespace Faker\Test\Provider\en_US;

use Faker\Generator;
use Faker\Provider\en_US\PhoneNumber;
use PHPUnit\Framework\TestCase;

class PhoneNumberTest extends TestCase
{

    /**
     * @var Generator
     */
    private $faker;

    public function setUp()
    {
        $faker = new Generator();
        $faker->addProvider(new PhoneNumber($faker));
        $this->faker = $faker;
    }

    public function testPhoneNumber()
    {
        for ($i = 0; $i < 100; $i++) {
            $number = $this->faker->phoneNumber;
            $baseNumber = preg_replace('/ *x.*$/', '', $number); // Remove possible extension
            $digits = array_values(array_filter(str_split($baseNumber), 'ctype_digit'));

            // Prefix '1' allowed
            if (count($digits) === 11) {
                $this->assertEquals('1', $digits[0]);
                $digits = array_slice($digits, 1);
            }

            // 10 digits
            $this->assertEquals(10, count($digits));

            // Last two digits of area code cannot be identical
            $this->assertNotEquals($digits[1], $digits[2]);

            // Last two digits of exchange code cannot be 1
            if ($digits[4] === 1) {
                $this->assertNotEquals($digits[4], $digits[5]);
            }

            // Test format
            $this->assertRegExp('/^(\+?1)?([ -.]*\d{3}[ -.]*| *\(\d{3}\) *)\d{3}[-.]?\d{4}$/', $baseNumber);
        }
    }

    public function testTollFreeAreaCode()
    {
        $this->assertContains($this->faker->tollFreeAreaCode, array(800, 822, 833, 844, 855, 866, 877, 888, 880, 887, 889));
    }

    public function testTollFreePhoneNumber()
    {
        for ($i = 0; $i < 100; $i++) {
            $number = $this->faker->tollFreePhoneNumber;
            $digits = array_values(array_filter(str_split($number), 'ctype_digit'));

            // Prefix '1' allowed
            if (count($digits) === 11) {
                $this->assertEquals('1', $digits[0]);
                $digits = array_slice($digits, 1);
            }

            // 10 digits
            $this->assertEquals(10, count($digits));

            $areaCode = $digits[0] . $digits[1] . $digits[2];
            $this->assertContains($areaCode, array('800', '822', '833', '844', '855', '866', '877', '888', '880', '887', '889'));

            // Last two digits of exchange code cannot be 1
            if ($digits[4] === 1) {
                $this->assertNotEquals($digits[4], $digits[5]);
            }

            // Test format
            $this->assertRegExp('/^(\+?1)?([ -.]*\d{3}[ -.]*| *\(\d{3}\) *)\d{3}[-.]?\d{4}$/', $number);
        }
    }
}