Support the ongoing development of Laravel.io →
posted 8 years ago
Testing

I have the following code:

// Departments
$physics = factory(App\Base\Academy\Department::class, 1)->create();
$chemistry = factory(App\Base\Academy\Department::class, 1)->create();
$biology = factory(App\Base\Academy\Department::class, 1)->create();

// Subjects
$subject_chemistry = factory(App\Base\Qualification\Subject::class, 1)->create([
    'name' => 'Chemistry',
]);
$subject_biology = factory(App\Base\Qualification\Subject::class, 1)->create([
    'name' => 'Biology',
]);
$subject_physics = factory(App\Base\Qualification\Subject::class, 1)->create([
    'name' => 'Physics',
]);

// Attach
$chemistry->attachToSubject($subject_chemistry);
$biology->attachToSubject($subject_biology);
$physics->attachToSubject($subject_physics);

Which is returning the following error:

PHPUnit 4.8.27 by Sebastian Bergmann and contributors.

.E.........................................

Time: 1.29 seconds, Memory: 26.00MB

There was 1 error:

1) DashboardTest::a_school_admin_sees_the_school_admin_section_on_the_dashboard
TypeError: Argument 1 passed to App\Base\Academy\Department::attachToSubject() must be an instance of Subject, instance of App\Base\Qualification\Subject given, called in /home/simon/Code/cpactracker.co.uk/tests/Acceptance/DashboardTest.php on line 69

/home/simon/Code/cpactracker.co.uk/app/Base/Academy/Department.php:51
/home/simon/Code/cpactracker.co.uk/tests/Acceptance/DashboardTest.php:69
/home/simon/Code/cpactracker.co.uk/tests/Acceptance/DashboardTest.php:35

Line 69 is the first line after the ~~~ // Attach ~~~ comment.

So it would appear that their is an error in the use statement of the Department class.

use Subject;

Which I have set as an alias elsewhere.

So if I change it to:

use App\Base\Qualification\Subject;

It works, but I lose the ease of using the alias.

However, if I change just one of the code blocks where the subjects are defined like so:

$subject_chemistry = factory(App\Base\Qualification\Subject::class, 1)->create([
    'name' => 'Chemistry',
]);
$subject_biology = factory(App\Base\Qualification\Subject::class, 1)->create([
    'name' => 'Biology',
]);
$subject_physics = Subject::create([
    'name' => 'Physics',
]);

(notice the change to the last definition).

It works even without changing the alias. It makes not difference what order the subject definitions are in, as long as one of them uses Subject::create, even if the other two still use model factories.

I'm baffled by this.

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

simonhunt simonhunt Joined 14 Jan 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.