Unit testing getting error class could not be found - cakephp

I have a class for dynamoDB in location
src/Dynamo/shop.php
My class looks like below
<?php
namespace App\Dynamo;
class Shop
{
-----
}
?>
I am trying to implement unit test for this class, So I have created a folder class dynamo in below location.
app/cake/tests/TestCase/Dynamo
In Dynamo folder I have created a class with file name ShopTest.php
For create a unit testing I have written this class like below
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Dynamo;
use App\Dynamo\Shop;
use Cake\TestSuite\TestCase;
class ShopTest{
public function setUp()
{
$this->shop = new Shop;
}
public function testconnectDynamoDB()
{
debug($this->shop->connectDynamoDB());
$this->assertNotEmpty($this->shop->connectDynamoDB());
}
}
Now after run phpunit command
vendor/bin/phpunit tests/TestCase/Dynamo/ShopTest.php
I am getting
Class 'App\Test\TestCase\Dynamo\ShopTest' could not be found in '/var/www/html/tests/TestCase/Dynamo/ShopTest.php'.
Class is present in this location , Why I am getting Class could not be found ?
root#0ceda1df4444:/var/www/html# cd /var/www/html/tests/TestCase/Dynamo/
root#0ceda1df4444:/var/www/html/tests/TestCase/Dynamo# ls
ShopTest.php
root#0ceda1df4444:/var/www/html/tests/TestCase/Dynamo# cat ShopTest.php
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Dynamo;
use App\Dynamo\Shop;
use Cake\TestSuite\TestCase;
class ShopTest{
Also I am trying to run all test case by below command , I am getting a warning.
root#0ceda1df4444:/var/www/html# vendor/bin/phpunit
PHPUnit 9.5.9 by Sebastian Bergmann and contributors.
Warning: Your XML configuration validates against a deprecated schema.
Suggestion: Migrate your XML configuration using "--migrate-configuration"!
No tests executed!

There is no test class
The error message there is a bit confusing, but the crux is this is not a test class:
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Dynamo;
use App\Dynamo\Shop;
use Cake\TestSuite\TestCase;
class ShopTest{ # <--
It is just a class (which coincidentally has the word Test in its name).
Compare to an example from the documentation:
namespace App\Test\TestCase\View\Helper;
use App\View\Helper\ProgressHelper;
use Cake\TestSuite\TestCase;
use Cake\View\View;
class ProgressHelperTest extends TestCase # <--
{
To be detected as a test the class must extend TestClass - therefore to correct this:
...
use App\Dynamo\Shop;
use Cake\TestSuite\TestCase;
class ShopTest extends TestCase # <--
{
With that change the test class will load and some easier-to-solve problems will become apparent:
$ vendor/bin/phpunit tests/TestCase/Dynamo/ShopTest.php
PHP Fatal error: Declaration of ShopTest::setUp() must be compatible with Cake\TestSuite\TestCase::setUp(): void in ~/repos/cakephp/app/tests/TestCase/Dynamo/ShopTest.php on line 11
Fatal error: Declaration of ShopTest::setUp() must be compatible with Cake\TestSuite\TestCase::setUp(): void in ~/repos/cakephp/app/tests/TestCase/Dynamo/ShopTest.php on line 11

Related

Facing java.lang.IllegalAccessError for a base abstract class when trying to inject its implementation in wildfly 10

We are receiving the following exceptions in one of our code :
java.lang.IllegalAccessError: tried to access class base.BaseMessage from class message.beans.TerminalPowerCommandProducer$Proxy$_$$_WeldSubclass
Our class structure is as follows :
The Base Message creator class with default message properties
package messages.base
//... required imports
abstract class BaseMessage{ //some protected variables and methods }
the intermediate message class with extension to BaseMessage some additional properties for specific message types
package messages.base
//... required imports
public abstract class PowerMessage extends BaseMessage {//some more protected variables and Logger(using #Inject) and methods}
The actual implementation of the above abstract classes
package messages.beans
//... required imports
#Named
public class TerminalCommandMessage extends PowerMessage {// some more variables with injections and methods with abstract method implementation}
This class is now injected in some other classes :
package messages.beans
#Named
public class TerminalPowerCommandProducer {
#Inject
TerminalCommandMessage commandMessage
//some other code
}
We are receiving exception as reported above.
We are using WildFly version 10.1.0 Final with jdk 8
Is there an issue with the way we have consumed it?
Because if we mark the BaseMessage class as public it all works fine.

CakePHP 3 Error on using SplFileInfo class

i'm getting the next error:
Error: Class 'App\JL\SplFileInfo' not found while trying to create a SplFileInfo class like this: $i = new SplFileInfo($P['Carpeta'].DS.$v);
i tested this with same results: $i = new SplFileInfo('');
this is in a cutom class called JL in namespace App\JL and calling it like use App\JL\JL;
The function is declared like this: public static function LeerArchivos(&$P = array()) {
i'm working in an element to be shown on a template...
¿Do i have to activate somethig? or ¿What am i doing wrong?
The problem is that you don't know how namespaces work. You should fix that gap in your knowledge first: http://php.net/manual/en/language.namespaces.php
You need to "import" the class from the "root" namespace \ into your current namespace \App\JL\JL. This us done via the use keyword. See the documentation for it.

Cakephp throws undefined method but the method exists

i created custom classes in the plugin. This class contains several methods. Unfortunately sometimes crashes on Exception
Error: Fatal Error (1): Call to undefined method DateTools::getUserDateFromSQL() in ...
but this method is in the class :(. Unfortunately an error occurs infrequently and can not even repeat it.
My class in plugin Tools (folder Lib)
class DateTools {
public function getUserDateFromSQL($value) {
if(empty($value)) return $value;
return Date("d.m.Y H:i", $this->getDateFromSQLDate($value));
}
...
...
...
The place where source crash.
App::import('DateTools', 'Tools.Lib');
class SomeController extends SomeParentAppController {
public function someMethod($arraysDate) {
$dateTools = new DateTools();
$result[$key] = $dateTools->getUserDateFromSQL($value);
...
...
...
Thanks for help
The syntax used to include the class is incorrect
You could use App::uses instead of App::import
App::uses('DateTools', 'Tools.Lib');
Syntax for App::import()
Syntax for App::uses()

CakePHP 3.0 vendor class not found

I'm adding an external class to a cake 3.0 app by putting it to /vendor/name folder and requiring it from a component like this:
require_once( $_SERVER['DOCUMENT_ROOT'].'/project/vendor/external/testClass.php');
But when I try to getInstance(); of a class - I get an error
Class 'App\Controller\Component\Test_Class' not found
I am calling this from a component (thus the \Controller\Component).
What is it that i'm doing wrong?
CakePHP 3.0 uses namespaces. So use proper namespace for your vendor class or if it's not using namespaces prefix the class name with backslash when using it.
E.g. $object = new \Test_Class();.

CakePHP lazy loading fails with static access to class constants

In a CakePHP 2.2 app, I'm using class constants in a Model for some internal configuration. The following issue came up.
Short version:
Cake's lazy class loading will not be triggered by a static call to the Model class.
If the first access to a Model in a Controller is
MyModel::SOME_CONST // fails
the class will be unknown. If any instance of the class is used before, it's fine:
$this->MyModel->something();
MyModel::SOME_CONST // works
Not knowing about the details of the lazy loading implementation:
Question: Is this something that is impossible to fix? If so, why? How do I then best work around it in my App myself (wrap consts in a function)? Or is there a chance to improve the lazy loading so that it works with static access, too?
Long version with code:
In order to test the different cases, I made a small test App with 1 Model and 1 Controller:
Model/Post.php:
<?php
class Post extends AppModel {
public $useTable = false; // Don't bother with a DB
const FOO = "foo";
public $bar = "bar";
}
Controller/PostsController.php:
<?php
class PostsController extends AppController {
public function constant() {
debug(Post::FOO);
}
public function variable() {
debug($this->Post->bar);
}
public function variableFirst() {
debug($this->Post->bar);
debug(Post::FOO);
}
}
Accessing the three controller actions through the browser, the different cases can now be tested.
1) accessing the Model constant (at /posts/constant):
Error: Class 'AppModel' not found
2) accessing the Model variable (at /posts/variable):
'bar'
3) accessing the Model constant AFTER a variable (at /posts/variable):
'bar'
'foo'
lazyloading works with normal class calls as well as static calls IF you correctly approach it.
Correctly means, that you always have to App::uses() all used classes at the top of your file
for AppModel in a model file:
App::uses('AppModel', 'Model');
class Post extends AppModel {}
see the core files for details.

Resources