phpunit unit testing error zf2 doctrine - unit-testing

I've just started with Zend framework 2, with Doctrine. I want to set up unit testing for my Album module.
When I run c:\wamp\www\zf2-tutorial\module\Album\test > phpunit from command prompt,
I get following error:
PHPUnit 3.7.10 by Sebastian Bergmann.
Configuration read from C:\wamp\www\zf2-tutorial\module\Album\test\phpunit.xml.d
ist
.FFE
Time: 2 seconds, Memory: 8.25Mb
There was 1 error:
1) AlbumTest\Controller\AlbumControllerTest::testIndexActionCanBeAccessed
Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\Serv
iceManager::get was unable to fetch or create an instance for doctrine.entityman
ager.orm_default
C:\wamp\www\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\Service
Manager\ServiceManager.php:452
C:\wamp\www\zf2-tutorial\module\Album\src\Album\Controller\AlbumController.php:2
5
C:\wamp\www\zf2-tutorial\module\Album\src\Album\Controller\AlbumController.php:3
3
C:\wamp\www\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\Mvc\Con
troller\AbstractActionController.php:88
C:\wamp\www\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\EventMa
nager\EventManager.php:464
C:\wamp\www\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\EventMa
nager\EventManager.php:208
C:\wamp\www\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\Mvc\Con
troller\AbstractController.php:107
C:\wamp\www\zf2-tutorial\module\Album\test\AlbumTest\Controller\AlbumControllerT
est.php:71
--
There were 2 failures:
1) AlbumTest\Controller\AlbumControllerTest::testDeleteActionCanBeAccessed
Failed asserting that 302 matches expected 200.
C:\wamp\www\zf2-tutorial\module\Album\test\AlbumTest\Controller\AlbumControllerT
est.php:54
2) AlbumTest\Controller\AlbumControllerTest::testEditActionCanBeAccessed
Failed asserting that 302 matches expected 200.
C:\wamp\www\zf2-tutorial\module\Album\test\AlbumTest\Controller\AlbumControllerT
est.php:64
FAILURES!
Tests: 4, Assertions: 3, Failures: 2, Errors: 1.
The root of the issue seems to be:
Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\Serv
iceManager::get was unable to fetch or create an instance for doctrine.entityman
ager.orm_default from test..
I don't understand this, please help!

Okay finally got it!!
I changed my TestConfig.php.dist inside module/Album/test to
<?php
return array(
'modules' => array(
'DoctrineModule',
'DoctrineORMModule',
'Album',
),
'module_listener_options' => array(
'config_glob_paths' => array(
'../../../config/autoload/{,*.}{global,local}.php',
),
'module_paths' => array(
'module',
'vendor',
),
),
);
so basically just added two missing modules, DoctrineModule and DoctrineORMModule, and the phpunit command showed no errors!
Hope it helps others.

Related

Cakephp 3 unit test unable to post data

I run an api using postman, it works. But my unit tests that I create for it doesn't work
Below is my unit test file
<?php
namespace App\Test\TestCase\Controller\Api;
use App\Controller\Api\EmployeesController;
use Cake\TestSuite\IntegrationTestCase;
/**
* App\Controller\Api\EmployeesController Test Case
*/
class EmployeesApiControllerTest extends IntegrationTestCase
{
/**
* Fixtures
*
* #var array
*/
public $fixtures = [
'app.projects',
'app.projects_sites',
'app.transactions',
'app.sites'
];
/**
* Test REST API PIN login
*/
public function testJwtToken() {
$this->configRequest([
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]
]);
$this->post('/api/employees/token', ['pin_code' => '1001']);
$this->assertResponseOk();
}
}
And this is the console response after running it
$ docker-compose run webapp vendor/bin/phpunit tests/TestCase/Controller/Api/EmployeesApiControllerTest.php
Starting timesheetcakephp_mysql_1 ... done
PHPUnit 6.5.7 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 1.52 seconds, Memory: 6.00MB
There was 1 failure:
1) App\Test\TestCase\Controller\Api\EmployeesApiControllerTest::testJwtToken
Status code is not between 200 and 204
Failed asserting that 500 is equal to 204 or is less than 204.
/var/www/html/vendor/cakephp/cakephp/src/TestSuite/IntegrationTestCase.php:796
/var/www/html/vendor/cakephp/cakephp/src/TestSuite/IntegrationTestCase.php:713
/var/www/html/tests/TestCase/Controller/Api/EmployeesApiControllerTest.php:37
FAILURES!
Tests: 1, Assertions: 4, Failures: 1.
The debug log keeps showing this
2018-03-29 19:47:13 Debug: duration=0 rows=0 SELECT Employees.id AS `Employees__id`, Employees.project_id AS `Employees__project_id`, Employees.name AS `Employees__name` FROM employees Employees WHERE Employees.pin_code = NULL LIMIT 1
I don't understand why the pin_code keep stating as NULL when I have clearly stated in my unit test to be '1001'.

AWS php sdk is not working with PHP 7

Following code works well in PHP 5.6.
require_once '../aws/aws-autoloader.php';
$config = [
'region' => 'ap-south-1',
'version' => 'latest',
'credentials' => [
'key' => '...',
'secret' => '...'
]
];
$sdk = new Aws\Sdk($config);
$client = $sdk->createS3();
But on PHP 7, it through error:
Fatal error: Uncaught TypeError: Argument 1 passed to Aws\Common\Client\AbstractClient::__construct() must be an instance of Aws\Common\Credentials\CredentialsInterface, array given, called in /var/www/html/webservice/vendor/aws3/Aws/Sdk.php on line 316 and defined in /var/www/html/aws-sdk/vendor/aws/aws-sdk-php/src/Aws/Common/Client/AbstractClient.php:75 Stack trace: #0 /var/www/html/webservice/vendor/aws3/Aws/Sdk.php(316): Aws\Common\Client\AbstractClient->__construct(Array) #1 /var/www/html/webservice/vendor/aws3/Aws/Sdk.php(291): Aws\Sdk->createClient('S3', Array) #2 index.php(14): Aws\Sdk->__call('createS3', Array) thrown in /var/www/html/aws-sdk/vendor/aws/aws-sdk-php/src/Aws/Common/Client/AbstractClient.php on line 75
Please suggest me how can I make it work on PHP 7.
Thanks.
UPDATE: I have both AWS SDK version 2 & 3 in my project. this might be a issue of conflict. Then how to solve the conflict?
This problem doesn't have any issue with PHP 7.0.
It's the case of conflict SDK ver 2 & 3.
This two sdk can't be used together. so I had to upgrade to sdk 3.

Yii2 codeception authentication and XML reports output

I have some trouble testing my code with codeception in Yii2 and i hope one of you can help me.
First of all my authentication doesn't work as expected.
In my Class PagesUrl the user isn't logged in but in my template file the user is logged in. Whenever the page is not accessed with Codeception it's working fine.
Code to check if user is logged in: var_dump(Yii::$app->getUser()->getIsGuest());
Config of UrlManager:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'common\routes\PagesUrl',
'pattern' => '',
'route' => 'site/index',
],
//Some additional rules
],
]
Second I can't seem to generate XML reports.
Command for test output: codecept run functional LoginFormCest --xml -vvv
Gives this output:
Codeception PHP Testing Framework v2.1.0
Powered by PHPUnit 4.8.35-1-g912b8c1e9 by Sebastian Bergmann and contributors.
Functional Tests (5) ----------------------------------------------------------------------------------------------------------------------------------------------------------
Modules: Filesystem, Yii2
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Printing JUNIT report into report.xml
[PHPUnit_Framework_Exception]
Undefined index: log_incomplete_skipped
Exception trace:
() at C:\xampp\htdocs\stoneart-v2\vendor\codeception\base\src\Codeception\Subscriber\ErrorHandler.php:75
Codeception\Subscriber\ErrorHandler->errorHandler() at C:\xampp\htdocs\stoneart-v2\vendor\codeception\base\src\Codeception\PHPUnit\Runner.php:145
Codeception\PHPUnit\Runner->applyReporters() at C:\xampp\htdocs\stoneart-v2\vendor\codeception\base\src\Codeception\PHPUnit\Runner.php:91
Codeception\PHPUnit\Runner->doEnhancedRun() at C:\xampp\htdocs\stoneart-v2\vendor\codeception\base\src\Codeception\SuiteManager.php:157
Codeception\SuiteManager->run() at C:\Users\j-rub\AppData\Roaming\Composer\vendor\codeception\codeception\src\Codeception\Codecept.php:200
Codeception\Codecept->runSuite() at C:\Users\j-rub\AppData\Roaming\Composer\vendor\codeception\codeception\src\Codeception\Codecept.php:172
Codeception\Codecept->run() at C:\Users\j-rub\AppData\Roaming\Composer\vendor\codeception\codeception\src\Codeception\Command\Run.php:184
Codeception\Command\Run->execute() at C:\Users\j-rub\AppData\Roaming\Composer\vendor\symfony\console\Command\Command.php:264
Symfony\Component\Console\Command\Command->run() at C:\Users\j-rub\AppData\Roaming\Composer\vendor\symfony\console\Application.php:846
Symfony\Component\Console\Application->doRunCommand() at C:\Users\j-rub\AppData\Roaming\Composer\vendor\symfony\console\Application.php:191
Symfony\Component\Console\Application->doRun() at C:\Users\j-rub\AppData\Roaming\Composer\vendor\symfony\console\Application.php:122
Symfony\Component\Console\Application->run() at C:\Users\j-rub\AppData\Roaming\Composer\vendor\codeception\codeception\codecept:28
Command for test coverage: codecept run functional LoginFormCest --coverage-xml
Gives this output:
FAILURES!
Tests: 5, Assertions: 3, Failures: 5.
[yii\base\ErrorException]
Undefined index: quiet
Exception trace:
() at C:\xampp\htdocs\stoneart-v2\vendor\codeception\base\src\Codeception\Coverage\Subscriber\Printer.php:61
::call_user_func:{C:\xampp\htdocs\stoneart-v2\vendor\symfony\event-dispatcher\EventDispatcher.php:184}() at C:\xampp\htdocs\stoneart-v2\vendor\symfony\event-dispatcher\EventDispatcher.php:184
Symfony\Component\EventDispatcher\EventDispatcher->doDispatch() at C:\xampp\htdocs\stoneart-v2\vendor\symfony\event-dispatcher\EventDispatcher.php:46
Symfony\Component\EventDispatcher\EventDispatcher->dispatch() at C:\Users\j-rub\AppData\Roaming\Composer\vendor\codeception\codeception\src\Codeception\Codecept.php:218
Codeception\Codecept->printResult() at C:\Users\j-rub\AppData\Roaming\Composer\vendor\codeception\codeception\src\Codeception\Command\Run.php:204
Codeception\Command\Run->execute() at C:\Users\j-rub\AppData\Roaming\Composer\vendor\symfony\console\Command\Command.php:264
Symfony\Component\Console\Command\Command->run() at C:\Users\j-rub\AppData\Roaming\Composer\vendor\symfony\console\Application.php:846
Symfony\Component\Console\Application->doRunCommand() at C:\Users\j-rub\AppData\Roaming\Composer\vendor\symfony\console\Application.php:191
Symfony\Component\Console\Application->doRun() at C:\Users\j-rub\AppData\Roaming\Composer\vendor\symfony\console\Application.php:122
Symfony\Component\Console\Application->run() at C:\Users\j-rub\AppData\Roaming\Composer\vendor\codeception\codeception\codecept:28
Codeception.yml
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
helpers: tests/_support
settings:
bootstrap: _bootstrap.php
memory_limit: 1024M
colors: false
modules:
config:
Yii2:
configFile: 'tests/config/test.php'
cleanup: false
coverage:
enabled: true
whitelist:
include:
- common/modules/news/*
Updating to the newest version did the work for me. Although there seems to be a bug displaying the right version of codeception. I would recommend to check your composer.json. Thanks.

Doctrine Orm Module Pdo doesnt get correct parameters for database when accessing website or console after Aws Beanstalk deploy

I am working through deploying a Zf2 app with Doctrine on Aws Beanstalk. Composer is running and setting up everything and afterwards I run a symfony console command to generate the databases and schemas using the parameters specified in my config. I followed the examples out of here to get the rds params for the configuration.
http://www.michaelgallego.fr/blog/2013/05/24/how-to-deploy-safe-zf-2-applications-on-amazon-elastic-beanstalk/
All this works up to accessing the website. At which point it gives me a Pdo error accessing the database.
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002]
No such file or directory' in
/var/app/current/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:43 Stack
trace: #0
/var/app/current/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php(43):
PDO->__construct('mysql:host=loca...', 'username', 'password', Array) #
/var/app/current/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php(45):
Doctrine\DBAL\Driver\PDOConnection->__construct('mysql:host=loca...', 'username',
'password', Array) #2
/var/app/current/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(360):
Doctrine\DBAL\Driver\PDOMySql\Driver->connect(Array, 'username', 'password', Array) #3
/var/app/current/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(429):
Doctrine\DBAL\Connection->connect() #4
/var/app/current/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(389):
Doctrine\DBAL\Connection->getDatabasePlatformVersion() #5
/var/app/current/vendor/doctrine/dbal/lib/Doctrine/DBAL/C in
The zf2 app is apigility. After deployment if I log into the server and run the php command
php public/index.php development enable
It will produce the same error as the frontend website.
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002]
No such file or directory' in
/var/app/current/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:43
I am setting up the database schemas through console commands that are in the aws config. This does setup the correct database and runs the schema and data import.
.ebextensions/composer.config
container_commands:
01installDev:
command: "/usr/bin/composer.phar install --dev"
02makeDatabase:
command: "/usr/bin/php /var/app/ondeck/cwg.php database:create"
03createDatabase:
command: "/usr/bin/php /var/app/ondeck/cwg.php database:fresh"
config/autoload/database.global.php
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\\DBAL\\Driver\\PDOMySql\\Driver',
'params' => array(
'host' => $_SERVER['RDS_HOSTNAME'],
'port' => $_SERVER['RDS_PORT'],
'user' => $_SERVER['RDS_USERNAME'],
'password' => $_SERVER['RDS_PASSWORD'],
'dbname' => 'api_default',
),
),
),
),
);
When I run the console commands I am just creating a doctrine instance with the parameters in these files, that is the only difference from the zf2 app, but I do get the correct params.
Trying to debug this I dumped the $options and $pdo variables inside a doctrine orm module factory.
vendor/doctrine/doctrine-orm-module/src/DoctrineORMModule/Service/DBALConnectionFactory.php
var_dump($options);die();
object(DoctrineORMModule\Options\DBALConnection)#369 (8) { ["configuration":protected]=>
string(11) "orm_default" ["eventmanager":protected]=> string(11) "orm_default
["pdo":protected]=> NULL ["driverClass":protected]=> string(36)
"Doctrine\DBAL\Driver\PDOMySql\Driver" ["wrapperClass":protected]=> NULL
["params":protected]=> array(5) {
["host"]=> string(9) "localhost"
["port"]=> string(4) "3306"
["user"]=> string(8) "username"
["password"]=> string(8) "password"
["dbname"]=>string(8) "database"
} ["doctrineTypeMappings":protected]=> array(0) {
["__strictMode__":protected]=> bool(true) }
var_dump($pdo);die();
NULL
The parameters that are being used are the default doctrine parameters. I dont know if this is to early in the process if zf2 hasn't loaded the config files but I haven't found any other places related to the connection that will var_dump before the error. What am I doing wrong either in my aws configuration setup, or firewall rules between the rds servers and the api (which I assumed beanstalk configured for you since it set everything up), or not doing my zf2 configuration correctly? Any help is appreciated.
edit
I moved the var_dump further down to line 60
var_dump($connection->getParams());die();
array(8) { ["driverClass"]=> string(36) "Doctrine\DBAL\Driver\PDOMySql\Driver
["wrapperClass"]=> NULL ["pdo"]=> NULL ["host"]=> string(9) "localhost" ["port"]=>
string(4) "3306" ["user"]=> string(8) "username" ["password"]=> string(8) "password
["dbname"]=> string(8) "database" }
I think this pretty well shows that Doctrine does not see my configuration at all and somehow I am doing something or not doing something.
Somehow inside the application.config.php file a static location was set for configuration.
'config_glob_paths' => array(
'/home/myhome/api/config/autoload/{,*.}{global,local}.php'
),
Once I changed that to
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php'
),
Doctrine started getting the correct params.

Doctrine migrations not using specified configuration

I have been using Doctrine and Migrations in my ZF2 project for a few weeks without issue. I have been executing migrations by specifying my own configuration file:
vendor/bin/doctrine-module migrations:diff --configuration=/path/to/migrations-config.xml
I recently brought in Codeception into my project via composer and when I recently went to execute a new migration (with the same above command), I received the following error:
[InvalidArgumentException]
Migrations directory data/DoctrineORMModule/Migrations does not exist
This is not the directory I have specified in my configuration file. At first I thought Doctrine Migrations was completely ignoring my --configuration argument. When I traced it through, I saw that Doctrine Migrations wasn't using my --configuration argument because it had already loaded a config file.
Would anyone know where this config file may be pulling in from? I can work around the issue for the time being by commenting out the conditional in AbstractCommand getMigrationConfiguration(), but long term I'd rather not have to rely on this. I'll also try and remove codeception and see if that has any impact. Any help is appreciated.
As it turns out, I was able to move Doctrine migrations configuration into my ZF2 module config. In my module.config.php, I have the following specified (Api is the module name):
'doctrine' => array(
'driver' => array(
'application_entities' => array(
'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/Api/Entity')
),
'orm_default' => array(
'drivers' => array(
'Api\Entity' => 'application_entities'
)
)
),
'migrations_configuration' => array(
'orm_default' => array(
'directory' => 'data/migrations',
'namespace' => 'My\Namespace\Migrations',
'table' => 'migrations',
),
),
),
This allows me to run Doctrine migrations commands without having to specify a configuration file, for example:
vendor/bin/doctrine-module migrations:diff
I see an alternative in using a stand-alone doctrine-migration.phar command:
php doctrine-migrations.phar migrations:migrate --configuration=config.xml --db-configuration=db-config.xml
To make this work, you need to download doctrine-migrations.phar from here: https://github.com/doctrine/migrations