Folder structure
/app/lib/Helper.php
/tests/HelperTest.php
/vendor/autoload.php
composer.json
{
"require-dev": {
"phpunit/phpunit": "*"
},
"autoload": {
"psr-4": {
"Datapark\\LPS\\": "app/"
}
},
"autoload-dev": {
"psr-4": {
"Datapark\\LPS\\Tests\\": "tests/"
}
},
}
Helper.php
<?php
namespace lib;
class Helper
{
public function array_get($array, $key, $default = null)
{
// code
}
}
HelperTest.php
<?php
use lib\Helper;
class HelperTest extends \PHPUnit_Framework_TestCase
{
public function test_array_get()
{
$helper = new Helper();
}
}
Command I run on the Server [Debian 8 / PHP7]
phpunit --bootstrap vendor/autoload.php tests
Error I get
1) HelperTest::test_array_get
Error: Class 'lib\Helper' not found
lib\Helper is loaded via namespace and my IDE (PhpStorm) also recognize it. Struggling around already a few hours and don't get it to work.
Your autoload configuration says:
"Datapark\\LPS\\": "app/"
Which means something like:
classes in
app
directory haveDatapark\LPS\
namespace prefix.
So as an example class in file app/lib/Helper.php
should have namespace Datapark\LPS\lib
. Therefore you need to change namespace declaration for Helper
class to:
namespace Datapark\LPS\lib;
And there is similar issue for your test folder.
I noticed that when I run:
$ vendor/bin/phpunit tests
then my tests started to work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With