Cakephp - Testing Components - cannot find component - unit-testing

I am writing a component test in cakephp
here is my code
<?php
class PermissionTestCase extends CakeTestCase {
var $fixtures = array('Org');
function testsetPermission() {
$this->PermissionComponentTest = new PermissionComponent(); <---- line 5
I get this error - Fatal error: Class 'PermissionComponent' not found in /Sites/php/cake/Demo_Code/perm/app/tests/cases/components/permission.test.php on line 5
Why is it looking for the component in tests/cases ?
Also I tried moving the component into this directory and it didnt work.
Thanks
Alex

You have to include the component manually with App::import():
App::import('Component', 'Permission');

Related

Symfony4 - Unable to find template

I'm a beginner in symfony 4, I create a symfony 4 project But I get this error after adding boostrap themes
Unable to find template "blog/home.html.twig" (looked into:
D:\Interactions\Symfony4\demo\vendor\symfony\twig-
bridge/Resources/views/Form).
This is my action code in the controller:
/**
* #Route("/", name="home")
*/
public function home()
{
return $this->render('blog/home.html.twig');
}
Any suggestions to fix that ?
This the content of My twig.yaml:
twig:
default_path: '%kernel.project_dir%/templat:es'
debug: '%kernel.debug%'
strict_variables: '%kernel.debug%'
form_themes: ['bootstrap_4_layout.html.twig']
Try fixing
default_path: '%kernel.project_dir%/templat:es'
with
default_path: '%kernel.project_dir%/templates'
and finally clear the cache

PHPUnit test in Zend Framework 3

I am trying to create a test case on the login action with 2 post parameter the code is as here
namespace UserTest\Controller;
use Application\Controller\LoginController;
use Application\Service\AuthenticationService;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\Parameters;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
class UserControllerTest extends AbstractHttpControllerTestCase
public function testIndexActionCanBeAccessed()
{
$p = new Parameters();
$p->set('username','foo');
$p->set('password','bar');
$this->getRequest()->setMethod('POST');
$this->getRequest()->setPost($p);
$this->dispatch('widget/login');
$this->assertModuleName('Application');
$this->assertControllerName(LoginController::class);
$this->assertControllerClass('LoginController');
//$this->assertMatchedRouteName('login');
}
and executing the test case from the command line the command is
./vendor/bin/phpunit
/var/www/myproject/application/module/Application/test/Controller/UserControllerTest.php
and the script throwing error that is
Fatal error: Declaration of Application\Service\AuthenticationService::authenticate() must be compatible with Zend\Authentication\AuthenticationService::authenticate(?Zend\Authentication\Adapter\AdapterInterface $adapter = NULL) in /var/www/myproject/application/module/Application/src/Application/Service/AuthenticationService.php on line 16
Please, help me to fix this issue and let me know what i am doing wrong in it

Controller Unit Test fails with SQLSTATE[42000] error

I want to unit test a controller action and have some problem toio excecute it.
The Error i got is the following:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'questionExists'
at line 1
The Method:
questionExists
is defined inside the Question Model.
My test function looks like this:
public function testView() {
$result = $this->testAction('/questions/questions/view/1', array('return' => 'vars'));
}
The Controller action i want to test looks like this:
public function view($id = null) {
if (!$this->Question->questionExists($id, 'id_virtual')) {
throw new NotFoundException(__('Invalid question'));
}
$options = array('conditions' => array('Question.id_virtual' => $id));
$this->set('question', $this->Question->find('first', $options));
}
So this is very confusing to me.
Can anybody point me to the right direction ?
Your model class is not found, CakePHP creates an instance on the fly for that table but this is not more than the basic Model class. When the code is then called it tries to call that method on an instance that is not your Question model in your app. You'll have to figure out what that happens.

ZF2 model unit test, class not found

Eh, this one makes my hair fall out...
I did some usefull stuff in zf1 and now I'm struggling to switch to zf2, and to do the thing right, I want to get stuff done TDD-style.
I've set up the Skeleton application, then made two additional modules, called "Weather" and "Airport". I than made a test case for WeatherController which works fine. Than I made a test case for models within Airport module and it fails with :
Fatal error: Class 'Airport\Model\Airport' not found in C:\xampp\htdocs...
, and the error is triggered here (AirportTableTest.php) :
<?php
namespace AirportTest\Model;
use Airport\Model\Airport;
use Airport\Model\AirportTable;
use PHPUnit_Framework_TestCase;
class AirportTableTest extends PHPUnit_Framework_TestCase {
public function testExample() {
$airport = new Airport(); // - this is not getting loaded and throws the fatal error :(
}
}
The code is based on the Album module example in ZF2 tutorial. The AirportTable model is supposed to interface a SQL table in the DB and the Airport model is written just like the Album model was written in the tutorial. The directory structure is (abbrevated) :
/module
/Airport
/src
/Airport
/Controller
/Model
AirportTable.php
Airport.php
/Application
/Weather
/public
/tests
/module
/Airport
/src
/Airport
/Controller
/Model
AirportTableTest.php
AirportTest.php
/Application
/Weather
bootstrap.php
phpunit.xml
/vendor
bootstrap.php from tests directory :
<?php
chdir(dirname(__DIR__));
error_reporting(E_ALL | E_STRICT);
include __DIR__.'/../init_autoloader.php';
The Airport.php with the class that is not being loaded :
<?php
namespace Airport\Model;
class Airport
{
public $icao;
public $lat;
public $lng;
public $metar;
public function exchangeArray($data){
$this->icao = (isset($data['id'])) ? $data['icao'] : null;
$this->lat = (isset($data['lat'])) ? $data['lat'] : null;
$this->lng = (isset($data['lng'])) ? $data['lng'] : null;
$this->metar = (isset($data['metar'])) ? $data['metar'] : null;
}
}
?>
The Module.php for Airport module :
<?php
namespace Airport;
use Airport\Model\Airport;
use Airport\Model\AirportTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
class Module
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getServiceConfig()
{
return array(
'factories' => array(
'Airport\Model\AirportTable' => function($sm) {
$tableGateway = $sm->get('AirportTableGateway');
$table = new AirportTable($tableGateway);
return $table;
},
'AirportTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Airport());
return new TableGateway('airport', $dbAdapter, null, $resultSetPrototype);
},
),
);
}
}
So I'm probably missing something pretty obvious, like autoloader things related perhaps ? So, uhm... help maybe (pretty please) ?
Sooo, I came up with a working solution, although I'm not quite sure whether its smart or completly retarded.
Based on PHPUnit with a Zend Framework 2 module I added the line
Zend\Mvc\Application::init(include '/../config/application.config.php');
to the bootstrap.php of the test suite, and now everything works as expected, however I have no idea whatsoever why it would work without this line for the "Weather" module and not for "Airport" module...
You may want to have a look at the way the ZF2 getting started tutorial lays out tests. I have completed the tutorial and committed the changes to my own fork of the ZF2 Skeleton Application source.
Basically, each module has it's own test suite, with a dedicated Bootstrap file with configuration and a phpunit.xml that will tell PHPUnit to load all this when you run your test (so long as you're in the tests directory when running phpunit). This helps keep the tests modular.

