Class not found in path - php

So I have php project and when I'm running the tests the class TestCase isn't found and I can't figure out why. The error in question is this: Fatal error: Class 'project\Tests\Api\TestCase' not found in /some/path/here/test/project/Tests/Api/UsersTest.php on line 6
The below file is named UsersTest.php and TestCase is the class that can't be found.
<?php
namespace project\Tests\Api;
class UsersTest extends TestCase
{
// code here
}
The below file is TestCase.php which exists in the same folder as UsersTest.php
<?php
namespace project\Tests\Api;
abstract class TestCase extends \PHPUnit_Framework_TestCase
{
abstract protected function getApiClass();
protected function getApiMock()
{
// code here
}
}
Both of these files exists in a folder structure test/project/Tests/Api/. From my understanding UsersTest.php should be able to find the class because both of these files exist in the same namespace.
In test/project I have bootstrap.php file that looks like this.
<?php
function includeIfExists($file)
{
if (file_exists($file)) {
return include $file;
}
}
if ((!$loader = includeIfExists(__DIR__.'/../vendor/autoload.php')) && (!$loader = includeIfExists(__DIR__.'/../../../.composer/autoload.php'))) {
die('You must set up the project dependencies, run the following commands:'.PHP_EOL.
'curl -s http://getcomposer.org/installer | php'.PHP_EOL.
'php composer.phar install'.PHP_EOL);
}
$loader->add('project\Tests', __DIR__);
return $loader;

Related

Class 'App\TestService' not found - Laravel

