GeneratorTest.php
4.23 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
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
<?php
namespace Faker\Test;
use Faker\Generator;
class GeneratorTest extends \PHPUnit_Framework_TestCase
{
public function testAddProviderGivesPriorityToNewlyAddedProvider()
{
$generator = new Generator;
$generator->addProvider(new FooProvider());
$generator->addProvider(new BarProvider());
$this->assertEquals('barfoo', $generator->format('fooFormatter'));
}
public function testGetFormatterReturnsCallable()
{
$generator = new Generator;
$provider = new FooProvider();
$generator->addProvider($provider);
$this->assertTrue(is_callable($generator->getFormatter('fooFormatter')));
}
public function testGetFormatterReturnsCorrectFormatter()
{
$generator = new Generator;
$provider = new FooProvider();
$generator->addProvider($provider);
$expected = array($provider, 'fooFormatter');
$this->assertEquals($expected, $generator->getFormatter('fooFormatter'));
}
/**
* @expectedException InvalidArgumentException
*/
public function testGetFormatterThrowsExceptionOnIncorrectProvider()
{
$generator = new Generator;
$generator->getFormatter('fooFormatter');
}
/**
* @expectedException InvalidArgumentException
*/
public function testGetFormatterThrowsExceptionOnIncorrectFormatter()
{
$generator = new Generator;
$provider = new FooProvider();
$generator->addProvider($provider);
$generator->getFormatter('barFormatter');
}
public function testFormatCallsFormatterOnProvider()
{
$generator = new Generator;
$provider = new FooProvider();
$generator->addProvider($provider);
$this->assertEquals('foobar', $generator->format('fooFormatter'));
}
public function testFormatTransfersArgumentsToFormatter()
{
$generator = new Generator;
$provider = new FooProvider();
$generator->addProvider($provider);
$this->assertEquals('bazfoo', $generator->format('fooFormatterWithArguments', array('foo')));
}
public function testParseReturnsSameStringWhenItContainsNoCurlyBraces()
{
$generator = new Generator();
$this->assertEquals('fooBar#?', $generator->parse('fooBar#?'));
}
public function testParseReturnsStringWithTokensReplacedByFormatters()
{
$generator = new Generator();
$provider = new FooProvider();
$generator->addProvider($provider);
$this->assertEquals('This is foobar a text with foobar', $generator->parse('This is {{fooFormatter}} a text with {{ fooFormatter }}'));
}
public function testMagicGetCallsFormat()
{
$generator = new Generator;
$provider = new FooProvider();
$generator->addProvider($provider);
$this->assertEquals('foobar', $generator->fooFormatter);
}
public function testMagicCallCallsFormat()
{
$generator = new Generator;
$provider = new FooProvider();
$generator->addProvider($provider);
$this->assertEquals('foobar', $generator->fooFormatter());
}
public function testMagicCallCallsFormatWithArguments()
{
$generator = new Generator;
$provider = new FooProvider();
$generator->addProvider($provider);
$this->assertEquals('bazfoo', $generator->fooFormatterWithArguments('foo'));
}
public function testSeed()
{
$generator = new Generator;
$generator->seed(0);
$mtRandWithSeedZero = mt_rand();
$generator->seed(0);
$this->assertEquals($mtRandWithSeedZero, mt_rand(), 'seed(0) should be deterministic.');
$generator->seed();
$mtRandWithoutSeed = mt_rand();
$this->assertNotEquals($mtRandWithSeedZero, $mtRandWithoutSeed, 'seed() should be different than seed(0)');
$generator->seed();
$this->assertNotEquals($mtRandWithoutSeed, mt_rand(), 'seed() should not be deterministic.');
}
}
class FooProvider
{
public function fooFormatter()
{
return 'foobar';
}
public function fooFormatterWithArguments($value = '')
{
return 'baz' . $value;
}
}
class BarProvider
{
public function fooFormatter()
{
return 'barfoo';
}
}