How to use DB in unit tests ?

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

How to use DB in unit tests ?

Post by MikaelNazarenko » 25 May 2021, 12:57

Hi)

I need to cover complicated job with unit test.

Here is my blank Test class:

<?php

namespace Aimeos\Controller\Jobs;

Code: Select all

class OrderCommissionTest extends \PHPUnit\Framework\TestCase
{
	private $object;


	protected function setUp()
	{
	    
	}

	public function testRun()
	{
        $this->assertTrue(true);
	}
}

There must be involved a lot of data: users, orders, order_products, base_orders etc.. I think to create the mock objects is bad idea. My idea is to fill the extra DB specially for tests and retrieve data from there. But how can I set up the extra DB for tests and use DB connection to retrieve objects in tests ?

Thanks a lot!

Packages versions:

Code: Select all

aimeos/ai-admin-jqadm                 2019.10.6  Aimeos ai-admin-jqadm extension
aimeos/ai-admin-jsonadm               2019.10.2  Aimeos ai-admin-jsonadm extension
aimeos/ai-client-html                 2019.10.10 Aimeos ai-client-html extension
aimeos/ai-client-jsonapi              2019.10.2  Aimeos JSON API extension
aimeos/ai-controller-frontend         2019.10.3  Aimeos ai-controller-frontend extension
aimeos/ai-controller-jobs             2019.10.4  Aimeos ai-controller-jobs extension
aimeos/ai-gettext                     2019.10.1  Aimeos Gettext extension
aimeos/ai-laravel                     2019.10.2  Laravel adapter for Aimeos web shops and e-commerce solutions
aimeos/ai-payments                    2019.10.1  Payment extension for Aimeos web shops and e-commerce solutions
aimeos/ai-swiftmailer                 2019.10.1  SwiftMailer adapter for Aimeos web shops and e-commerce solutions
aimeos/aimeos-core                    2019.10.7  Full-featured e-commerce components for high performance online shops
aimeos/aimeos-laravel                 2019.10.1  Professional, full-featured and high performance Laravel e-commerce package for online shops and complex B2B projects

User avatar
aimeos
Administrator
Posts: 7866
Joined: 01 Jan 1970, 00:00

Re: How to use DB in unit tests ?

Post by aimeos » 29 May 2021, 10:36

You can write your own setup task that adds additional test data to the database like these ones do:
https://github.com/aimeos/aimeos-core/t ... p/unittest

Beware: Chances may be high that adding additional data to existing data domains will break the Aimeos core tests.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Brendon

Re: How to use DB in unit tests ?

Post by Brendon » 12 Aug 2021, 03:50

Given that I'm fairly new to server side testing, I've read that you usually want to mock the db for unit tests, and my reaction is: why in the world would I ever want to do that ? Building a query for the db that actually works is a huge (if not the MAIN) part of the server side (especially in node, since it's not calculation heavy), and I make use of it in pretty much 95% of the requests probably.

So why ? That would make tests pointless as they would just be testing that my mock responds well to queries (which doesn't make sense). What am I missing ?
Last edited by Brendon on 13 Aug 2021, 04:46, edited 1 time in total.

Post Reply