Problem
I created service TestService, that I use in colntroller file TestController in function test().
When I called test(), I got an error:
local.ERROR: Class 'App\TestService' not found {"userId":1,"exception":"[object] (Error(code: 0): Class 'App\TestService' not found at /Backend/app/Http/Controllers/TestController.php:8)
Code
TestController.php:
<?php
namespace App\Http\Controllers;
use App\TestService;
class TestController extends Controller
{
public function test()
{
return response()->json(TestService::getTest());
}
}
TestService.php:
<?php
namespace App;
use App\TestService;
class TestService
{
public static function getTest()
{
return "test";
}
}
What I tried
I checked all the names and they are correct.
When I wrote in the colntroller file use App\TestService;
I had autocomplete, so the service and name are visible.
I used these commands to refresh the files: php artisan serve and php artisan clear-compiled.
But it still doesn't work.
You have not correctly defined Namespace.
The namespace must be a directory path where you have created a file.
TestService.php:
<?php
namespace App\Services\Tests;
class TestService
{
public static function getTest()
{
return "test";
}
}
TestController.php:
<?php
namespace App\Http\Controllers;
use App\Services\Tests\TestService;
class TestController extends Controller
{
public function test()
{
//Call service class like.
return response()->json(TestService::getTest());
}
}

Correct way to reference class\method in another file

I have class that I have set up like this:
use MVC\Model;
use \Firebase\JWT\JWT;
class ModelsAuth extends Model {
In my ModelsAuth class, I have this member function:
public function validateToken($token) {
}
I am using this to autoload my classes:
// autoload class
function autoload($class) {
// set file class
$file = SYSTEM . str_replace('\\', '/', $class) . '.php';
if (file_exists($file))
require_once $file;
else
throw new Exception(sprintf('Class { %s } Not Found!', $class));
}
// set autoload function
spl_autoload_register('autoload');
Is it possible to call this class and function from outside the class?
I have tried:
$authorization = new \MVC\Model\ModelsAuth();
$authorization->validateToken($token);
and
$authorization = MVC\Model\ModelsAuth::validateToken($token);
Both return a class not found error.
Any thoughts on what I might be doing wrong with this approach?
You have not declared a namespace for your class, so it's not clear that it will exist as MVC\Model\ModelsAuth and it's probably just going into the root namespace as \ModelsAuth. You probably want something like this:
namespace MVC\Model;
class ModelsAuth extends \MVC\Model {
...
}
Which can also be specified as:
namespace MVC\Model;
use MVC\Model;
class ModelsAuth extends Model {
...
}
I'm not fond of either of these options however, because then you've got a base class named \MVC\Model and a namespace named \MVC\Model, which is somewhat confusing. I'd recommend putting your base class as an abstract into the \MVC\Model namespace, in a file named MVC/Model/AbstractModel.php like this:
namespace MVC\Model;
abstract class AbstractModel {
...
}
And then your model in a file name MVC/Model/MyModel.php like this:
namespace MVC\Model;
class MyModel extends AbstractModel {
...
}
This way, all your models are in the same namespace and the same directory.
Also, if you use a PSR-4 autoloader then most of these sorts of issues will go away.

PHP 5.4 trying to implement namespaces in project

I have a php 5.4 application where I am trying to implement namespaces.
I have a models folder with a model Admin.php in it.
<?php
namespace models;
class Admin extends Model
{
public function getAllContent($lang)
{
return some stuff ...
}
}
I have an adminController.php where I do this:
<?php
use models\Admin;
class adminController extends Controller
{
function index()
{
$variable = Admin::getAllContent($this->lang);
}
}
I get this error
Fatal error: Class 'models\Admin' not found in /.../controllers/adminController.php on line 22
this is line 22 : $variable = Admin::getAllContent($this->lang);

Identical class name in different files phpunit conflict

When i test my application on Zend Framework 2 i have strange error. For example, i have 2 phpunit test files:
1:
namespace Test\Model;
class Test1Test extends \PHPUnit_Framework_TestCase {
public function test1test() {
$this->assertTrue((new \Austero\Model\Test1())->t1());
}
}
2:
namespace Test\Model;
class Test2Test extends \PHPUnit_Framework_TestCase {
public function test1test() {
$this->assertTrue((new \Austero\Model\Test2())->t2());
}
}
And 2 tested model files:
namespace App\Model;
class Test1 {
public function t1() {
return true;
}
}
And:
namespace App\Model;
use
\App\Controller\Test1; // THERE USE FILE FROM ANOTHER NAMESPACE WITH SAME NAME THAT Test1 FROM App\Model NAMESPACE
class Test2 {
public function t2() {
(new Test1())->t3();
return true;
}
}
Then i got:
PHP Fatal error: Cannot use App\Controller\Test1 as Test1 because the name is already in use in /home/user/projects/app/module/App/src/App/Model/Test2.php on line 5
How can i avoid this, without change used alias? Thanks for answer

Included class inside a PHPUnit file can't use Autoloader?

I have write a router.test.php file:
class RouterTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
require_once '../router.class.php';
}
public function testUnit()
{
$this->assertEquals(true, true);
}
}
... for simply test if my PHPUnit library is working correctly. But when I type phpunit router.test.php in my console I get this error message: Fatal error: Trait 'Core' not found in C:\xampp\htdocs\_Framework\ver_3\libraries\router\router.class.php on line 23
... my Router class is working correctly and my Autoloader automatically include trait (named Core) inside my Router class.
Where is the problem?
My Autoloader register method looks like:
public static function register()
{
return spl_autoload_register(
__CLASS__ . '::load'
);
}
... I use this method in my index.php file, should I write another autoloader for PHPUnits?
I have include necessary files in the setUp method, now the class looks like that:
<?php
use Framework\Libraries\Configuration\Configuration;
use Framework\Libraries\Autoload\Autoloader;
class RouterTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
require '../../core.trait.php';
require '../../configuration/configuration.class.php';
require '../../autoload/autoloader.class.php';
Configuration::set('files', [
require '../../../configuration/framework.configuration.php',
require '../../../configuration/autoloader.configuration.php',
require '../../../configuration/router.configuration.php',
]);
Autoloader::set('prefixes', ['Framework']);
Autoloader::set('extensions', ['.class.php', '.trait.php']);
Autoloader::register();
require_once '../router.class.php';
}
public function testUnit()
{
$this->assertEquals(true, true);
}
}
... an I get a new error message: E Time: 109 ms, Memory: 1.75Mb There was 1 error, but I can't get the error message. I think it's because Autoloader can't load files because of wrong path.
No it won't load index file automatically. So you need to include all your relevant class in your unit test class by adding include_once('AutoLoader.php'); in or another way is to create a bootstrap.php file & include all classes you need in all the PHPUnit Test class.
Reference.

Categories