1

guys. I have two Unit Tests Classes in CakePhp:

<?php
App::import('Vendor/Laravel', 'Api');
class UserApi extends LaravelApi {

And

<?php
App::uses('Api', 'Vendor/Laravel');
App::import('Vendor/Laravel', 'Api');
App::import('Component', 'Session');
class LaravelApiTest extends CakeTestCase {

When I run my unit test (I will call it AllApp.php) that should run these two tests (and some other too), I get the message

Fatal Error Error: Cannot declare class LaravelApi, because the name is already in use in [/var/www/html/app/Vendor/Laravel/Api.php, line 9]

I have tried by removing one of the two App:import('Vendor/Laravel', 'Api'); and "AllApp.php" runs fine, but of course, the individual test I modified fails because:

Error: Class 'LaravelApi' not found

I think it is because these two files are not run at the same time when I run the individual test, but when I run them with "AllApp" the two files are loaded and the class gets "required" two times (even thought I think App::import is similar to "require_once"). Can anyone help me with this?

LuisE
  • 553
  • 3
  • 18
  • Does this answer your question? [Using App::uses (instead of App::import) in a CakePHP 2.1 Plugin](https://stackoverflow.com/questions/8994514/using-appuses-instead-of-appimport-in-a-cakephp-2-1-plugin) – ficuscr Sep 22 '20 at 21:46
  • I have already checked that, no it doesn't. but Thank you very much. – LuisE Sep 22 '20 at 21:48
  • Cake loves to wrap things that don't need it. They still have `up()` that just calls `strtoupper`? Sure does sound like a `include` over `include_once`. Not clear how the autoload works here or if there even is autoloading. If not then even the order of those import/uses becomes important. – ficuscr Sep 22 '20 at 21:54
  • meh, I guess I will go with a good old "require_once" then, it seems to work. Thank you guys anyway. – LuisE Sep 22 '20 at 21:55
  • 1
    `if(!class_exists('LaravelApi')) { App::import(...); }` – Greg Schmidt Sep 22 '20 at 22:44
  • Thank you very much Greg, but require_once would not require the "if" line, so I would rather that. – LuisE Sep 22 '20 at 23:17

0 Answers0