1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Ablett\TwigFaker\TwigFakerExtension; |
4
|
|
|
use Ablett\TwigFaker\InvalidFactoryException; |
5
|
|
|
|
6
|
|
|
class TwigFakerExtensionTest extends PHPUnit_Framework_TestCase |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
protected $twigFaker; |
9
|
|
|
|
10
|
|
|
public function setUp() |
11
|
|
|
{ |
12
|
|
|
TwigFakerExtension::$factoriesPath = 'tests/factories/'; |
13
|
|
|
|
14
|
|
|
$this->twigFaker = new TwigFakerExtension; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @test |
19
|
|
|
*/ |
20
|
|
|
public function it_registers_the_extension_name_correctly() |
21
|
|
|
{ |
22
|
|
|
$name = $this->twigFaker->getName(); |
23
|
|
|
|
24
|
|
|
$this->assertEquals('faker', $name); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @test |
29
|
|
|
*/ |
30
|
|
|
public function it_registers_a_fake_function() |
31
|
|
|
{ |
32
|
|
|
$registeredFunctions = $this->twigFaker->getFunctions(); |
33
|
|
|
|
34
|
|
|
$this->assertArrayHasKey('fake', $registeredFunctions); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @test |
39
|
|
|
* |
40
|
|
|
* @expectedException Ablett\TwigFaker\InvalidFactoryException |
41
|
|
|
*/ |
42
|
|
|
public function it_throws_an_exception_when_an_invalid_factory_is_used() |
43
|
|
|
{ |
44
|
|
|
$data = $this->twigFaker->fakerData('does-not-exist'); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @test |
49
|
|
|
*/ |
50
|
|
|
public function it_returns_one_fake_item_when_no_count_is_passed() |
51
|
|
|
{ |
52
|
|
|
$fakePerson = $this->twigFaker->fakerData('person'); |
53
|
|
|
|
54
|
|
|
$this->assertCount(1, $fakePerson); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @test |
59
|
|
|
*/ |
60
|
|
|
public function it_returns_fifteen_fake_items_when_the_count_is_passed() |
61
|
|
|
{ |
62
|
|
|
$fakePeople = $this->twigFaker->fakerData('person', 15); |
63
|
|
|
|
64
|
|
|
$this->assertCount(15, $fakePeople); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @test |
69
|
|
|
*/ |
70
|
|
|
public function it_caches_a_single_fake_item_when_given_a_cache_key() |
71
|
|
|
{ |
72
|
|
|
$fakePerson = $this->twigFaker->fakerData('person', 1, 'my-cache-key'); |
73
|
|
|
$cachedFakePerson = $this->twigFaker->fakerData('person', 1, 'my-cache-key'); |
74
|
|
|
|
75
|
|
|
$this->assertSame($fakePerson, $cachedFakePerson); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @test |
80
|
|
|
*/ |
81
|
|
|
public function it_caches_multiple_fake_item_when_given_a_cache_key() |
82
|
|
|
{ |
83
|
|
|
$fakePeople = $this->twigFaker->fakerData('person', 35, 'my-cache-key'); |
84
|
|
|
$cachedFakePeople = $this->twigFaker->fakerData('person', 35, 'my-cache-key'); |
85
|
|
|
|
86
|
|
|
$this->assertSame($fakePeople, $cachedFakePeople); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @test |
91
|
|
|
*/ |
92
|
|
|
public function it_caches_single_fake_items_with_different_cache_keys() |
93
|
|
|
{ |
94
|
|
|
$fakePerson = $this->twigFaker->fakerData('person', 1, 'cache-key-one'); |
95
|
|
|
$cachedfakePerson = $this->twigFaker->fakerData('person', 1, 'cache-key-one'); |
96
|
|
|
|
97
|
|
|
$anotherFakePerson = $this->twigFaker->fakerData('person', 1, 'cache-key-two'); |
98
|
|
|
$cachedAnotherFakePerson = $this->twigFaker->fakerData('person', 1, 'cache-key-two'); |
99
|
|
|
|
100
|
|
|
$this->assertSame($fakePerson, $cachedfakePerson); |
101
|
|
|
$this->assertSame($anotherFakePerson, $cachedAnotherFakePerson); |
102
|
|
|
$this->assertNotSame($fakePerson, $anotherFakePerson); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @test |
107
|
|
|
*/ |
108
|
|
|
public function it_allows_the_same_cache_key_to_be_used_for_different_factories() |
109
|
|
|
{ |
110
|
|
|
$fakePerson = $this->twigFaker->fakerData('person', 1, 'cache-key-one'); |
111
|
|
|
$fakeAnimal = $this->twigFaker->fakerData('animal', 1, 'cache-key-one'); |
112
|
|
|
|
113
|
|
|
$this->assertNotSame($fakePerson, $fakeAnimal); |
114
|
|
|
} |
115
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.