I am trying to run simple test and insert two record in db via fixture dataFile.
What I get is :
[ReflectionException] Class C:\xampp\htdocs\codeception\frontend\tests/_data\user.php does not exist
The file is obviously there. My UserTest.php looks like this:
<?php
class UserTest extends \Codeception\Test\Unit
{
public function _fixtures()
{
return [
'class' => \frontend\tests\fixtures\UserFixture::className(),
'dataFile' => codecept_data_dir() . 'user.php'
];
}
public function testValidation()
{
$user = new \common\models\User();
$user->setUsername(null);
$this->assertFalse($user->validate(['username']));
$user->setUsername('aaaaaaaaaaaaaaaaaaaaaaaaaa');
$this->assertFalse($user->validate(['username']));
$user->setUsername('toma');
$this->assertTrue($user->validate(['username']));
}
public function testIfUserExist()
{
$this->tester->seeRecord('user', ['name' => 'Toma']);
}
}
I saw the linux like forward slash in the error but don't know how to change it. Not sure if this is the problem because I had the same path with some images and it was fine then. What can cause this ? Thank you!
You're using seeRecord() in a wrong way. First argument needs to by class name, including namespace, so you should use it like this:
$this->tester->seeRecord(\frontend\models\User::class, ['name' => 'Toma']);
Related
Any one know how to send error messages to database in laravel which generate from app/exceptions/handler.php ?
I need to send what error massages generated in report() method to database.
If you are interested doing this manually, you can do something as following.
Step 1 -
Create a model to store errors that has a DB structure as following.
class Error extends Model
{
protected $fillable = ['user_id' , 'code' , 'file' , 'line' , 'message' , 'trace' ];
}
Step 2
Locate the App/Exceptions/Handler.php file, include Auth, and the Error model you created. and replace the report function with the following code.
public function report(Exception $exception) {
// Checks if a user has logged in to the system, so the error will be recorded with the user id
$userId = 0;
if (Auth::user()) {
$userId = Auth::user()->id;
}
$data = array(
'user_id' => $userId,
'code' => $exception->getCode(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'message' => $exception->getMessage(),
'trace' => $exception->getTraceAsString(),
);
Error::create($data);
parent::report($exception);
}
(I am demonstrating this using laravel 5.6)
Because Laravel uses Monolog for handling logging it seems that writing Monolog Handler would be the cleanest way.
I was able to find something that exists already, please have a look at monolog-mysql package. I did not use it, so I don't know whether it works and if it works well, but it's definitely good starting point.
I'm using YII2 2.0.10, basic. It seems that it don't use yii-codeception module anymore. So documentation from this page is not actual:
http://www.yiiframework.com/doc-2.0/guide-test-fixtures.html
My problem is that I can't load fixture to my test.
I'm using such way:
tests\unit\models\UserTest.php
<?php
namespace tests\models;
use app\models\User;
use tests\fixtures\UserFixture;
class UserTest extends \Codeception\Test\Unit
{
public function testFindUserById()
{
// load fixtures
$this->tester->haveFixtures([
'user' => [
'class' => UserFixture::className(),
// fixture data located in tests/_data/user.php
'dataFile' => codecept_data_dir() . 'user.php'
]
]);
// get first user from fixtures
$this->tester->grabFixture('user', 0);
...
tests\fixtures\UserFixture.php
namespace tests\fixtures;
use yii\test\ActiveFixture;
class UserFixture extends ActiveFixture
{
public $modelClass = 'dektrium\user\models\User';
}
And have some data returned from tests\fixtures\data\user.php
After vendor/codeception/base/codecept run I get
2) UserTest: Find user by id
Test tests/unit/models/UserTest.php:testFindUserById
[Error] Class 'tests\_fixtures\UserFixture' not found
#1 tests\models\UserTest->testFindUserById
What I'm doing wrong?
I spent half day to resolve this issue with no success.
I'm doing a setup in EC2, centos 6/64 bit. LAMP installed. On another hosting, my zf2 solution it work fine, so I've searched issue in php modules installed also (list at the end).
This error happen when zf2 try to get an instance of my custom service, also with wasabi mail.
\zend\config\application.config.php
'config_glob_paths' => array(
'./config/autoload/{,*.}{global,local}.php',
),
\zend\config\autoload\global.php
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
'Zend\CustomLogger' => function ($sm) {
$auth = $sm->get('zfcuser_auth_service');
$customLogger = new \Application\Service\CustomLogger(
$sm->get('Request'),
$sm->get('ZendLog'),
new \Zend\Session\SessionManager(),
$auth->getIdentity(), // $user
$sm->get('Mail'));
return $customLogger;
},
controller
<?php
namespace Foo\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Doctrine\ORM\EntityManager;
use MyProject\Proxies\__CG__\OtherProject\Proxies\__CG__\stdClass;
class FooController extends AbstractActionController
{
protected $customLogger;
private function getCustomLogger()
{
if (null === $this->customLogger) {
$this->customLogger = $this->getServiceLocator()->get('Zend\CustomLogger');
}
return $this->customLogger;
}
public function indexAction()
{
$this->getCustomLogger();
$this->customLogger->controllerLog("ENTER IN Foo\Controller\FooController\index", "info");
// .... other code
}
}
Error
Zend\ServiceManager\Exception\ServiceNotCreatedException
File:
/var/www/solutions/mysolution/zend/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:930
Message:
An exception was raised while creating "Zend\CustomLogger"; no instance returned
PHP modules installed
bz2.so curl.so fileinfo.so iconv.so mbstring.so mysqlnd.so pdo_sqlite.so shmop.so sqlite3.so sysvshm.so xmlreader.so xsl.so
calendar.so dom.so ftp.so intl.so mysqlnd_mysqli.so pdo_mysqlnd.so phar.so simplexml.so sysvmsg.so tokenizer.so xml.so zip.so
ctype.so exif.so gettext.so json.so mysqlnd_mysql.so pdo.so posix.so sockets.so sysvsem.so wddx.so xmlwriter.so
Extension enabled in PHP ini
extension=/usr/lib64/php/5.5/modules/php_bz2.so
extension=/usr/lib64/php/5.5/modules/php_curl.so
extension=/usr/lib64/php/5.5/modules/php_fileinfo.so
extension=/usr/lib64/php/5.5/modules/php_gd2.so
extension=/usr/lib64/php/5.5/modules/php_intl.so
extension=/usr/lib64/php/5.5/modules/php_mbstring.so
extension=/usr/lib64/php/5.5/modules/php_mysql.so
extension=/usr/lib64/php/5.5/modules/php_mysqli.so
extension=/usr/lib64/php/5.5/modules/php_openssl.so
extension=/usr/lib64/php/5.5/modules/php_pdo_mysql.so
extension=/usr/lib64/php/5.5/modules/php_soap.so
extension=/usr/lib64/php/5.5/modules/php_xmlrpc.so
extension=/usr/lib64/php/5.5/modules/php_xsl.so
So simple solution... permission on log directory and some others directory are wrong. Restore the correct permissione, user and group on directory (e.g. data/logs, data/cache...)
I'm looking for a way to use component inside a cell, is there any way ?
I tried :
$this->loadComponent('SessionsActivity');
My Cell :
namespace App\View\Cell;
use Cake\Core\Configure;
use Cake\View\Cell;
class UserCell extends Cell
My query :
$user = $this->Users
->find()
->where([
'Users.id' => $this->request->id
])
->contain([
'Towns' => function ($q) {
return $q->find('short');
},
'Countries' => function ($q) {
return $q->find('short');
}
])
->map(function ($user) {
$user->online = $this->SessionsActivity->getOnlineStatus($user);
return $user;
})
->first();
Using the same example at the CakePHP cells documentation, suppose you want to show the inbox for the connected user. You'd want to access the Auth component to take the connected user id.
AFAIK, Cells act like view-controller, a view part display.ctp file, and a controller part where you access the model to generate data for the view.
I'm using CakePHP 3.0 and $this->_registry doesn't work.
You could try:
$this->SessionsActivity = $this->_registry->load('SessionsActivity');
it is another question if you should use a component here
I'm using a simple CsvBulkLoader to bulk update dataobjects.
class OrderImporter extends CsvBulkLoader {
public $delimiter = ';';
public $enclosure = '"';
public $hasHeaderRow = true;
public $columnMap = array(
'ID' => 'ID',
'Bezahlt' => 'Payed',
'Geandert' => 'NeedReview'
);
}
My problem is, that I don't want to create new objects, If they are in the import file. I only want to update the existing ones.
Is there a way to achieve this? Sadly I can't find anything in the docs.
I think you'd have a look at CsvBulkLoader::processRecord(). This is where each line is processed. You could try in your OrderImporter class (untested):
protected function processRecord($record, $columnMap, &$results, $preview = false) {
// find existing object
$existingObj = $this->findExistingObject($record, $columnMap);
return ($existingObject)
? parent::processRecord($record, $columnMap, $results, $preview)
: false;
}
HTH, wmk
You need to set $duplicateChecks based on what fields already existing in your DB can be checked against the import file. If the IDs match you can use:
public $duplicateChecks = array(
'ID' => 'ID',
);
You should test the import on a dev server first, especially if you use a combination of fields, as the results can be different from what you're expecting.
See $duplicateChecks in the BulkLoader api http://api.silverstripe.org/3.1/class-BulkLoader.html