Skip to content
This repository was archived by the owner on Nov 19, 2025. It is now read-only.

Commit 2dea5ae

Browse files
committed
Write documentation.
1 parent 744e0f9 commit 2dea5ae

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2013 CodeSleeve
3+
Copyright (c) 2013 Travis Bennett
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of
66
this software and associated documentation files (the "Software"), to deal in

README.md

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Install the package using Composer. Edit your project's `composer.json` file to
1919
```
2020

2121
## Quickstart
22+
### Step 1 - Fixture setup
2223
Inside your application test folder, create a folder named fixtures. Next, create a couple of fixture files inside this folder. Fixture files are written using native php array syntax. To create one, simply create a new file named after the table that the fixture corresponds to and have it return an array of data. As an example of this, let's create some fixture data for a hypothetical 'soul_reapers' table (bear with me, I'm a huge Bleach fan):
2324

2425
in tests/fixtures/soul_reapers.php
@@ -53,7 +54,7 @@ in tests/fixtures/zanpakutos.php
5354
'name' => 'Zabimaru',
5455
),
5556
'Ryujin Jakka' => array(
56-
'soul_reaper_id' => 'Yammamoto',
57+
'soul_reaper_id' => 'Genryusai',
5758
'name' => 'Ryujin Jakka',
5859
)
5960
);
@@ -90,7 +91,7 @@ in tests/fixtures/ranks_soul_reapers.php
9091
),
9192
'CaptainYammamoto' => array (
9293
'soul_reaper_id' => 'Yammamoto',
93-
'rank_id' => 'Captain'
94+
'rank_id' => 'Captain'
9495
),
9596
'LieutenantAbari' => array (
9697
'soul_reaper_id' => 'Renji',
@@ -103,3 +104,52 @@ in tests/fixtures/ranks_soul_reapers.php
103104
);
104105
```
105106

107+
Notice that we have both a 'CommanderYammamoto' and a 'CaptainYammamoto' entry inside our ranks_soul_reapers join table; That's because Genryusai Yammamoto was the Captain Commander (he had both the commander role and was also captain level as well) of the Gotei 13.
108+
109+
### Step 2 - Initialize an instance of the fixture class.
110+
Now that the fixture files have been created, the next step is to create an instance of the fixture library inside of our unit tests. Consider the following test (we're using PHPUnit here, but the testing framework doesn't matter; SimpleTest would work just as well):
111+
112+
in tests/exampleTest.php
113+
```php
114+
<?php
115+
116+
use Codesleeve\Fixture\Fixture;
117+
use Codesleeve\Fixture\Repositories\StandardRepository;
118+
119+
class ExampleTest extends PHPUnit_Framework_TestCase {
120+
121+
protected $fixture;
122+
protected $repository;
123+
124+
public function setUp()
125+
{
126+
if (!$this->repository)
127+
{
128+
$pdo = new pdo();
129+
$this->repository = new StandardRepository($pdo);
130+
}
131+
132+
$this->fixture = Fixture::getInstance();
133+
$this->fixture->setRepository($this->repository);
134+
$this->fixture->setConfig(array('location' => ''));
135+
$this->fixture->up();
136+
}
137+
138+
public function tearDown()
139+
{
140+
$this->fixture->down();
141+
}
142+
}
143+
?>
144+
```
145+
146+
What's going on here? A few things:
147+
* We're creating an instance of 'Codesleeve\Fixture\Repositories\StandardRepository' and caching it as a property on the test class.
148+
** This is the most basic repository avaialble for this package. It requires no ORM and has no concept of relationships.
149+
** In order to create a new repository we first need to instantiate a PDO database connection object and pass it as a parameter to the StandardRepository constructor.
150+
* We're creating a new instance of fixture via the getInstance() method (this is a singleton pattern).
151+
* We're injecting the stnadardRepository object into the fixture instance via the setRepository() method.
152+
* We're injecting in a configuration array with a location parameter that contains the file system location of the folder we want to load our fixtures from.
153+
* We're invoking the up() method on the fixture object. This method seeds the database and caches the inserted records as php standard objects on the fixture object.
154+
** Invoking the up method with no params will seed all fixtures.
155+
** Invoking the up method with an array of fixture names will seed only those fixtures (e.g $this->fixture->up(['soul_reapers']) would seed the soul_reapers table only).

0 commit comments

Comments
 (0)