Framework: Factory create error in Array to string conversion

Created on 11 Jul 2019  路  7Comments  路  Source: laravel/framework

  • Laravel Version: 5.8.27
  • PHP Version: 7.2
  • Database Driver & Version: sqlite in memory

Description:

Captura de tela de 2019-07-11 18-37-50

Steps To Reproduce:

<?php

namespace Tests\Unit;

use App\Donation;
use App\DonatorStatus;
use App\PaymentForm;
use App\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class CreateDonationTest extends TestCase
{
    use DatabaseMigrations, RefreshDatabase;

    /**
     * A basic unit test example.
     * @test
     * @return void
     */
    public function CreateDonation()
    {
        //$status = factory(DonatorStatus::class)->create();
        // $formpayment = factory(PaymentForm::class)->create();
        // $donator = factory(User::class)->create();
        // dd($donator);

        $donation =
            factory(Donation::class)
                ->create();

        $this->assertDatabaseHas('donations', [
             'our_number' => '11233'
         ]);
        $this->assertEquals('11233', '11233');
    }
}

<?php

/* @var $factory \Illuminate\Database\Eloquent\Factory */

use App\Donation;
use Faker\Generator as Faker;

$factory->define(Donation::class, function (Faker $faker) {
    return [
        'amount' => $faker->randomFloat(2, 4),
        'paid_amount' => $faker->randomFloat(2, 4),
        'received_amount'=>$faker->randomFloat(2,4),
        'tax'=>$faker->randomFloat(2,3),
        'our_number'=>$faker->randomNumber(),
        'return_card'=>$faker->words,
        'result_card'=>$faker->words,
        'transaction_code'=>$faker->countryCode,
        'result_approved_card'=>$faker->words,
        'description'=>$faker->paragraph,
        'url_ticket_payment'=>$faker->url,
        'purse'=>$faker->word,
        'flag_card'=>$faker->word,
        'payment_form_id'=>factory(\App\PaymentForm::class)->create()->id,
        'donation_status_id'=>factory(\App\DonatorStatus::class)->create()->id,
        'donator_id'=>factory(\App\User::class)->create()->id,
        'user_id'=>factory(\App\User::class)->create()->id,
        'card_number'=>$faker->creditCardNumber,
        'payment_date' => $faker->dateTime,
        'maturity_date' => $faker->dateTime,
        'import_date'  => $faker->dateTime,
        'shipment_date'  => $faker->dateTime,
        'email_send_date'  => $faker->dateTime,
        'user_create_date'  => $faker->dateTime,
        'extra' => $faker->boolean
    ];
});

Most helpful comment

It is a problem with lines like this.

'return_card'=>$faker->words,
'result_card'=>$faker->words,

Please read the Faker documentation thoroughly and you should find the problem.

https://github.com/fzaninotto/Faker

All 7 comments

Igor, these are problems specific with your code, and not issues with the framework. Please ask questions like these on a site like StackOverflow, or the Laravel Slack channel

Github issues are only for reporting framework bugs.

Thanks!

It is a problem with lines like this.

'return_card'=>$faker->words,
'result_card'=>$faker->words,

Please read the Faker documentation thoroughly and you should find the problem.

https://github.com/fzaninotto/Faker

Oh, shit, sorry, very sorry, I think that could be with framework.

No need to apologize, just try those other options first next time.

Thanks!

ErrorException : Array to string conversion at /var/www/html/LaravelAppFolder/vendor/laravel/framework/src/Illuminate/Support/Str.php:353 349| 350| $result = array_shift($segments); 351| 352| foreach ($segments as $segment) { > 353| $result .= (array_shift($replace) ?? $search).$segment; 354| } 355| 356| return $result; 357| } Exception trace: 1 Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Array to string conversion", "/var/www/html/LaravelAppFolder/vendor/laravel/framework/src/Illuminate/Support/Str.php") /var/www/html/LaravelAppFolder/vendor/laravel/framework/src/Illuminate/Support/Str.php:353

Steps to reproduce the error:
Factory File

`

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Setting;
use Faker\Generator as Faker;

$factory->define(Setting::class, function (Faker $faker) {
return [
'header_logo' => $faker->image('public/images/settings/',
186,165,null, false),
'footer_logo' => $faker->image('public/images/settings/',
200,70,null, false),
'motto' => $faker->catchPhrase,
'header_words'=> $faker->sentence($nbWords = 8, $variableNbWords = true),
'location' => $faker->name,
'phone' => $faker->e164PhoneNumber,
'currency' => $faker->currencyCode,
'tANDc' => $faker->$faker->sentence($nbWords = 8, $variableNbWords = true),
'privacy_and_policy' => $faker->sentence($nbWords = 8, $variableNbWords = true),
'copyrites' => '2020 Demo System. All Rights Reserved',
'mission' => $faker->sentence($nbWords = 8, $variableNbWords = true),
'vision' => $faker->sentence($nbWords = 8, $variableNbWords = true),
'goal' => $faker->numberBetween($min = 100, $max = 900),
'fb' => $faker->url,
'twitter' => $faker->domainName,
'linkedin' => $faker->freeEmailDomain,
'instagram' => $faker->safeEmailDomain
];
});
`

Seeder File Below:

`

use Illuminate\Database\Seeder;
use App\Setting;

class SettingsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Setting::truncate();

    $setting = factory(Setting::class)->create();

}

}

`

In your factory, for "tANDc", your calling $factory->$factory. I fixed that error and ran your code, and it works as expected.

In your factory, for "tANDc", your calling $factory->$factory. I fixed that error and ran your code, and it works as expected.

Thank you @devcircus

Was this page helpful?
0 / 5 - 0 ratings

Related issues

felixsanz picture felixsanz  路  3Comments

kerbylav picture kerbylav  路  3Comments

iivanov2 picture iivanov2  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments

YannPl picture YannPl  路  3Comments