CakePHP 3.0 vendor class not found - cakephp

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();.

Related

Unit testing getting error class could not be found

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

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.

Undefined Property: Security::$table [duplicate]

Hey I have coded CakePHP for a number of things but never ran into this problem before surprisingly. Also I have thoroughly searched the net and CakePHP docs and have not found an answer to my question. My question is, I have a table for my model that should be named Class, obviously I cannot use that name though since it's a reserved PHP keyword. What options do I have to be able to refer to this model appropriately.
So far I have;
Renamed my class model file to player_class.php
Renamed my class model class to PlayerClass
Changed var $name to 'PlayerClass'
Added to my class model class; var $useTable = 'classes';
Renamed my class controller to player_classes_controller.php
Renamed my class controller class to PlayerClassesController
Changed var $name to 'PlayerClasses'
While this does work, is this what has to be done or are to other options to be able to refer to it as Class still, like can I do any sort of mangling like _Class?
I once tested all CakePHP class names for Cake 1.2 if they can be used as Model names, here are the results:
NOT possible is:
app
appcontroller
appmodel
behaviorcollection
cache
cacheengine
cakelog
cakesession
classregistry
component
configure
connectionmanager
controller
datasource
debugger
dispatcher
file
fileengine
folder
helper
inflector
model
modelbehavior
object
overloadable
overloadable2
router
security
sessioncomponent
set
string
validation
Possible is:
acl
aclbase
aclbehavior
aclcomponent
aclnode
aclshell
aco
acoaction
admin
ajaxhelper
apcengine
apishell
app_model
apphelper
aro
authcomponent
bake
baker
bakeshell
behavior
cachehelper
cake
cakeschema
cakesocket
consoleshell
containablebehavior
controllertask
cookiecomponent
dbacl
dbaclschema
dbconfigtask
dboadodb
dbodb2
dbofirebird
dbomssql
dbomysql
dbomysqlbase
dbomysqli
dboodbc
dbooracle
dbopostgres
dbosource
dbosqlite
dbosybase
element
emailcomponent
error
errorhandler
extracttask
flay
formhelper
htmlhelper
httpsocket
i18n
i18nmodel
i18nschema
i18nshell
iniacl
javascripthelper
jshelper
jshelperobject
l10n
layout
magicdb
magicfileresource
mediaview
memcacheengine
modeltask
multibyte
numberhelper
page
pagescontroller
paginatorhelper
permission
plugintask
projecttask
requesthandlercomponent
rsshelper
sanitize
scaffold
schema
schemashell
securitycomponent
sessionhelper
sessionsschema
shell
shelldispatcher
test
testsuiteshell
testtask
texthelper
themeview
timehelper
translate
translatebehavior
treebehavior
viewtask
xcacheengine
xml
xmlelement
xmlhelper
xmlmanager
xmlnode
xmltextnode
When i run into this sort of problem i usually do what you did, only i prefix the reserved word with "My" (so when i read the code it doesn't look like that class has anything to do with "Player"... for example, just the other day i wanted to model a "ACO" model.. but that already existed in cake (same scenario of reserved word) so i created a model called Myaco.
I think you should just name it Myclass.
Regarding the model name and controller name changes- i think you did good, i would do the same. Your only real option is to use the $useTable = 'classed'; to use your DB table.
If you use the underscore prefix, i believe cake will not be able to handle it (it will fail in the Inflector class).
Good luck
I can second that solution. I had the same problem and used a prefix that was the initials of the client. Ended up calling mine Dtclass. Unfortunately, it took me an hour or so to figure out what the problem was. One of those cases where the answer stares you in the face all the time till you finally recognize it.

cakephp3 Class DateTime not found

I'm trying to convert my cakephp plugin from 2.X to 3.
I have a little problem inside my component plugin at this line:
$dStart = new DateTime($now); //$now = date('Y-m-d H:i:s');
return me this error:
Error: Class 'CurrencyConverter\Controller\Component\DateTime' not found
Seems that it search DateTime inside my plugin directory.
How can I solve it?
Thanks
Try:
$dStart = new \DateTime($now);
because CakePHP 3.0 using namespaces and if you not add a root namespace it will be looking for class under current namespace.

solr: read stopword.txt in Custom Handler

I want to read stopword.txt in my custom handler. How to do that ? I know that this is used in Filtering and can be done from there. But I need to read that list in my Custom UpdateRequestProcessorFactory. Also can I read any other custom file created by me.
I was aware that limitation. I overlooked that you are using about update processor.
I looked into the code, here is an existing code you can use as example. SolrCoreAware is the interface you are after.
public class StatelessScriptUpdateProcessorFactory extends UpdateRequestProcessorFactory implements SolrCoreAware
#Override
public void inform(SolrCore core) {
resourceLoader = core.getResourceLoader();
}
Classes that implement org.apache.lucene.analysis.util.ResourceLoaderAware can read files under conf directory. However what it your use case anyway?
looks like xy problem

Resources