Can you repeat what you are asking since I'm confused. Also why do you have factory created $order and mocked version of it assigned to the same variable?
Jan 26, 2020
11
Level 13
Test Model From Job Class PHPUnit Mockery
How do I mock the markshipped method on the ebayOrders model? I am testing that the job handle() method works correctly. My current test the markshipped method still runs.
Job -
class ProcessOrderJob implements ShouldQueue
{
public function handle()
{
$DB = '';
$Controller = '';
if (strcmp($this->username, 'user1') === 0) {
$DB = '\App\user1orders';
} elseif (strcmp($this->username, 'user2') === 0) {
$DB = '\App\user2orders';
}
while ($DB::all()->isNotEmpty()) {
$order = $DB::first();
$multiple_orders = $order->getOtherOrders();
$processboxhelper = new ProcessBoxHelper();
if ($multiple_orders->count() > 1) {
$multiple_orders->push($order);
if ($processboxhelper->process($multiple_orders)) {
$multiple_orders->map(function ($single_order) {
$response = $single_order->markShipped();
// $response = ['success' => true, 'ShippedTime' => now()];
if ($response['success']) {
$single_order->ShippedTime = $response['ShippedTime'];
$single_order->markComplete();
} else {
$single_order->markError();
}
});
} else {
$multiple_orders->map(function ($single_order) {
$single_order->markError();
});
}
}
if ($processboxhelper->process($order)) {
$response = $order->markShipped();
// $response = ['success' => true, 'ShippedTime' => now()];
if ($response['success']) {
$order->ShippedTime = $response['ShippedTime'];
$order->markComplete();
} else {
$order->markError();
}
} else {
$order->markError();
}
}
}
}
My test is currently -
class ProcessOrderJobTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function job_processes_order_successfully()
{
$order = factory(ebayOrders::class)->create();
$order = $this->createMock(ebayOrders::class);
$order->method('markShipped')
->willReturn(['success' => true, 'ShippedTime' => now()]);
$job = new ProcessOrderJob(1, 'user1');
$job->handle();
$this->assertCount(1, CompletedOrder::all());
$this->assertDatabaseHas('completed_orders', $order->toArray());
}
}
Please or to participate in this conversation.