0

I am working on a project which has Laravel 5.3 version and using Laravel Infyom Generator, somehow it generated all these traits and other test files such as (ApiTest, RepositoryTest, ...etc). when I try to run PHPUNIT I am getting this error Can someone help me to find out why I am getting this error?

PHP Fatal error:  Trait 'MakeCustomerTrait' not found in C:\Users\ahmed\dev\gamla\tests\CustomerApiTest.php on line 8

Fatal error: Trait 'MakeCustomerTrait' not found in C:\Users\ahmed\dev\gamla\tests\CustomerApiTest.php on line 8

I want to start making new tests for my project do I need to delete these files? because it keeps giving me that error?

screenshot of CustomerApiTest code: enter image description here

MakeCustomerTrait

<?php

use Faker\Factory as Faker;
use App\Models\Customer;
use App\Repositories\CustomerRepository;

trait MakeCustomerTrait
{
    /**
     * Create fake instance of Customer and save it in database
     *
     * @param array $customerFields
     * @return Customer
     */
    public function makeCustomer($customerFields = [])
    {
        /** @var CustomerRepository $customerRepo */
        $customerRepo = App::make(CustomerRepository::class);
        $theme = $this->fakeCustomerData($customerFields);
        return $customerRepo->create($theme);
    }

    /**
     * Get fake instance of Customer
     *
     * @param array $customerFields
     * @return Customer
     */
    public function fakeCustomer($customerFields = [])
    {
        return new Customer($this->fakeCustomerData($customerFields));
    }

    /**
     * Get fake data of Customer
     *
     * @param array $postFields
     * @return array
     */
    public function fakeCustomerData($customerFields = [])
    {
        $fake = Faker::create();

        return array_merge([
            'name' => $fake->word,
            'address_street' => $fake->word,
            'address_zip' => $fake->word,
            'address_city' => $fake->word,
            'address_country' => $fake->word,
            'shipping_address_street' => $fake->word,
            'shipping_address_zip' => $fake->word,
            'shipping_address_city' => $fake->word,
            'shipping_address_country' => $fake->word,
            'contact_person_id' => $fake->randomDigitNotNull,
            'created_at' => $fake->word,
            'updated_at' => $fake->word
        ], $customerFields);
    }
}

1 Answers1

0

Actually what is happening is here is autoloader is unable to resolve the class in your code. Laravel uses PSR-4 standards to autoload classes which needs fully qualified name spaces for the class which also represents the path of the file which holds the class.

See it this way that if you want to load a class inside your Laravel app directory at the following address :

app/repositories/users/UserRoleRepository.php

You will specify the namespace of your class like App\repositories\users with class name UserRoleRepository so the autoloader can load your class. Otherwise you have to include the file of your class manually.

You can register custom autoloads in your composer.json and by running the following command composer dump-autoload.

You can find more about it over internet like this

Hope it would help.

Parantap Parashar
  • 1,930
  • 1
  • 14
  • 22
  • I already have **"App\\": "app/"** in my composer.json, Can I add more than one? Please can you show it to me I really got headache since morning :/ – Ahmed Abdulrahman Mar 22 '17 at 13:45
  • I added this **"Tests\\": "tests/"** as well, then spefity the namespace like this : **use Tests\Traits\MakeCustomerTrait;** but this time I am getting this error **PHP Fatal error: Trait 'Tests\Traits\MakeCustomerTrait' not found in C:\Users\ahmed\dev\myfurhat\tests\CustomerApiTest.php on line 8 Fatal error: Trait 'Tests\Traits\MakeCustomerTrait' not found in C:\Users\ahmed\dev\myfurhat\tests\CustomerApiTest.php on line 8** – Ahmed Abdulrahman Mar 22 '17 at 13:48
  • You can always manually include the files of your trait using php's `include` method. It would be okay for test run. – Parantap Parashar Mar 22 '17 at 14:00