/
demo.php
147 lines (123 loc) · 2.69 KB
/
demo.php
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
<?php
use IainConnor\MockingJay\MockingJay;
include(dirname(__FILE__) . "/vendor/autoload.php");
/**
* Class Foo
*/
class Foo {
/**
* You can use basic types and we'll guess at the format.
*
* @var string
*/
public $lorem;
/**
* You can use arrays.
*
* @var array<int>
*/
public $ipsum;
/**
* And you can specify the count of items to generate.
*
* @var array<string>
* @\IainConnor\MockingJay\Annotations\Count(count=3)
*/
public $dolor;
/**
* Or arrays in yet another format.
* And you can specify the range of items to generate.
*
* @var float[]
* @\IainConnor\MockingJay\Annotations\Count(min=0, max=3)
*/
public $sit;
/**
* You can provide a custom callback to generate the value.
*
* @\IainConnor\MockingJay\Annotations\Mock(callback="generateAmit")
* @var string
*/
public $amit;
/**
* You can use any provider from https://github.com/fzaninotto/Faker#formatters.
*
* @\IainConnor\MockingJay\Annotations\Mock(fakerProvider="name")
* @var string
*/
public $consectetur;
/**
* You can use another Object.
*
* @var Bar
*/
public $adipiscing;
/**
* And, of course, combine and mix these features.
*
* @var Bar[]
* @\IainConnor\MockingJay\Annotations\Count(count=4)
*/
public $lacinia;
/**
* You can ignore certain properties.
*
* @\IainConnor\MockingJay\Annotations\IgnoreMock()
* @var int
*/
public $elit;
/**
* Properties with default values will be left alone.
*
* @var string
*/
public $donec = "Donec";
public function generateAmit() {
return "AMIT!";
}
}
/**
* Class Bar
* If a class is annotated with `Whitelist`, only the properties specifically annotated with `Mock` will be included.
*
* @\IainConnor\MockingJay\Annotations\Whitelist()
*/
class Bar {
/**
* @var boolean
*/
public $lorem;
/**
* @\IainConnor\MockingJay\Annotations\Mock()
* @var string
*/
public $ipsum;
/**
* @\IainConnor\MockingJay\Annotations\Mock()
* @var Baz
*/
public $sit;
}
/**
* Class Baz
*/
class Baz {
/**
* @var boolean
*/
public $lorem;
}
$mockingJay = MockingJay::instance();
// You should always set an AnnotationReader to improve performance.
// @see http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/annotations.html
$mockingJay->setAnnotationReader(
new \IainConnor\Cornucopia\CachedReader(
new \IainConnor\Cornucopia\AnnotationReader(),
new \Doctrine\Common\Cache\ArrayCache()
));
// Mock an instance of `Foo` and dump it out.
var_dump($mockingJay->mock(Foo::class));
// Create a copy of `Foo` and mock the unset properties and dump it out.
$foo = new Foo();
$foo->lorem = "Lorem";
var_dump($mockingJay->mockInstance($foo));