Page 1 of 1

What is the best way to make an extension testable?

Posted: Sat Feb 16, 2019 2:57 pm
by FredQ
I think the phpBB team is doing a great job of moving the code standards to the right direction:
All the new stuff is now relying on Symfony Dependency Injection and that's great. Unfortunately there's still some legacy code in phpBB that you cannot avoid.

I find particularly difficult to unit test some parts of my extension, especially the part that make use of the global variables and/or the global functions.

For that purpose I had to create some wrapper classes like this to be able to test my code:

Code: Select all


class functions implements functions_interface
{
	public function confirm_box($check, $title = '', $hidden = '', $html_body = 'confirm_body.html', $u_action = '')
	{
		confirm_box($check, $title, $hidden, $html_body, $u_action);
	}
}

By using an interface I'm able to mock the confirm_box functionality inside a unit test. For the normal path, I'm making sure it's added as a dependency to the classes needing access to global functions.

It is something you guys came across as well? Have you found a better way to build a fully testable extension?

Or do you think that would be something useful to share?

Re: What is the best way to make an extension testable?

Posted: Mon Feb 18, 2019 10:51 am
by Senky
It's a wrong wrong way but this is how we do it:
https://github.com/phpbb-extensions/ad- ... r_test.php

Re: What is the best way to make an extension testable?

Posted: Tue Feb 19, 2019 1:02 pm
by FredQ
I see.
Well I did that for a few other tests as well but then I ran into issues where some functions were re-declared.

Typically you cannot run unit tests and functional tests in the same batch. I tried with the "separate process" option of phpunit but then it takes 3 hours to complete all my tests.

I'm not sure there's a better solution :roll: