Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Add zh_CN PhoneNumber TestCase & TollFreePhoneNumber API & E164PhoneN… #2054

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/Faker/Provider/zh_CN/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,36 @@ class PhoneNumber extends \Faker\Provider\PhoneNumber
170, 171, // virtual operators
);

protected static $tollFreePhoneOperators = array(
'400-6', '400-0', //China Unicom
'400-1', //China Mobile
'400-7', //China CTT=>China Mobile
'400-8', '400-9', // China Telecom

);

protected static $formats = array('###########');

protected static $tollFreeFormats = array('400-###-####');

public function phoneNumber()
{
$operator = static::randomElement(static::$operators);
$format = static::randomElement(static::$formats);

return $operator . static::numerify(substr($format, 0, strlen($format) - strlen($operator)));
return $operator . static::numerify(substr($format, strlen($operator)));
}

public function tollFreePhoneNumber()
{
$operator = static::randomElement(static::$tollFreePhoneOperators);
$format = static::randomElement(static::$tollFreeFormats);

return $operator . static::numerify(substr($format, strlen($operator)));
}

public function e164PhoneNumber()
{
return '+86' . $this->phoneNumber();
}
}
77 changes: 77 additions & 0 deletions test/Faker/Provider/zh_CN/PhoneNumberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Faker\Test\Provider\zh_CN;

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

final class PhoneNumberTest extends TestCase
{

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

protected static $purePhoneNumberInCNRegExp = '/^(\+?0?86\-?)?1[345789]\d{9}$/';

protected 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;
$digits = array_values(array_filter(str_split($number), 'ctype_digit'));
// Prefix 1
if (count($digits) === 11) {
$this->assertEquals('1', $digits[0]);
}
// 11 digits
$this->assertCount(11, $digits);
// Test format
$this->assertRegExp('/^1[345789]\d{9}$/', $number);
}
}

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

// Prefix '4'
$this->assertEquals('4', $digits[0]);
$this->assertContains($digits[3], array(0, 1, 6, 7, 8, 9));

// 10 digits
$this->assertCount(10, $digits);

// Test format
$this->assertRegExp('/^400-[016789]\d{2}-\d{4}$/', $number);
}
}

public function testE164PhoneNumber()
{
for ($i = 0; $i < 100; $i++) {
$e164PhoneNumber = $this->faker->e164PhoneNumber;
$this->assertEquals('+86', substr($e164PhoneNumber, 0, 3));
$pureNumber = substr($e164PhoneNumber, 3);
$digits = array_values(array_filter(str_split($pureNumber), 'ctype_digit'));
// Prefix 1
if (count($digits) === 11) {
$this->assertEquals('1', $digits[0]);
}
// 11 digits
$this->assertCount(11, $digits);
// Test format
$this->assertRegExp('/^(\+?0?86\-?)1[345789]\d{9}$/', $e164PhoneNumber);
}
}
}