phpunit test with db not working.

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
andreask
Registered User
Posts: 752
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

phpunit test with db not working.

Post by andreask »

Hello,

I've setup a local env to do my dev and tests.
For some reason I cannot make phpunit work.

Here is what I have done.

Cloned the phpbb 3.3 github repo from here.
I've also did what is mentioned in 3.3.x/tests/RUNNING_TESTS.md

I have a config_test.php file located in my ./tests directory.

For some reason I think phpunit does not "see" that file. Here is the output of phpunit when I run it on an "empty" test with db.

Code: Select all

-> % ./phpBB/vendor/bin/phpunit -c phpBB/ext/andreask/ium/phpunit.xml.dist --verbose           
PHPUnit 9.5.20 #StandWithUkraine

Runtime:       PHP 8.1.6
Configuration: phpBB/ext/andreask/ium/phpunit.xml.dist
Warning:       Your XML configuration validates against a deprecated schema.
Suggestion:    Migrate your XML configuration using "--migrate-configuration"!

E                                                                   1 / 1 (100%)

Time: 00:00.009, Memory: 8.00 MB

There was 1 error:

1) Warning
TypeError: phpbb\db\doctrine\connection_factory::get_connection_from_params(): Argument #1 ($driver) must be of type string, null given, called in /var/www/html/phpbb/phpBB/phpbb/db/doctrine/connection_factory.php on line 49 and defined in /var/www/html/phpbb/phpBB/phpbb/db/doctrine/connection_factory.php:73
Stack trace:
#0 /var/www/html/phpbb/phpBB/phpbb/db/doctrine/connection_factory.php(49): phpbb\db\doctrine\connection_factory::get_connection_from_params()
#1 /var/www/html/phpbb/tests/test_framework/phpbb_database_test_case.php(101): phpbb\db\doctrine\connection_factory::get_connection()
#2 /var/www/html/phpbb/phpBB/vendor/phpunit/phpunit/src/Framework/TestSuite.php(621): phpbb_database_test_case::setUpBeforeClass()
#3 /var/www/html/phpbb/phpBB/vendor/phpunit/phpunit/src/Framework/TestSuite.php(677): PHPUnit\Framework\TestSuite->run()
#4 /var/www/html/phpbb/phpBB/vendor/phpunit/phpunit/src/Framework/TestSuite.php(677): PHPUnit\Framework\TestSuite->run()
#5 /var/www/html/phpbb/phpBB/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(673): PHPUnit\Framework\TestSuite->run()
#6 /var/www/html/phpbb/phpBB/vendor/phpunit/phpunit/src/TextUI/Command.php(143): PHPUnit\TextUI\TestRunner->run()
#7 /var/www/html/phpbb/phpBB/vendor/phpunit/phpunit/src/TextUI/Command.php(96): PHPUnit\TextUI\Command->run()
#8 /var/www/html/phpbb/phpBB/vendor/phpunit/phpunit/phpunit(98): PHPUnit\TextUI\Command::main()
#9 /var/www/html/phpbb/phpBB/vendor/bin/phpunit(120): include('...')
#10 {main}
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
Here is my test_config.php

Code: Select all

<?php
// This file should be located as: tests/test_config.php
// So it will be placed after the RUNNING_TESTS.md file.
$dbms = 'phpbb\\db\\driver\\mysqli';							# Change this when needed
$dbhost = 'localhost';
$dbport = '';
$dbname = 'phpbb_tests';									# Change this
$dbuser = 'user';										# Change this
$dbpasswd = 'password';										# Change this
$table_prefix = 'phpbb_';
$phpbb_adm_relative_path = 'adm/';
$acm_type = 'phpbb\\cache\\driver\\file';

$phpbb_functional_url = 'http://test.devian/';				# Change this when needed
And here is my test...

Code: Select all

<?php

namespace andreask\ium\tests\users;

class get_users_test extends \phpbb_database_test_case
{

    protected $config;
    protected $db;
    protected $user;
    protected $user_load;
    protected $log;
    protected $top_topic;
    protected $ignore_us;
    protected $request;
    protected $rout_help;

