Skip to content

Commit

Permalink
Add raw method
Browse files Browse the repository at this point in the history
  • Loading branch information
guidocella committed Feb 3, 2017
1 parent 379deab commit f03f396
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,14 @@ $populator->make(User::class, ['admin' => true]);
$populator->create(User::class, 'admin', $otherAttributes);
```

Furthermore, you can call `raw` to `make` a model and convert it to an array.

```php
$user = $populator->raw(User::class, ['name' => 'foo']);

$user['name']; // "foo"
```

### Relations

`create` and `make` change how relations are associated.
Expand Down
12 changes: 12 additions & 0 deletions src/ModelPopulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,18 @@ public function getInsertRecords(array $insertedPKs)
return [$this->model, $tables, $pivotRecords, $foreignKeys];
}

/**
* Create the given model and convert it to an array.
*
* @param array $customAttributes Custom attributes that will override the guessed formatters.
* @return array
*/
public function raw($customAttributes = [])
{
// We actually need to make() the model first since closures attributes receive the model instance.
return $this->make($customAttributes)->toArray();
}

/**
* Create an instance of the given model and persist it to the database.
*
Expand Down
19 changes: 19 additions & 0 deletions src/Populator.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,25 @@ public function create(
return $this->add($modelClass, $quantity, $customAttributes, $modifiers)->create();
}

/**
* Create $quantity instances of the given model and convert them to arrays.
* The second argument can be either the quantity, custom attributes or a state.
*
* @param string $modelClass The class name of the Eloquent model to create.
* @param int|array|string $quantity The number of models to populate.
* @param array $customAttributes Custom attributes that will override the guessed formatters.
* @param array $modifiers Functions to call before the model is saved.
* @return array
*/
public function raw(
$modelClass,
$quantity = 1,
array $customAttributes = [],
array $modifiers = []
) {
return $this->add($modelClass, $quantity, $customAttributes, $modifiers)->raw();
}

/**
* Set the locales in which to create translations.
*
Expand Down
30 changes: 25 additions & 5 deletions tests/PopulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ public function testExecuteCreatesAndReturnsModels()
$this->assertSame(5, Post::count());
}

public function testMakeOne()
{
$user = $this->populator->make(User::class);

$this->assertInstanceOf(User::class, $user);

$this->assertFalse(User::exists());
}

public function testMakeMany()
{
$users = $this->populator->make(User::class, 5);

$this->assertCount(5, $users);

$this->assertFalse(User::exists());
}

public function testCreateOne()
{
$user = $this->populator->create(User::class);
Expand All @@ -46,18 +64,20 @@ public function testCreateMany()
$this->assertCount(5, $users);
}

public function testMakeOne()
public function testRawOne()
{
$user = $this->populator->make(User::class);
$user = $this->populator->raw(User::class);

$this->assertInstanceOf(User::class, $user);
$this->assertInternalType('array', $user);

$this->assertFalse(User::exists());
}

public function testMakeMany()
public function testRawMany()
{
$users = $this->populator->make(User::class, 5);
$users = $this->populator->raw(User::class, 5);

$this->assertInternalType('array', $users);

$this->assertCount(5, $users);

Expand Down

0 comments on commit f03f396

Please sign in to comment.