CakePHP fatal error: Class 'ErrorHandler' not found

I've generated testsuits via "cake bake testsuit" and used localhost/test.php for my app.
So, the is an error when I tried to run one of test (else tests are valid):
Fatal error: Class 'ErrorHandler' not found in Z:\home\prodvigator\www\cake\libs\object.php on line 201
This models and controllers are generated by scaffold and I don't think that an error is in this sources.
Using:
CakePHP 1.3
The latest SimpleTest
In my case, deleting all the files in the folder /app/tmp/cache/persistent solved the problem.
try checking the generated tests for an error that gets written at the top of the file.
sometimes i've been known to find something like this in both model and controller tests.
Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EDT/-4.0/DST' instead in /projectname/cake/console/templates/default/classes/test.ctp on line 22
In my case, the error was:
Fatal error: Uncaught Error: Class 'ErrorHandler' not found in C:\[path]\core\cake\libs\object.php on line 211
( ! ) Error: Class 'ErrorHandler' not found in C:\[path]\core\cake\libs\object.php on line 211
The error was happening to me when trying to visit http://localhost/user_accounts/index
I already had the view created at app\views\user_accounts\index.ctp with the following content:
<div>
Text from div
</div>
I had created the corresponding controller as well at app\controllers\user_accounts_controller.php:
<?php
class UserAccountsController extends AppController {
public function index() {
// Render the view in /views/user_accounts/index.ctp
$this->render();
}
}
?>
Since I was not associating a model to this controller, I was missing this: var $uses = array();. It would have saved me time if the error had been more explicit, something such as "You do not have a model associated to this controller".
The fix was:
<?php
class UserAccountsController extends AppController {
// Use this controller without a need for a corresponding Model file.
var $uses = array();
public function index() {
// Render the view in /views/user_accounts/index.ctp
$this->render();
}
}
?>