    public function setUp(): void
    {
        // parent::setUp();

        // $this->config   = $this->getMockBuilder('\phpbb\config\config')->disableOriginalConstructor()->getMock();
        // $this->db       = $this->test_case->getMockBuilder('phpbb\\db\\driver\\driver')
        //     ->disableOriginalConstructor()
        //     ->disableOriginalClone()
        //     ->disableArgumentCloning()
        //     ->disallowMockingUnknownTypes()
        //     ->getMock();
            
        // $this->getMockBuilder('\phpbb\db\driver\mysqli')->disableOriginalConstructor()->getMock();
        // $this->user     = $this->getMockBuilder('\phpbb\user')->disableOriginalConstructor()->getMock();
        // $this->user_load= $this->getMockBuilder('\phpbb\user_loader')->disableOriginalConstructor()->getMock();
        // $this->log      = $this->getMockBuilder('\phpbb\log\log')->disableOriginalConstructor()->getMock();
        // $this->top_topic= $this->getMockBuilder('\andreak\ium\classes\top_topics')->disableOriginalConstructor()->getMock();
        // $this->ignore_us= $this->getMockBuilder('\andreask\ium\classes\ignore_user')->disableOriginalConstructor()->getMock();
        // $this->request  = $this->getMockBuilder('\phpbb\request\request')->disableOriginalConstructor()->getMock();
        // $this->rout_help= $this->getMockBuilder('\phpbb\routing\helper')->disableOriginalConstructor()->getMock();

        // $this->reminder = new \andreask\ium\classes\reminder(
        //     $this->config,
        //     $this->db,
        //     $this->user,
        //     $this->user_load,
        //     $this->log,
        //     $this->top_topic,
        //     $this->ignore_us,
        //     $this->request,
        //     $this->rout_help,
        //     $this->table_prefix,
        //     $this->phpbb_root_path,
        //     $this->php_ext);
    }

    public function getDataSet()
    {
        return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/phpbb_users.xml');
    }
}
Most of the code in the test is commented out, no matter, the result is the same always.

What have I done wrong?
FYI

Code: Select all

php -v
PHP 8.1.6 (cli) (built: May 17 2022 16:48:41) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.6, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.6, Copyright (c), by Zend Technologies
Thank you in advance!
Here is what I am working on right now...
Inactive User Manager for phpBB
Give it a try...
If you would like to buy me a bier ;) for my work I will drink it on a hot summer day and thank you!!!
andreask
Registered User
Posts: 752
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

Re: phpunit test with db not working.

Post by andreask »

Sooo....

I tried to do some digging. And this is what I have found out.
The bootstrap.php file is loading...
does some stuff and then of course at some point it has to do it's magic in /var/www/html/phpbb/phpBB/phpbb/db/doctrine/connection_factory.php...

Code: Select all

public static function get_connection(config_php_file $config): Connection
 40         {
 41                 $driver = $config->get('dbms');
 42                 $host = $config->get('dbhost');
 43                 $user = $config->get('dbuser');
 44                 $pass = $config->get('dbpasswd');
 45                 $name = $config->get('dbname');
 46                 $port = $config->get('dbport');
 47 
 48                 return self::get_connection_from_params(
 49                         $driver,
 50                         $host,
 51                         $user,
 52                         $pass,
 53                         $name,
 54                         $port
 55                 );
 56         }
There, it uses the config_php_file \phpbb\config_php_file.
Which in its __constructor it requires $phpbb_root_path and $php_ext.
But they are not given through connection_factory.php as you see above...
And they are probably out of scope of the class config_php_file.
Thus it does not load any config_test.php file.

So as a "smart boy" as I consider myself I add those values...
I check that they work (and they do).
But when I run the test I get the exact same message....
I edit again the file...

In the get() function of php_config_file there is a call to another function load_config_file().
So I add a "debug" message in there....

Code: Select all

protected function load_config_file()
        {
                var_dump($this);
                if (!$this->config_loaded && file_exists($this->config_file))
                {
                        $defined_vars = null; // Define variable before call get_defined_vars
                        $defined_vars = get_defined_vars();
                        require($this->config_file);
                        $this->config_data = array_diff_key(get_defined_vars(), $defined_vars);

                        $this->config_loaded = true;
                }
        }
