Weird behavior with models and phpunit in laravel - Trying to get property 'id' of non-object
I am having some very strange behavior with PHPUnit in laravel. I have a test invoices_are_split_between_extensions
that creates some timesheets with entries and a contract that has a special condition that needs to be tested.
I crammed up the functions needed into one function that runs the test as example.
/** @test */
public function invoices_are_split_between_extensions()
{
$user = $this->user();
$contract = factory(Contract::class)->states('complete')->create([
'user_id' => $user->id
]);
$extension = factory(ContractExtension::class)->create([
'contract_id' => $contract->id,
'start' => '2018-01-01',
'end' => '2018-01-15',
'sp' => '50',
'pp' => '40',
]);
$extension2 = factory(ContractExtension::class)->create([
'contract_id' => $contract->id,
'start' => '2018-01-16',
'end' => '2018-01-31',
'sp' => '50',
'pp' => '40',
]);
$timesheet = factory(Timesheet::class)->create([
'user_id' => $user->id,
'customer_id' => $contract->customer_id,
'contract_id' => $contract->id,
'extension_id' => $extension->id,
'total' => 16,
'date' => '2018-01-01',
]);
$entry = factory(TimesheetEntry::class)->create([
'date' => '2018-01-14',
'value' => 8,
'user_id' => $user->id,
'tariff_id' => 1,
'contract_id' => $contract->id,
'customer_id' => $contract->customer_id,
'extension_id' => $extension->id,
'timesheet_id' => $timesheet->id,
'invoiced' => false,
]);
$entry1 = factory(TimesheetEntry::class)->create([
'date' => '2018-01-15',
'value' => 8,
'user_id' => $user->id,
'tariff_id' => 1,
'contract_id' => $contract->id,
'customer_id' => $contract->customer_id,
'extension_id' => $extension->id,
'timesheet_id' => $timesheet->id,
'invoiced' => false,
]);
$entry2 = factory(TimesheetEntry::class)->create([
'date' => '2018-01-16',
'value' => 4,
'user_id' => $user->id,
'tariff_id' => 1,
'contract_id' => $contract->id,
'customer_id' => $contract->customer_id,
'extension_id' => $extension2->id,
'timesheet_id' => $timesheet->id,
'invoiced' => false,
]);
$invoices = Invoice::fromTimesheet($timesheet);
$this->assertCount(2, $invoices);
$this->assertEquals(800, $invoices[0]->total);
$this->assertEquals(200, $invoices[1]->total);
$this->assertEquals($extension->id, $invoices[0]->extension_id);
$this->assertEquals($extension2->id, $invoices[1]->extension_id);
}
This test throws an error ErrorException: Trying to get property 'id' of non-object
1) TestsFeatureInvoiceTest::invoices_are_split_between_extensions
ErrorException: Trying to get property 'id' of non-object
/home/ilyas/script/clockwork/contract-module/src/Models/Contract.php:435
/home/ilyas/script/clockwork/timesheet-module/src/Models/Timesheet.php:293
/home/ilyas/script/clockwork/app/src/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1011
/home/ilyas/script/clockwork/app/src/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php:261
/home/ilyas/script/clockwork/timesheet-module/src/Models/Timesheet.php:303
/home/ilyas/script/clockwork/invoice-module/src/Models/Invoice.php:272
/home/ilyas/script/clockwork/app/src/tests/Feature/InvoiceTest.php:145
When I go to line 435 in Contract.php
I find the following line. Where $tariff->id
is according to the error not an object.
$override = $this->tariffs->where('id', $tariff->id)->first()->pivot->sp ?? null;
After a quick dd()
I find that this is an object, an instance of Tariff
called as Tariff::findOrFail($key);
this returns a valid eloquent object.
Now the fun part begins. If I get the id like $tariff['id']
it works. If I try something like $tariff->toArray()
I get the error Error: Call to a member function toArray() on null
.
$tariff = $tariff->toArray();
$override = $this->tariffs->where('id', $tariff['id'])->first()->pivot->sp ?? null;
If I dd()
$tariff before the toArray()
I get an Eloquent collection. If I dd()
after $override
I get the same collection as an array. If I remove the dd()
the error reappears... This is driving me crazy for hours.
I easily can fix that function by using tariff['id']
but I got to know what could be causing this issue.
laravel eloquent phpunit
add a comment |
I am having some very strange behavior with PHPUnit in laravel. I have a test invoices_are_split_between_extensions
that creates some timesheets with entries and a contract that has a special condition that needs to be tested.
I crammed up the functions needed into one function that runs the test as example.
/** @test */
public function invoices_are_split_between_extensions()
{
$user = $this->user();
$contract = factory(Contract::class)->states('complete')->create([
'user_id' => $user->id
]);
$extension = factory(ContractExtension::class)->create([
'contract_id' => $contract->id,
'start' => '2018-01-01',
'end' => '2018-01-15',
'sp' => '50',
'pp' => '40',
]);
$extension2 = factory(ContractExtension::class)->create([
'contract_id' => $contract->id,
'start' => '2018-01-16',
'end' => '2018-01-31',
'sp' => '50',
'pp' => '40',
]);
$timesheet = factory(Timesheet::class)->create([
'user_id' => $user->id,
'customer_id' => $contract->customer_id,
'contract_id' => $contract->id,
'extension_id' => $extension->id,
'total' => 16,
'date' => '2018-01-01',
]);
$entry = factory(TimesheetEntry::class)->create([
'date' => '2018-01-14',
'value' => 8,
'user_id' => $user->id,
'tariff_id' => 1,
'contract_id' => $contract->id,
'customer_id' => $contract->customer_id,
'extension_id' => $extension->id,
'timesheet_id' => $timesheet->id,
'invoiced' => false,
]);
$entry1 = factory(TimesheetEntry::class)->create([
'date' => '2018-01-15',
'value' => 8,
'user_id' => $user->id,
'tariff_id' => 1,
'contract_id' => $contract->id,
'customer_id' => $contract->customer_id,
'extension_id' => $extension->id,
'timesheet_id' => $timesheet->id,
'invoiced' => false,
]);
$entry2 = factory(TimesheetEntry::class)->create([
'date' => '2018-01-16',
'value' => 4,
'user_id' => $user->id,
'tariff_id' => 1,
'contract_id' => $contract->id,
'customer_id' => $contract->customer_id,
'extension_id' => $extension2->id,
'timesheet_id' => $timesheet->id,
'invoiced' => false,
]);
$invoices = Invoice::fromTimesheet($timesheet);
$this->assertCount(2, $invoices);
$this->assertEquals(800, $invoices[0]->total);
$this->assertEquals(200, $invoices[1]->total);
$this->assertEquals($extension->id, $invoices[0]->extension_id);
$this->assertEquals($extension2->id, $invoices[1]->extension_id);
}
This test throws an error ErrorException: Trying to get property 'id' of non-object
1) TestsFeatureInvoiceTest::invoices_are_split_between_extensions
ErrorException: Trying to get property 'id' of non-object
/home/ilyas/script/clockwork/contract-module/src/Models/Contract.php:435
/home/ilyas/script/clockwork/timesheet-module/src/Models/Timesheet.php:293
/home/ilyas/script/clockwork/app/src/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1011
/home/ilyas/script/clockwork/app/src/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php:261
/home/ilyas/script/clockwork/timesheet-module/src/Models/Timesheet.php:303
/home/ilyas/script/clockwork/invoice-module/src/Models/Invoice.php:272
/home/ilyas/script/clockwork/app/src/tests/Feature/InvoiceTest.php:145
When I go to line 435 in Contract.php
I find the following line. Where $tariff->id
is according to the error not an object.
$override = $this->tariffs->where('id', $tariff->id)->first()->pivot->sp ?? null;
After a quick dd()
I find that this is an object, an instance of Tariff
called as Tariff::findOrFail($key);
this returns a valid eloquent object.
Now the fun part begins. If I get the id like $tariff['id']
it works. If I try something like $tariff->toArray()
I get the error Error: Call to a member function toArray() on null
.
$tariff = $tariff->toArray();
$override = $this->tariffs->where('id', $tariff['id'])->first()->pivot->sp ?? null;
If I dd()
$tariff before the toArray()
I get an Eloquent collection. If I dd()
after $override
I get the same collection as an array. If I remove the dd()
the error reappears... This is driving me crazy for hours.
I easily can fix that function by using tariff['id']
but I got to know what could be causing this issue.
laravel eloquent phpunit
add a comment |
I am having some very strange behavior with PHPUnit in laravel. I have a test invoices_are_split_between_extensions
that creates some timesheets with entries and a contract that has a special condition that needs to be tested.
I crammed up the functions needed into one function that runs the test as example.
/** @test */
public function invoices_are_split_between_extensions()
{
$user = $this->user();
$contract = factory(Contract::class)->states('complete')->create([
'user_id' => $user->id
]);
$extension = factory(ContractExtension::class)->create([
'contract_id' => $contract->id,
'start' => '2018-01-01',
'end' => '2018-01-15',
'sp' => '50',
'pp' => '40',
]);
$extension2 = factory(ContractExtension::class)->create([
'contract_id' => $contract->id,
'start' => '2018-01-16',
'end' => '2018-01-31',
'sp' => '50',
'pp' => '40',
]);
$timesheet = factory(Timesheet::class)->create([
'user_id' => $user->id,
'customer_id' => $contract->customer_id,
'contract_id' => $contract->id,
'extension_id' => $extension->id,
'total' => 16,
'date' => '2018-01-01',
]);
$entry = factory(TimesheetEntry::class)->create([
'date' => '2018-01-14',
'value' => 8,
'user_id' => $user->id,
'tariff_id' => 1,
'contract_id' => $contract->id,
'customer_id' => $contract->customer_id,
'extension_id' => $extension->id,
'timesheet_id' => $timesheet->id,
'invoiced' => false,
]);
$entry1 = factory(TimesheetEntry::class)->create([
'date' => '2018-01-15',
'value' => 8,
'user_id' => $user->id,
'tariff_id' => 1,
'contract_id' => $contract->id,
'customer_id' => $contract->customer_id,
'extension_id' => $extension->id,
'timesheet_id' => $timesheet->id,
'invoiced' => false,
]);
$entry2 = factory(TimesheetEntry::class)->create([
'date' => '2018-01-16',
'value' => 4,
'user_id' => $user->id,
'tariff_id' => 1,
'contract_id' => $contract->id,
'customer_id' => $contract->customer_id,
'extension_id' => $extension2->id,
'timesheet_id' => $timesheet->id,
'invoiced' => false,
]);
$invoices = Invoice::fromTimesheet($timesheet);
$this->assertCount(2, $invoices);
$this->assertEquals(800, $invoices[0]->total);
$this->assertEquals(200, $invoices[1]->total);
$this->assertEquals($extension->id, $invoices[0]->extension_id);
$this->assertEquals($extension2->id, $invoices[1]->extension_id);
}
This test throws an error ErrorException: Trying to get property 'id' of non-object
1) TestsFeatureInvoiceTest::invoices_are_split_between_extensions
ErrorException: Trying to get property 'id' of non-object
/home/ilyas/script/clockwork/contract-module/src/Models/Contract.php:435
/home/ilyas/script/clockwork/timesheet-module/src/Models/Timesheet.php:293
/home/ilyas/script/clockwork/app/src/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1011
/home/ilyas/script/clockwork/app/src/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php:261
/home/ilyas/script/clockwork/timesheet-module/src/Models/Timesheet.php:303
/home/ilyas/script/clockwork/invoice-module/src/Models/Invoice.php:272
/home/ilyas/script/clockwork/app/src/tests/Feature/InvoiceTest.php:145
When I go to line 435 in Contract.php
I find the following line. Where $tariff->id
is according to the error not an object.
$override = $this->tariffs->where('id', $tariff->id)->first()->pivot->sp ?? null;
After a quick dd()
I find that this is an object, an instance of Tariff
called as Tariff::findOrFail($key);
this returns a valid eloquent object.
Now the fun part begins. If I get the id like $tariff['id']
it works. If I try something like $tariff->toArray()
I get the error Error: Call to a member function toArray() on null
.
$tariff = $tariff->toArray();
$override = $this->tariffs->where('id', $tariff['id'])->first()->pivot->sp ?? null;
If I dd()
$tariff before the toArray()
I get an Eloquent collection. If I dd()
after $override
I get the same collection as an array. If I remove the dd()
the error reappears... This is driving me crazy for hours.
I easily can fix that function by using tariff['id']
but I got to know what could be causing this issue.
laravel eloquent phpunit
I am having some very strange behavior with PHPUnit in laravel. I have a test invoices_are_split_between_extensions
that creates some timesheets with entries and a contract that has a special condition that needs to be tested.
I crammed up the functions needed into one function that runs the test as example.
/** @test */
public function invoices_are_split_between_extensions()
{
$user = $this->user();
$contract = factory(Contract::class)->states('complete')->create([
'user_id' => $user->id
]);
$extension = factory(ContractExtension::class)->create([
'contract_id' => $contract->id,
'start' => '2018-01-01',
'end' => '2018-01-15',
'sp' => '50',
'pp' => '40',
]);
$extension2 = factory(ContractExtension::class)->create([
'contract_id' => $contract->id,
'start' => '2018-01-16',
'end' => '2018-01-31',
'sp' => '50',
'pp' => '40',
]);
$timesheet = factory(Timesheet::class)->create([
'user_id' => $user->id,
'customer_id' => $contract->customer_id,
'contract_id' => $contract->id,
'extension_id' => $extension->id,
'total' => 16,
'date' => '2018-01-01',
]);
$entry = factory(TimesheetEntry::class)->create([
'date' => '2018-01-14',
'value' => 8,
'user_id' => $user->id,
'tariff_id' => 1,
'contract_id' => $contract->id,
'customer_id' => $contract->customer_id,
'extension_id' => $extension->id,
'timesheet_id' => $timesheet->id,
'invoiced' => false,
]);
$entry1 = factory(TimesheetEntry::class)->create([
'date' => '2018-01-15',
'value' => 8,
'user_id' => $user->id,
'tariff_id' => 1,
'contract_id' => $contract->id,
'customer_id' => $contract->customer_id,
'extension_id' => $extension->id,
'timesheet_id' => $timesheet->id,
'invoiced' => false,
]);
$entry2 = factory(TimesheetEntry::class)->create([
'date' => '2018-01-16',
'value' => 4,
'user_id' => $user->id,
'tariff_id' => 1,
'contract_id' => $contract->id,
'customer_id' => $contract->customer_id,
'extension_id' => $extension2->id,
'timesheet_id' => $timesheet->id,
'invoiced' => false,
]);
$invoices = Invoice::fromTimesheet($timesheet);
$this->assertCount(2, $invoices);
$this->assertEquals(800, $invoices[0]->total);
$this->assertEquals(200, $invoices[1]->total);
$this->assertEquals($extension->id, $invoices[0]->extension_id);
$this->assertEquals($extension2->id, $invoices[1]->extension_id);
}
This test throws an error ErrorException: Trying to get property 'id' of non-object
1) TestsFeatureInvoiceTest::invoices_are_split_between_extensions
ErrorException: Trying to get property 'id' of non-object
/home/ilyas/script/clockwork/contract-module/src/Models/Contract.php:435
/home/ilyas/script/clockwork/timesheet-module/src/Models/Timesheet.php:293
/home/ilyas/script/clockwork/app/src/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1011
/home/ilyas/script/clockwork/app/src/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php:261
/home/ilyas/script/clockwork/timesheet-module/src/Models/Timesheet.php:303
/home/ilyas/script/clockwork/invoice-module/src/Models/Invoice.php:272
/home/ilyas/script/clockwork/app/src/tests/Feature/InvoiceTest.php:145
When I go to line 435 in Contract.php
I find the following line. Where $tariff->id
is according to the error not an object.
$override = $this->tariffs->where('id', $tariff->id)->first()->pivot->sp ?? null;
After a quick dd()
I find that this is an object, an instance of Tariff
called as Tariff::findOrFail($key);
this returns a valid eloquent object.
Now the fun part begins. If I get the id like $tariff['id']
it works. If I try something like $tariff->toArray()
I get the error Error: Call to a member function toArray() on null
.
$tariff = $tariff->toArray();
$override = $this->tariffs->where('id', $tariff['id'])->first()->pivot->sp ?? null;
If I dd()
$tariff before the toArray()
I get an Eloquent collection. If I dd()
after $override
I get the same collection as an array. If I remove the dd()
the error reappears... This is driving me crazy for hours.
I easily can fix that function by using tariff['id']
but I got to know what could be causing this issue.
laravel eloquent phpunit
laravel eloquent phpunit
asked Nov 20 '18 at 18:55
OdysseeOdyssee
661419
661419
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399722%2fweird-behavior-with-models-and-phpunit-in-laravel-trying-to-get-property-id%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399722%2fweird-behavior-with-models-and-phpunit-in-laravel-trying-to-get-property-id%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown