From f03f3966cb88fc593434893391b2c0ae505801ac Mon Sep 17 00:00:00 2001 From: guidocella Date: Fri, 3 Feb 2017 08:53:29 +0100 Subject: [PATCH] Add raw method --- readme.md | 8 ++++++++ src/ModelPopulator.php | 12 ++++++++++++ src/Populator.php | 19 +++++++++++++++++++ tests/PopulatorTest.php | 30 +++++++++++++++++++++++++----- 4 files changed, 64 insertions(+), 5 deletions(-) diff --git a/readme.md b/readme.md index 94df6f4..df66524 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/src/ModelPopulator.php b/src/ModelPopulator.php index 88cfb2b..a4d941d 100644 --- a/src/ModelPopulator.php +++ b/src/ModelPopulator.php @@ -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. * diff --git a/src/Populator.php b/src/Populator.php index 3b169f0..018474e 100644 --- a/src/Populator.php +++ b/src/Populator.php @@ -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. * diff --git a/tests/PopulatorTest.php b/tests/PopulatorTest.php index 6ca3946..34f2bc1 100644 --- a/tests/PopulatorTest.php +++ b/tests/PopulatorTest.php @@ -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); @@ -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);