But alas! nothing happens!

Please help I'm not that advanced in php and this really has troubled me for quite some time now!
Here is what I am working on right now...
Inactive User Manager for phpBB
Give it a try...
If you would like to buy me a bier ;) for my work I will drink it on a hot summer day and thank you!!!
rxu
Extensions Development Team
Posts: 3711
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation
Contact:

Re: phpunit test with db not working.

Post by rxu »

This may sound like a stupid advice, but anyway.
andreask wrote: Thu Jun 16, 2022 12:17 pm I have a config_test.php file located in my ./tests directory.
andreask wrote: Thu Jun 16, 2022 12:17 pm Here is my test_config.php
andreask wrote: Sat Jun 25, 2022 7:57 pm Thus it does not load any config_test.php file.
What is the actual file name you've created? It should be exactly test_config.php. Also ensure that there're no extra spaces in the file name (at the start/end/in-between).
andreask
Registered User
Posts: 752
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

Re: phpunit test with db not working.

Post by andreask »

Hi, thanks again for responding...
Anything is better than nothing!

The filename was config_test.php and there are no spaces.
I renamed the file to test_config.php still nothing...
I now have 2 files config_test.php and test_config.php, I tried with both files existing still nope!

Code: Select all

-> % ../../../vendor/bin/phpunit                   
PHPUnit 9.5.20 #StandWithUkraine

Runtime:       PHP 8.1.6
Configuration: /var/www/html/phpbb/phpBB/ext/andreask/ium/phpunit.xml.dist

E                                                                   1 / 1 (100%)

Time: 00:00.007, Memory: 8.00 MB

There was 1 error:

1) Warning
TypeError: phpbb\db\doctrine\connection_factory::get_connection_from_params(): Argument #1 ($driver) must be of type string, null given, called in /var/www/html/phpbb/phpBB/phpbb/db/doctrine/connection_factory.php on line 49 and defined in /var/www/html/phpbb/phpBB/phpbb/db/doctrine/connection_factory.php:73
Stack trace:
#0 /var/www/html/phpbb/phpBB/phpbb/db/doctrine/connection_factory.php(49): phpbb\db\doctrine\connection_factory::get_connection_from_params()
#1 /var/www/html/phpbb/tests/test_framework/phpbb_database_test_case.php(101): phpbb\db\doctrine\connection_factory::get_connection()
#2 /var/www/html/phpbb/phpBB/vendor/phpunit/phpunit/src/Framework/TestSuite.php(621): phpbb_database_test_case::setUpBeforeClass()
#3 /var/www/html/phpbb/phpBB/vendor/phpunit/phpunit/src/Framework/TestSuite.php(677): PHPUnit\Framework\TestSuite->run()
#4 /var/www/html/phpbb/phpBB/vendor/phpunit/phpunit/src/Framework/TestSuite.php(677): PHPUnit\Framework\TestSuite->run()
#5 /var/www/html/phpbb/phpBB/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(673): PHPUnit\Framework\TestSuite->run()
#6 /var/www/html/phpbb/phpBB/vendor/phpunit/phpunit/src/TextUI/Command.php(143): PHPUnit\TextUI\TestRunner->run()
#7 /var/www/html/phpbb/phpBB/vendor/phpunit/phpunit/src/TextUI/Command.php(96): PHPUnit\TextUI\Command->run()
#8 /var/www/html/phpbb/phpBB/vendor/phpunit/phpunit/phpunit(98): PHPUnit\TextUI\Command::main()
#9 /var/www/html/phpbb/phpBB/vendor/bin/phpunit(120): include('...')
#10 {main}
ERRORS!
Tests: 1, Assertions: 0, Errors: 1
Here is what I am working on right now...
Inactive User Manager for phpBB
Give it a try...
If you would like to buy me a bier ;) for my work I will drink it on a hot summer day and thank you!!!
Post Reply

Return to “Extension Writers Discussion”