1 | <?php |
||
34 | class FixtureFactory |
||
35 | { |
||
36 | |||
37 | /** |
||
38 | * @var array Array of fixture items, keyed by class and unique identifier, |
||
39 | * with values being the generated database ID. Does not store object instances. |
||
40 | */ |
||
41 | protected $fixtures = array(); |
||
42 | |||
43 | /** |
||
44 | * @var array Callbacks |
||
45 | */ |
||
46 | protected $blueprints = array(); |
||
47 | |||
48 | /** |
||
49 | * @param string $name Unique name for this blueprint |
||
50 | * @param array|FixtureBlueprint $defaults Array of default values, or a blueprint instance |
||
51 | * @return $this |
||
52 | */ |
||
53 | public function define($name, $defaults = array()) |
||
69 | |||
70 | /** |
||
71 | * Writes the fixture into the database using DataObjects |
||
72 | * |
||
73 | * @param string $name Name of the {@link FixtureBlueprint} to use, |
||
74 | * usually a DataObject subclass. |
||
75 | * @param string $identifier Unique identifier for this fixture type |
||
76 | * @param array $data Map of properties. Overrides default data. |
||
77 | * @return DataObject |
||
78 | */ |
||
79 | public function createObject($name, $identifier, $data = null) |
||
95 | |||
96 | /** |
||
97 | * Writes the fixture into the database directly using a database manipulation. |
||
98 | * Does not use blueprints. Only supports tables with a primary key. |
||
99 | * |
||
100 | * @param string $table Existing database table name |
||
101 | * @param string $identifier Unique identifier for this fixture type |
||
102 | * @param array $data Map of properties |
||
103 | * @return int Database identifier |
||
104 | */ |
||
105 | public function createRaw($table, $identifier, $data) |
||
118 | |||
119 | /** |
||
120 | * Get the ID of an object from the fixture. |
||
121 | * |
||
122 | * @param string $class The data class, as specified in your fixture file. Parent classes won't work |
||
123 | * @param string $identifier The identifier string, as provided in your fixture file |
||
124 | * @return int |
||
125 | */ |
||
126 | public function getId($class, $identifier) |
||
134 | |||
135 | /** |
||
136 | * Return all of the IDs in the fixture of a particular class name. |
||
137 | * |
||
138 | * @param string $class The data class or table name |
||
139 | * @return array|false A map of fixture-identifier => object-id |
||
140 | */ |
||
141 | public function getIds($class) |
||
149 | |||
150 | /** |
||
151 | * @param string $class |
||
152 | * @param string $identifier |
||
153 | * @param int $databaseId |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function setId($class, $identifier, $databaseId) |
||
161 | |||
162 | /** |
||
163 | * Get an object from the fixture. |
||
164 | * |
||
165 | * @param string $class The data class or table name, as specified in your fixture file. Parent classes won't work |
||
166 | * @param string $identifier The identifier string, as provided in your fixture file |
||
167 | * @return DataObject |
||
168 | */ |
||
169 | public function get($class, $identifier) |
||
188 | |||
189 | /** |
||
190 | * @return array Map of class names, containing a map of in-memory identifiers |
||
191 | * mapped to database identifiers. |
||
192 | */ |
||
193 | public function getFixtures() |
||
197 | |||
198 | /** |
||
199 | * Remove all fixtures previously defined through {@link createObject()} |
||
200 | * or {@link createRaw()}, both from the internal fixture mapping and the database. |
||
201 | * If the $class argument is set, limit clearing to items of this class. |
||
202 | * |
||
203 | * @param string $limitToClass |
||
204 | */ |
||
205 | public function clear($limitToClass = null) |
||
225 | |||
226 | /** |
||
227 | * @return array Of {@link FixtureBlueprint} instances |
||
228 | */ |
||
229 | public function getBlueprints() |
||
233 | |||
234 | /** |
||
235 | * @param String $name |
||
236 | * @return FixtureBlueprint|false |
||
237 | */ |
||
238 | public function getBlueprint($name) |
||
242 | |||
243 | /** |
||
244 | * Parse a value from a fixture file. If it starts with => |
||
245 | * it will get an ID from the fixture dictionary |
||
246 | * |
||
247 | * @param string $value |
||
248 | * @return string Fixture database ID, or the original value |
||
249 | */ |
||
250 | protected function parseValue($value) |
||
273 | } |
||
274 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: