How to use DB in unit tests ?
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!
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
-
- Expert
- Posts: 274
- Joined: 27 Jun 2019, 16:19
How to use DB in unit tests ?
Hi)
I need to cover complicated job with unit test.
Here is my blank Test class:
<?php
namespace Aimeos\Controller\Jobs;
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:
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
Re: How to use DB in unit tests ?
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.
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, give us a star
If you like Aimeos, give us a star
Re: How to use DB in unit tests ?
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 ?
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.