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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* @author Mark van der Velden <mark@dynom.nl>
*/
namespace Faker\Test\Provider;
use Faker;
/**
* Class ProviderOverrideTest
*
* @package Faker\Test\Provider
*
* This class tests a large portion of all locale specific providers. It does not test the entire stack, because each
* locale specific provider (can) has specific implementations. The goal of this test is to test the common denominator
* and to try to catch possible invalid multi-byte sequences.
*/
class ProviderOverrideTest extends \PHPUnit_Framework_TestCase
{
/**
* Constants with regular expression patterns for testing the output.
*
* Regular expressions are sensitive for malformed strings (e.g.: strings with incorrect encodings) so by using
* PCRE for the tests, even though they seem fairly pointless, we test for incorrect encodings also.
*/
const TEST_STRING_REGEX = '/.+/u';
/**
* Slightly more specific for e-mail, the point isn't to properly validate e-mails.
*/
const TEST_EMAIL_REGEX = '/^(.+)@(.+)$/ui';
/**
* @dataProvider localeDataProvider
* @param string $locale
*/
public function testAddress($locale = null)
{
$faker = Faker\Factory::create($locale);
$this->assertRegExp(static::TEST_STRING_REGEX, $faker->city);
$this->assertRegExp(static::TEST_STRING_REGEX, $faker->postcode);
$this->assertRegExp(static::TEST_STRING_REGEX, $faker->address);
$this->assertRegExp(static::TEST_STRING_REGEX, $faker->country);
}
/**
* @dataProvider localeDataProvider
* @param string $locale
*/
public function testCompany($locale = null)
{
$faker = Faker\Factory::create($locale);
$this->assertRegExp(static::TEST_STRING_REGEX, $faker->company);
}
/**
* @dataProvider localeDataProvider
* @param string $locale
*/
public function testDateTime($locale = null)
{
$faker = Faker\Factory::create($locale);
$this->assertRegExp(static::TEST_STRING_REGEX, $faker->century);
$this->assertRegExp(static::TEST_STRING_REGEX, $faker->timezone);
}
/**
* @dataProvider localeDataProvider
* @param string $locale
*/
public function testInternet($locale = null)
{
$faker = Faker\Factory::create($locale);
$this->assertRegExp(static::TEST_STRING_REGEX, $faker->userName);
$this->assertRegExp(static::TEST_EMAIL_REGEX, $faker->email);
$this->assertRegExp(static::TEST_EMAIL_REGEX, $faker->safeEmail);
$this->assertRegExp(static::TEST_EMAIL_REGEX, $faker->freeEmail);
$this->assertRegExp(static::TEST_EMAIL_REGEX, $faker->companyEmail);
}
/**
* @dataProvider localeDataProvider
* @param string $locale
*/
public function testPerson($locale = null)
{
$faker = Faker\Factory::create($locale);
$this->assertRegExp(static::TEST_STRING_REGEX, $faker->name);
$this->assertRegExp(static::TEST_STRING_REGEX, $faker->title);
$this->assertRegExp(static::TEST_STRING_REGEX, $faker->firstName);
$this->assertRegExp(static::TEST_STRING_REGEX, $faker->lastName);
}
/**
* @dataProvider localeDataProvider
* @param string $locale
*/
public function testPhoneNumber($locale = null)
{
$faker = Faker\Factory::create($locale);
$this->assertRegExp(static::TEST_STRING_REGEX, $faker->phoneNumber);
}
/**
* @dataProvider localeDataProvider
* @param string $locale
*/
public function testUserAgent($locale = null)
{
$faker = Faker\Factory::create($locale);
$this->assertRegExp(static::TEST_STRING_REGEX, $faker->userAgent);
}
/**
* @dataProvider localeDataProvider
*
* @param null $locale
* @param string $locale
*/
public function testUuid($locale = null)
{
$faker = Faker\Factory::create($locale);
$this->assertRegExp(static::TEST_STRING_REGEX, $faker->uuid);
}
/**
* @return array
*/
public function localeDataProvider()
{
$locales = $this->getAllLocales();
$data = array();
foreach ($locales as $locale) {
$data[] = array(
$locale
);
}
return $data;
}
/**
* Returns all locales as array values
*
* @return array
*/
private function getAllLocales()
{
static $locales = array();
if ( ! empty($locales)) {
return $locales;
}
// Finding all PHP files in the xx_XX directories
$providerDir = __DIR__ .'/../../../src/Faker/Provider';
foreach (glob($providerDir .'/*_*/*.php') as $file) {
$localisation = basename(dirname($file));
if (isset($locales[ $localisation ])) {
continue;
}
$locales[ $localisation ] = $localisation;
}
return $locales;
}
}