Skip to content

Commit

Permalink
Merge pull request #2442 from janhenkgerritsen/2.1
Browse files Browse the repository at this point in the history
Add wrapper methods for Laravel 5 model factories to Laravel 5 module.
  • Loading branch information
janhenkgerritsen committed Oct 12, 2015
2 parents 44f82a9 + 0155118 commit 35cff9d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,8 @@
# Changelog

#### 2.1.4
[Laravel5] Added wrapper methods for Laravel 5 model factories. See #2442. By @janhenkgerritsen

#### 2.1.3

* [REST] **Added matching data types** by with new methods `seeResponseMatchesJsonType` and `dontSeeResponseMatchesJsonType`. See #2391
Expand Down
94 changes: 93 additions & 1 deletion src/Codeception/Module/Laravel5.php
Expand Up @@ -2,6 +2,7 @@
namespace Codeception\Module;

use Codeception\Exception\ModuleConfigException;
use Codeception\Exception\ModuleException;
use Codeception\Lib\Connector\Laravel5 as LaravelConnector;
use Codeception\Lib\Framework;
use Codeception\Lib\Interfaces\ActiveRecord;
Expand All @@ -24,7 +25,7 @@
* ## Status
*
* * Maintainer: **Jan-Henk Gerritsen**
* * Stability: **dev**
* * Stability: **stable**
* * Contact: janhenkgerritsen@gmail.com
*
* ## Example
Expand Down Expand Up @@ -731,4 +732,95 @@ protected function findRecord($tableName, $attributes = [])
return $query->first();
}

/**
* Use Laravel's model factory to create a model.
* Can only be used with Laravel 5.1 and later.
*
* ``` php
* <?php
* $I->haveModel('App\User');
* $I->haveModel('App\User', ['name' => 'John Doe']);
* $I->haveModel('App\User', [], 'admin');
* $I->haveModel('App\User', [], 'admin', 3);
* ?>
* ```
*
* @see http://laravel.com/docs/5.1/testing#model-factories
* @param string $model
* @param array $attributes
* @param string $name
* @param int $times
* @return mixed
*/
public function haveModel($model, $attributes = [], $name = 'default', $times = 1)
{
return $this->createModel($model, $attributes, $name, $times);
}

/**
* Use Laravel's model factory to create a model.
* Can only be used with Laravel 5.1 and later.
*
* ``` php
* <?php
* $I->createModel('App\User');
* $I->createModel('App\User', ['name' => 'John Doe']);
* $I->createModel('App\User', [], 'admin');
* $I->createModel('App\User', [], 'admin', 3);
* ?>
* ```
*
* @see http://laravel.com/docs/5.1/testing#model-factories
* @param string $model
* @param array $attributes
* @param string $name
* @param int $times
* @return mixed
*/
public function createModel($model, $attributes = [], $name = 'default', $times = 1)
{
return $this->modelFactory($model, $name, $times)->create($attributes);
}

/**
* Use Laravel's model factory to make a model.
* Can only be used with Laravel 5.1 and later.
*
* ``` php
* <?php
* $I->makeModel('App\User');
* $I->makeModel('App\User', ['name' => 'John Doe']);
* $I->makeModel('App\User', [], 'admin');
* $I->makeModel('App\User', [], 'admin', 3);
* ?>
* ```
*
* @see http://laravel.com/docs/5.1/testing#model-factories
* @param string $model
* @param array $attributes
* @param string $name
* @param int $times
* @return mixed
*/
public function makeModel($model, $attributes = [], $name = 'default', $times = 1)
{
return $this->modelFactory($model, $name, $times)->make($attributes);
}

/**
* @param string $model
* @param string $name
* @param int $times
* @return \Illuminate\Database\Eloquent\FactoryBuilder
* @throws ModuleException
*/
protected function modelFactory($model, $name, $times)
{
if (! function_exists('factory')) {
throw new ModuleException($this, 'The factory() method does not exist. ' .
'This functionality relies on Laravel model factories, which were introduced in Laravel 5.1.');
}

return factory($model, $name, $times);
}
}

0 comments on commit 35cff9d

Please sign in to comment.