From c2a50b2888d14627946c5052113becd08a0effd7 Mon Sep 17 00:00:00 2001 From: Benjamin Haines Date: Mon, 20 Apr 2015 10:49:48 +1200 Subject: [PATCH 1/5] Update composer dependencies Update travis --- .travis.yml | 16 ++++++++++------ composer.json | 20 ++++++++++---------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4a7ff2b..58318c2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,16 @@ language: php -php: - - 5.3 +php: - 5.4 - 5.5 + - 5.6 + - 7.0 + - hhvm -before_script: - - curl -s http://getcomposer.org/installer | php - - php composer.phar install --dev +matrix: + allow_failures: + - php: 7.0 -script: phpunit \ No newline at end of file +install: travis_retry composer install --no-interaction --prefer-source + +script: vendor/bin/phpunit diff --git a/composer.json b/composer.json index 7073c01..4598b8f 100644 --- a/composer.json +++ b/composer.json @@ -10,21 +10,21 @@ } ], "require": { - "php": ">=5.3.0", - "fzaninotto/faker": "1.3.0" + "php": ">=5.4.0", + "fzaninotto/faker": "~1.4" }, "require-dev": { - "mockery/mockery": "dev-master", - "illuminate/support": "~4", - "illuminate/database": "~4" + "mockery/mockery": "~0.9", + "illuminate/support": "~5", + "illuminate/database": "~5", + "phpunit/phpunit": "~4.6" }, "autoload": { - "psr-0": { - "Codesleeve\\Fixture": "src/" + "psr-4": { + "Codesleeve\\Fixture\\": "src/Codesleeve/Fixture/" }, "classmap": [ "tests/eloquent_models" ] - }, - "minimum-stability": "dev" -} \ No newline at end of file + } +} From 5cb848a43538a150b45119cebd2c277b97d60b59 Mon Sep 17 00:00:00 2001 From: Benjamin Haines Date: Mon, 20 Apr 2015 10:50:25 +1200 Subject: [PATCH 2/5] Use the latest stable faker api --- tests/FixtureTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/FixtureTest.php b/tests/FixtureTest.php index 68c69a0..a8e51da 100644 --- a/tests/FixtureTest.php +++ b/tests/FixtureTest.php @@ -30,8 +30,8 @@ public function tearDown() /** * Test that the fixture class is able to generate a single instance - * of itself. - * + * of itself. + * * @test * @return void */ @@ -72,7 +72,7 @@ public function it_should_throw_an_exception_if_the_fixture_is_not_an_array() } /** - * Test that an an exception is thrown when trying to access a fixture that + * Test that an an exception is thrown when trying to access a fixture that * does not exist * * @test @@ -98,9 +98,9 @@ public function it_should_throw_an_exception_if_the_fixture_name_does_not_exist( public function it_should_able_to_generate_fake_fixture_data() { $word = Fixture::fake('word'); - $number = Fixture::fake('randomNumber', 1, 1); + $number = Fixture::fake('numberBetween', 1, 1); $this->assertInternalType('string', $word); $this->assertEquals(1, $number); } -} \ No newline at end of file +} From 85d2030c60fc4220b93830c1d4b32ea87e3a5363 Mon Sep 17 00:00:00 2001 From: Benjamin Haines Date: Mon, 20 Apr 2015 12:49:21 +1200 Subject: [PATCH 3/5] Rename BaseDriver to PDODriver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rename BaseDriver to the more implicit PDODriver. Remove Str class from being able to be passed in. Str generally won’t change. --- src/Codesleeve/Fixture/Drivers/BaseDriver.php | 34 ----------- src/Codesleeve/Fixture/Drivers/Eloquent.php | 35 ++++------- src/Codesleeve/Fixture/Drivers/PDODriver.php | 59 +++++++++++++++++++ src/Codesleeve/Fixture/Drivers/Standard.php | 28 +-------- 4 files changed, 72 insertions(+), 84 deletions(-) delete mode 100644 src/Codesleeve/Fixture/Drivers/BaseDriver.php create mode 100644 src/Codesleeve/Fixture/Drivers/PDODriver.php diff --git a/src/Codesleeve/Fixture/Drivers/BaseDriver.php b/src/Codesleeve/Fixture/Drivers/BaseDriver.php deleted file mode 100644 index 971cc0e..0000000 --- a/src/Codesleeve/Fixture/Drivers/BaseDriver.php +++ /dev/null @@ -1,34 +0,0 @@ -tables as $table) { - $this->db->query("DELETE FROM $table"); - } - - $this->tables = array(); - } - - /** - * Generate an integer hash of a string. - * We'll use this method to convert a fixture's name into the - * primary key of it's corresponding database table record. - * - * @param string $value - This should be the name of the fixture. - * @return integer - */ - protected function generateKey($value) - { - $hash = sha1($value); - $integerHash = base_convert($hash, 16, 10); - - return (int)substr($integerHash, 0, 10); - } -} \ No newline at end of file diff --git a/src/Codesleeve/Fixture/Drivers/Eloquent.php b/src/Codesleeve/Fixture/Drivers/Eloquent.php index 78ec98c..aa0afcd 100644 --- a/src/Codesleeve/Fixture/Drivers/Eloquent.php +++ b/src/Codesleeve/Fixture/Drivers/Eloquent.php @@ -1,5 +1,6 @@ db = $db; - $this->str = $str; + $this->str = new Str(); + + parent::__construct($pdo); } /** @@ -184,11 +171,11 @@ protected function buildBelongsToManyRecord($recordName, Relation $relation, $re $otherKeyPieces = explode('.', $relation->getOtherKey()); $otherKeyName = $otherKeyPieces[1]; $otherKeyValue = $this->generateKey($relatedRecordName); - + $fields = "$foreignKeyName, $otherKeyName"; $values = array($foreignKeyValue, $otherKeyValue); - - foreach ($pivotColumns as $pivotColumn) + + foreach ($pivotColumns as $pivotColumn) { list($columnName, $columnValue) = explode(':', $pivotColumn); $fields .= ", $columnName"; @@ -208,4 +195,4 @@ protected function generateModelName($tableName) { return $this->str->singular(str_replace(' ', '', ucwords(str_replace('_', ' ', $tableName)))); } -} \ No newline at end of file +} diff --git a/src/Codesleeve/Fixture/Drivers/PDODriver.php b/src/Codesleeve/Fixture/Drivers/PDODriver.php new file mode 100644 index 0000000..87eef96 --- /dev/null +++ b/src/Codesleeve/Fixture/Drivers/PDODriver.php @@ -0,0 +1,59 @@ +db = $pdo; + } + /** + * Truncate a table. + */ + public function truncate() + { + foreach ($this->tables as $table) { + $this->db->query("DELETE FROM $table"); + } + + $this->tables = array(); + } + + /** + * Generate an integer hash of a string. + * We'll use this method to convert a fixture's name into the + * primary key of it's corresponding database table record. + * + * @param string $value - This should be the name of the fixture. + * + * @return int + */ + protected function generateKey($value) + { + $hash = sha1($value); + $integerHash = base_convert($hash, 16, 10); + return (int)substr($integerHash, 0, 10); + } +} diff --git a/src/Codesleeve/Fixture/Drivers/Standard.php b/src/Codesleeve/Fixture/Drivers/Standard.php index 8d35cc3..59ab83d 100644 --- a/src/Codesleeve/Fixture/Drivers/Standard.php +++ b/src/Codesleeve/Fixture/Drivers/Standard.php @@ -2,33 +2,9 @@ use PDO; -class Standard extends BaseDriver implements DriverInterface +class Standard extends PDODriver implements DriverInterface { /** - * A PDO connection instance. - * - * @var PDO - */ - protected $db; - - /** - * An array of tables that have had fixture data loaded into them. - * - * @var array - */ - protected $tables = array(); - - /** - * Constructor method - * - * @param PDO $db - */ - public function __construct(PDO $db) - { - $this->db = $db; - } - - /** * Build a fixture record using the passed in values. * * @param string $tableName @@ -93,4 +69,4 @@ protected function endsWith($haystack, $needle) { return $needle === "" || substr($haystack, -strlen($needle)) === $needle; } -} \ No newline at end of file +} From 39b9399bdbdb0e4f58ba5083c1e24f585fa9d420 Mon Sep 17 00:00:00 2001 From: Benjamin Haines Date: Mon, 20 Apr 2015 12:56:37 +1200 Subject: [PATCH 4/5] Add key generators MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add the ability to generate keys from a “key generator” - Include the default SHA1 and a Rails-like key generators - Add docs & tests --- README.md | 128 ++++++++++-------- src/Codesleeve/Fixture/Drivers/Eloquent.php | 7 +- src/Codesleeve/Fixture/Drivers/PDODriver.php | 26 +++- .../KeyGenerators/Crc32KeyGenerator.php | 25 ++++ .../KeyGenerators/KeyGeneratorInterface.php | 16 +++ .../KeyGenerators/SHA1KeyGenerator.php | 35 +++++ tests/Crc3KeyGeneratorTest.php | 16 +++ tests/EloquentTest.php | 12 +- tests/SHA1KeyGeneratorTest.php | 23 ++++ 9 files changed, 214 insertions(+), 74 deletions(-) create mode 100644 src/Codesleeve/Fixture/KeyGenerators/Crc32KeyGenerator.php create mode 100644 src/Codesleeve/Fixture/KeyGenerators/KeyGeneratorInterface.php create mode 100644 src/Codesleeve/Fixture/KeyGenerators/SHA1KeyGenerator.php create mode 100644 tests/Crc3KeyGeneratorTest.php create mode 100644 tests/SHA1KeyGeneratorTest.php diff --git a/README.md b/README.md index c8f3e7b..e2fc5b5 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ #Fixture [![Build Status](https://travis-ci.org/CodeSleeve/fixture.png?branch=development)](https://travis-ci.org/CodeSleeve/fixture) -[![Latest Stable Version](https://poser.pugx.org/codesleeve/fixture/v/stable.png)](https://packagist.org/packages/codesleeve/fixture) -[![Total Downloads](https://poser.pugx.org/codesleeve/fixture/downloads.png)](https://packagist.org/packages/codesleeve/fixture) -[![Latest Unstable Version](https://poser.pugx.org/codesleeve/fixture/v/unstable.png)](https://packagist.org/packages/codesleeve/fixture) +[![Latest Stable Version](https://poser.pugx.org/codesleeve/fixture/v/stable.png)](https://packagist.org/packages/codesleeve/fixture) +[![Total Downloads](https://poser.pugx.org/codesleeve/fixture/downloads.png)](https://packagist.org/packages/codesleeve/fixture) +[![Latest Unstable Version](https://poser.pugx.org/codesleeve/fixture/v/unstable.png)](https://packagist.org/packages/codesleeve/fixture) [![License](https://poser.pugx.org/codesleeve/fixture/license.png)](https://packagist.org/packages/codesleeve/fixture) A framework agnostic, simple (yet elegant) fixture library for php. @@ -49,67 +49,67 @@ class fooTest extends PHPUnit_Framework_TestCase * @var PDO */ protected $db; - + /** * Initialize our test state. * * @return void */ - public function setUp() + public function setUp() { // create a pdo instance if (!$this->db) { $this->db = new PDO('sqlite::memory:'); } - + // populate the users table $sql = 'INSERT INTO users (email, password, status, created_at, updated_at) values (?, ?, ?, ?, ?)'; $sth = $this->prepare($sql); $sth->execute(array('john@doe.com', 'abc12345$%^', 1, date('Y-m-d'), date('Y-m-d'))); - + $sql = 'INSERT INTO users (email, password, status, created_at, updated_at) values (?, ?, ?, ?, ?)'; $sth = $this->prepare($sql); $sth->execute(array('Jane', 'Doe', 'jane@doe.com', 1, 'abc12345$%^', date('Y-m-d'), date('Y-m-d'))); - + $sql = 'INSERT INTO users (email, password, status, created_at, updated_at) values (?, ?, ?, ?, ?)'; $sth = $this->prepare($sql); $sth->execute(array('Jim', 'Doe', 'jim@doe.com', 0, 'abc12345$%^', date('Y-m-d'), date('Y-m-d'))); - + // populate the roles table $sql = 'INSERT INTO roles (name, created_at, updated_at) values (?, ?, ?)'; $sth = $this->prepare($sql); $sth->execute(array('admin', date('Y-m-d'), date('Y-m-d'))); - + $sql = 'INSERT INTO roles (name, created_at, updated_at) values (?, ?, ?)'; $sth = $this->prepare($sql); $sth->execute(array('user', date('Y-m-d'), date('Y-m-d'))); - + // populate the roles_users table $sql = 'INSERT INTO roles_users (role_id, user_id) values (?, ?)'; $sth = $this->prepare($sql); $sth->execute(array(1, 1)); - + $sql = 'INSERT INTO roles_users (role_id, user_id) values (?, ?)'; $sth = $this->prepare($sql); $sth->execute(array(1, 2)); - + $sql = 'INSERT INTO roles_users (role_id, user_id) values (?, ?)'; $sth = $this->prepare($sql); $sth->execute(array(2, 3)); } - + /** * Reset our test state. * * @return void */ - public function tearDown + public function tearDown { $this->db->query("TRUNCATE TABLE users"); $this->db->query("TRUNCATE TABLE roles"); $this->db->query("TRUNCATE TABLE roles_users"); } - + /** * A database integration test of some sort. * @@ -129,38 +129,38 @@ into this: class fooTest extends PHPUnit_Framework_TestCase { use Codesleeve\Fixture\Fixture; - + /** * The fixture instance. * * @var Fixture */ protected $fixture; - + /** * Initialize our test state. * * @return void */ - public function setUp() + public function setUp() { // set the fixture instance $this->fixture = Fixture::getInstance(); - + // populate our tables $this->fixture->up(); } - + /** * Reset our test state. * * @return void */ - public function tearDown + public function tearDown { $this->fixture->down(); } - + /** * A database integration test of some sort. * @@ -206,16 +206,16 @@ $fixture = Fixture::getInstance($config, $driver); ## Examples For our examples, let's assume that we have the following bleach-themed system: -* Tables: +* Tables: * soul_reapers * zanpakutos * ranks * ranks_soul_reapers (columns: integer rank_id, integer soul_reaper_id, integer status). -* Relationships: +* Relationships: * A soul reaper has one zanpakuto, belongs to many ranks (many to many). * A zanpakuto belongs to one soul reaper only. * A rank belongs to many soul reapers. - + ### Standard Driver #### Step 1 - Fixture setup 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 our 'soul_reapers' table: @@ -225,11 +225,11 @@ in tests/fixtures/soul_reapers.php return array ( 'Ichigo' => array ( 'first_name' => 'Ichigo', - 'last_name' => 'Kurosaki' + 'last_name' => 'Kurosaki' ), 'Renji' => array ( 'first_name' => 'Renji', - 'last_name' => 'Abarai' + 'last_name' => 'Abarai' ), 'Genryusai' => array( 'first_name' => 'Genryusai', @@ -302,7 +302,7 @@ return array ( ); ``` -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. +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. #### Step 2 - Initialize an instance of the fixture class. Now that the fixture files have been created, the next step is to create an instance of the fixture library inside of our tests. Consider the following test (we're using PHPUnit here, but the testing framework doesn't matter; SimpleTest would work just as well): @@ -312,34 +312,34 @@ in tests/exampleTest.php use Codesleeve\Fixture\Fixture; use Codesleeve\Fixture\Drivers\Standard as StandardDriver; - + /** * The fixture instance. * * @var Fixture */ protected $fixture; - + /** * Initialize our test state. * * @return void */ - public function setUp() + public function setUp() { // set the fixture instance $this->fixture = Fixture::getInstance(); - + // populate our tables $this->fixture->up(); } - + /** * Reset our test state. * * @return void */ - public function tearDown + public function tearDown { $this->fixture->down(); } @@ -362,23 +362,23 @@ echo $this->fixture->soul_reapers('Ichigo')->last_name; #### Step 1 - Model setup Inside your models folder (or wherever you currently store your models at), create both a SoulReaper and a Zanpakuto model: ```php - class SoulReaper extends Eloquent + class SoulReaper extends Eloquent { protected $table = 'soul_reapers'; - + /** * A soul reaper has one zanpakuto. - * + * * @return hasOne */ public function zanpakuto() { return $this->hasOne('Zanpakuto'); } - + /** * A soul reaper belongs to many ranks. - * + * * @return belongsToMany */ public function ranks() @@ -386,14 +386,14 @@ Inside your models folder (or wherever you currently store your models at), crea return $this->belongsToMany('ranks'); } } - - class Zanpakuto extends Eloquent + + class Zanpakuto extends Eloquent { protected $table = 'zanpakutos'; - + /** * A zanpakuto belongs to a Soul Reaper. - * + * * @return belongsTo */ public function soulReaper() @@ -412,12 +412,12 @@ return array ( 'Ichigo' => array ( 'first_name' => 'Ichigo', 'last_name' => 'Kurosaki', - 'ranks' => array('Substitute|active:1') + 'ranks' => array('Substitute|active:1') ), 'Renji' => array ( 'first_name' => 'Renji', 'last_name' => 'Abarai', - 'ranks' => array('Lieutenant|active:1') + 'ranks' => array('Lieutenant|active:1') ), 'Genryusai' => array( 'first_name' => 'Genryusai', @@ -474,19 +474,19 @@ Because we know that a Zanpakto has a 'belongsTo' relationship with a SoulReaper 'soulReaper' => 'Ichigo', 'name' => 'Zangetsu', ), - ``` + ``` Many to many (N to N) join table relationships can also be populated. In our running example, soul reapers have a many to many (belongsToMany) relationship with ranks. In essence, a soul reaper can have many ranks and ranks can belong to many soul reapers (notice that Genryusai has both 'Commander' and 'Captain' ranks; that's because Genryusai Yammamoto was the Captain Commander of the Gotei 13). To represent this, we simply assign an array to a value that's named after the belongsToMany relationship of the fixture's corresponding model. In our example, from the soul reapers side (of the belongsToMany relationship), we simple pass an array of 'ranks' (since we defined a belongsToMany relationship named 'ranks' inside our SoulReaper model) we want a soul reaper to have. Extra columns on the join table can be populated using a '|' delimiter with key/values separated with a ':'. ```php -// This assigns Genryusai the ranks of Captain and Commander and +// This assigns Genryusai the ranks of Captain and Commander and // sets the 'active' pivot column to 1 for each rank. 'Genryusai' => array( 'first_name' => 'Genryusai', 'last_name' => 'Yammamoto', 'ranks' => array('Captain|active:1', 'Commander|active:1') ) -``` +``` #### Step 3 - Initialize an instance of the fixture class. Now that the fixture files have been created, the next step is to create an instance of the fixture library inside of our tests. Consider the following test (we're using PHPUnit here, but the testing framework doesn't matter; SimpleTest would work just as well): @@ -496,36 +496,36 @@ in tests/exampleTest.php use Codesleeve\Fixture\Fixture; use Codesleeve\Fixture\Drivers\Eloquent as EloquentDriver; - + /** * The fixture instance. * * @var Fixture */ protected $fixture; - + /** * Initialize our test state. * * @return void */ - public function setUp() + public function setUp() { // set the fixture instance $db = new \PDO('sqlite::memory:'); $driver = new EloquentDriver($db); $this->fixture = Fixture::getInstance(array('location' => 'path/to/your/fixtures.php'), $driver); - + // populate our tables $this->fixture->up(); } - + /** * Reset our test state. * * @return void */ - public function tearDown + public function tearDown { $this->fixture->down(); } @@ -541,11 +541,25 @@ Fixture has built in integration with [Faker](https://github.com/fzaninotto/Fake 'ranks' => array('Substitute|active:1'), 'bio' => Fixture::fake('text'), 'age' => Fixture::fake('randomNumber', 15, 18), - 'address' => Fixture::fake('address') + 'address' => Fixture::fake('address') ), ``` By using fixtures to seed our test database we've gained very precise control over what's in our database at any given time during an integration test. This in turn allows us to very easily test the pieces of our application that contain database specific logic. +## Key generators +Fixture can support generating primary keys & relation keys for fixture data with different generators. To use a different key generator then the default SHA1KeyGenerator generator. You just need to pass an instance of that generator to the driver. Both the Eloquent & Standard drivers support it. + +```php + +use Codesleeve\Fixture\KeyGenerators\Crc32KeyGenerator; + +$generator = new Crc32KeyGenerator(); +//$generator = new SHA1KeyGenerator(10); + +new EloquentDriver($db, $generator); + +``` + ## Contributing -Fixture is always open to contributions from the community, however I ask that you please make all pull request to the development branch only. Let me reiterate this; I will not be accepting pull requests on master. \ No newline at end of file +Fixture is always open to contributions from the community, however I ask that you please make all pull request to the development branch only. Let me reiterate this; I will not be accepting pull requests on master. diff --git a/src/Codesleeve/Fixture/Drivers/Eloquent.php b/src/Codesleeve/Fixture/Drivers/Eloquent.php index aa0afcd..781dd49 100644 --- a/src/Codesleeve/Fixture/Drivers/Eloquent.php +++ b/src/Codesleeve/Fixture/Drivers/Eloquent.php @@ -20,13 +20,14 @@ class Eloquent extends PDODriver implements DriverInterface /** * Constructor method * - * @param DatabaseManager $pdo + * @param DatabaseManager $db + * @param KeyGeneratorInterface $keyGenerator */ - public function __construct(PDO $pdo) + public function __construct(PDO $pdo, KeyGeneratorInterface $keyGenerator = null) { $this->str = new Str(); - parent::__construct($pdo); + parent::__construct($pdo, $keyGenerator); } /** diff --git a/src/Codesleeve/Fixture/Drivers/PDODriver.php b/src/Codesleeve/Fixture/Drivers/PDODriver.php index 87eef96..0322d84 100644 --- a/src/Codesleeve/Fixture/Drivers/PDODriver.php +++ b/src/Codesleeve/Fixture/Drivers/PDODriver.php @@ -20,14 +20,28 @@ class PDODriver */ protected $tables = array(); + /** + * An instance of a key generator + * + * @var KeyGeneratorInterface + */ + protected $keyGenerator; + + /** * Constructor method * * @param DatabaseManager $db + * @param KeyGeneratorInterface $keyGenerator */ - public function __construct(PDO $pdo) + public function __construct(PDO $pdo, KeyGeneratorInterface $keyGenerator = null) { + if ($keyGenerator === null) { + $keyGenerator = new SHA1KeyGenerator(); + } + $this->db = $pdo; + $this->keyGenerator = $keyGenerator; } /** * Truncate a table. @@ -50,10 +64,8 @@ public function truncate() * * @return int */ - protected function generateKey($value) - { - $hash = sha1($value); - $integerHash = base_convert($hash, 16, 10); - return (int)substr($integerHash, 0, 10); - } + protected function generateKey($value) + { + return $this->keyGenerator->generateKey($value); + } } diff --git a/src/Codesleeve/Fixture/KeyGenerators/Crc32KeyGenerator.php b/src/Codesleeve/Fixture/KeyGenerators/Crc32KeyGenerator.php new file mode 100644 index 0000000..3bf859b --- /dev/null +++ b/src/Codesleeve/Fixture/KeyGenerators/Crc32KeyGenerator.php @@ -0,0 +1,25 @@ +length = $length; + } + + /** + * {@inheritDoc} + */ + public function generateKey($value) + { + $hash = sha1($value); + $integerHash = base_convert($hash, 16, 10); + + return (int) substr($integerHash, 0, $this->length); + } +} diff --git a/tests/Crc3KeyGeneratorTest.php b/tests/Crc3KeyGeneratorTest.php new file mode 100644 index 0000000..46aa645 --- /dev/null +++ b/tests/Crc3KeyGeneratorTest.php @@ -0,0 +1,16 @@ +generateKey('foo'); + + $this->assertEquals(9, strlen($key)); + $this->assertEquals(208889123, $key); + } +} diff --git a/tests/EloquentTest.php b/tests/EloquentTest.php index ef40b7d..6adb3f7 100644 --- a/tests/EloquentTest.php +++ b/tests/EloquentTest.php @@ -1,8 +1,7 @@ -db = $this->buildDB(); - $str = new Str; $this->fixture = Fixture::getInstance(); - $repository = new Eloquent($this->db, $str); + $repository = new Eloquent($this->db); $this->fixture->setDriver($repository); // Bootstrap Eloquent @@ -164,7 +162,7 @@ protected function buildDB() /** * Helper method to return the current record count in each * fixture table. - * + * * @return array */ protected function getRecordCounts() diff --git a/tests/SHA1KeyGeneratorTest.php b/tests/SHA1KeyGeneratorTest.php new file mode 100644 index 0000000..e922f2a --- /dev/null +++ b/tests/SHA1KeyGeneratorTest.php @@ -0,0 +1,23 @@ +generateKey('foo'); + + $this->assertEquals(10, strlen($key)); + $this->assertEquals(6812387308, $key); + } + + public function testReturnsValidKeyWithCustomLength() + { + $generator = new SHA1KeyGenerator(8); + + $this->assertEquals(8, strlen($generator->generateKey('foo'))); + } +} From 2d88ba1b3cb3093892a262ce0d66e721d45fd812 Mon Sep 17 00:00:00 2001 From: Benjamin Haines Date: Mon, 20 Apr 2015 13:18:02 +1200 Subject: [PATCH 5/5] Fix for > php 5.6 --- src/Codesleeve/Fixture/KeyGenerators/Crc32KeyGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Codesleeve/Fixture/KeyGenerators/Crc32KeyGenerator.php b/src/Codesleeve/Fixture/KeyGenerators/Crc32KeyGenerator.php index 3bf859b..5ffd641 100644 --- a/src/Codesleeve/Fixture/KeyGenerators/Crc32KeyGenerator.php +++ b/src/Codesleeve/Fixture/KeyGenerators/Crc32KeyGenerator.php @@ -12,7 +12,7 @@ class Crc32KeyGenerator implements KeyGeneratorInterface */ public function __construct() { - define(__NAMESPACE__.'\MAX_ID', 2 ** 30 - 1); + define(__NAMESPACE__.'\MAX_ID', pow(2, 30) - 1); } /**