In codeception functional test seeInDatabase not works from laravel - laravel

I am using codeception for testing in laravel 5.2.
Here is my codeception.yml file:
actor: Tester
paths:
tests: tests_codecept
log: tests_codecept/_output
data: tests_codecept/_data
support: tests_codecept/_support
envs: tests_codecept/_envs
settings:
bootstrap: _bootstrap.php
colors: false
memory_limit: 1024M
extensions:
enabled:
- Codeception\Extension\RunFailed
modules:
config:
Db:
dsn: 'mysql:host=localhost;dbname=kartice_test'
user: '*******'
password: '*******'
dump: tests_codecept/_data/dump.sql
populate: true
cleanup: true
reconnect: true
and here is functional.suite.yml file:
class_name: FunctionalTester
modules:
enabled:
# add framework module here
- \Helper\Functional
- Asserts
- Laravel5:
environment_file: .env.testing
- Db
here is my test method:
public function provera_dodavanja_novog_klijenta(FunctionalTester $I)
{
$this->authenticate($I);
$I->amOnPage('/kancelarija/komitenti/create');
$I->see('Kreiranje novog komitenta');
$I->fillField('input[name=komitent_code]', 'kom1');
$I->fillField('input[name=komitent_name]', 'Komitent 1');
$I->click('btnSave');
$I->seeInDatabase('komitenti', ['code' => 'kom1', 'name' => 'Komitent 1']);
$I->see('Komitent Komitent 1 je uspešno kreiran.');
}
Running functional test fails with message:
Step I see in database "komitenti",{"code":"kom1","name":"Komitent 1"}
Fail No matching records found for criteria {"code":"kom1","name":"Komitent 1"} in table komitenti
Failed asserting that 0 is greater than 0.
What I am doing wrong?
I have seen question Codeception seeInDatabase() doesn't work for me but this didn't helpe me.

You should probably useseeRecord method instead of seeInDatabase. I don't know why but for me first one was working and second one - not.

I use gulp tdd and when testing forms come across this error.
Please check:
You added this to Requests
<YourFormRequest>use App\Http\Requests\<YourFormRequest>;
Ensure the Model for your table is mass assignable
protected $fillable = ['tableField1', 'tableField2']

Related

symfony 5.1 Bundle migration result in No mapping information to process

I'm developing a Symfony 5.1 project under DDD, so I'm changing a bit all the default folders.
I have 2 bundles inside src folder and at the moment I only have entities inside one of them.
I generated the Entity User and its repository with the command make:user and then moved the files and changed namespaces, routes, configs, etc
When I run php bin/console make:migration I get an error "No mapping information to process"
$ run php bin/console make:migration -v
In NoMappingFound.php line 13:
[Doctrine\Migrations\Provider\Exception\NoMappingFound]
No mapping information to process
The folders are:
src
|-- Smartlink
|-- UserBundle/SmartlinkUserBundle.php
|-- Application
|-- Domain
| |-- Entity/User.php
| |-- interfaces/UserRepository.php
|-- Infrastructure
|-- Repository/MysqlUserRepository.php
The configuration is:
// composer.json
"autoload": {
"psr-4": {
"UserBundle\\": "src/Smartlink/UserBundle",
"SmartlinkBundle\\": "src/Smartlink/SmartlinkBundle",
"App\\": "src/"
}
},
===============================================
// config/bundles.php
return [
...
UserBundle\SmartlinkUserBundle::class => ['all' => true],
SmartlinkBundle\SmartlinkSmartlinkBundle::class => ['all' => true],
];
===============================================
// config/services.yaml
services:
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
UserBundle\:
resource: '../src/Smartlink/UserBundle/'
exclude:
- '../src/Smartlink/UserBundle/Domain/Entity/'
SmartlinkBundle\:
resource: '../src/Smartlink/SmartlinkBundle/'
exclude:
- '../src/Smartlink/SmartlinkBundle/Domain/Entity/'
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
- '../src/Tests/'
- '../src/Smartlink/'
===============================================
// config/routes/annotations.yaml
userbundle_controllers:
resource: ../../src/Smartlink/UserBundle/Infrastructure/Controller
type: annotation
smartlinkbundle_controllers:
resource: ../../src/Smartlink/SmartlinkBundle/Infrastructure/Controller
type: annotation
controllers:
resource: ../../src/Controller/
type: annotation
kernel:
resource: ../../src/Kernel.php
type: annotation
===============================================
// config/packages/doctrine.yaml
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
SmartlinkUserBundle:
is_bundle: true
type: annotation
dir: 'Domain/Entity'
alias: user_bundle
SmartlinkSmartlinkBundle:
is_bundle: true
type: annotation
dir: 'Domain/Entity'
alias: smartlink_bundle
The UserRepository is the same that generated the make:user command except for the namespace, which is changed to namespace UserBundle\Infrastructure\Repository; and the name that is changed to MysqlUserRepository that implements the interface UserRepository
And the entity User is
namespace UserBundle\Domain\Entity;
use UserBundle\Infrastructure\Repository\MysqlUserRepository;
/**
* #ORM\Entity(repositoryClass=MysqlUserRepository::class)
*/
class User implements UserInterface
{
...
I've been searching and all I have found is about symfony 2 and symfony 4, I tried the same that worked for the people who was asking, but still can't generate the migration of the bundles. what am I missing?
Edit: I changed some configurations and solved the UserBundle is not found or is not active but the main migration problem remains
Finally I found an answer, the solution is in the file config/packages/doctrine.yaml
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
SmartlinkUserBundle:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Smartlink/UserBundle/Domain/Entity' # 'Domain/Entity'
prefix: 'UserBundle\Domain\Entity'
alias: UserBundle
SmartlinkSmartlinkBundle:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Smartlink/SmartlinkBundle/Domain/Entity'
prefix: 'SmartlinkBundle\Domain\Entity'
alias: SmartlinkBundle
The fix is setting in_bundle to false
the dir: was wrong, so I had to fix it
and I added the prefix, without the prefix there was an error.
And now it works.

Symfony4: How to toggle entities using dotenv and multiple Entity Manager?

How it is possible through dotenv select different entity path with same name of entities.
Originally, We had application in Symfony 3 for people meetings on events of our organization. Then we decided to offer this application to our partners.
One of the partners asked us to customize the application for them with their data and specifications. We basically created a new instance of Symfony application with copy of database tables (with different prefix), changes in Entities to reflect new db table names, and some translation text changes.
It seems that other partners will follow this trend of customized instances.
Therefore, I am trying to update core application to Symfony 4 and I am trying to use multiple Entity Managers and dotenv to differentiate between partners database tables, as described bellow.
In a nutshell, I am trying to use multiple Entity Manager to switch db table names by prefix.
.env
###> doctrine/doctrine-bundle ###
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
# Configure your db driver and server_version in config/packages/doctrine.yaml
DATABASE_URL=oci8://user:pass#127.0.0.1:1521/XE
EM_TYPE=OpenAccess
###< doctrine/doctrine-bundle ###
Only in security.yaml working env good
security:
encoders:
App\Entity\%env(EM_TYPE)%\Osoba:
providers:
our_db_provider:
entity:
class: App\Entity\%env(EM_TYPE)%\Osoba
property: username
When i tried get %env(EM_TYPE)% in default_entity_manager, then give error You have requested a non-existent service "doctrine.orm.%env(EM_TYPE)%_entity_manager". doctrine.yaml
parameters:
env(DATABASE_URL): ''
doctrine:
dbal:
default_connection: '%env(EM_TYPE)%'
connections:
MeetingTool:
driver: 'oci8'
charset: UTF8
schema_filter: /^MT_/
url: '%env(resolve:DATABASE_URL)%'
OpenAccess:
driver: 'oci8'
charset: UTF8
schema_filter: /^OA6_/
url: '%env(resolve:DATABASE_URL)%'
orm:
auto_generate_proxy_classes: true
default_entity_manager: '%env(EM_TYPE)%'
entity_managers:
MeetingTool:
connection: MeetingTool
mappings:
Main:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/MeetingTool'
prefix: 'App\Entity\MeetingTool'
alias: App2
OpenAccess:
connection: OpenAccess
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/OpenAccess'
prefix: 'App\Entity\OpenAccess'
alias: OpenAccess
and biggest problem is use, how i targeting right entity here? for example loginController.php
<?php
namespace App\Controller;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\OpenAccess\LogPrihlaseni;
//use App\Entity\MeetingTool\LogPrihlaseni;
class LoginController extends AbstractController {
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function logPrihlaseni() {
$log = new LogPrihlaseni();

Cocedeption with Laravel 5 exception when running functional and acceptance tests

I have a really simple test:
public function tryToGetJsonDataForAnItem(FunctionalTester $I)
{
$I->wantTo('Test if correct JSON is returned');
$I->sendPOST('/series/series', ['itemId' => 1]);
}
When I run the functional or acceptance tests with code coverage like this:
./vendor/bin/codecept run functional tests/functional/frontend/SeriesCest.php:tryToGetJsonDataForAnItem --env testing --coverage-html
I get the following error:
file_get_contents(http://domain.dev/c3/report/clear): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
After some googling I found out, that this is because remote coverage is set to true. I set it to false. This is how my coverage part of the codeception.yml looks like:
coverage:
enabled: true
remote: false
whitelist:
include:
- app/*
exclude:
- app/Events/*
- app/Exceptions/*
- app/Providers/*
And this is my functional.suite.yml
class_name: FunctionalTester
modules:
enabled: [Laravel5, Db, \Helper\Functional, REST]
config:
Laravel5:
url: http://domain.dev
cleanup: false
REST:
depends: PhpBrowser
url: http://domain.dev
Note: If I run the tests without test coverage, they pass.

Test Database and Codeception

I'm working With Laravel 5 / Codeception.
I'm working with a test database.
Here is my config:
acceptance.suite.yml:
class_name: AcceptanceTester
modules:
enabled:
- WebDriver
- \Helper\Acceptance
- Db
- Asserts
config:
WebDriver:
url: 'http://laravel.dev'
browser: 'phantomjs'
window_size: 1024x768
Db:
dsn: 'mysql:host=laravel.dev;dbname=kendo_test'
user: 'homestead'
password: 'secret'
So, here I define my db being my Test db.
Then, in my bootstrap.php I have:
$app->loadEnvironmentFrom('.env.testing');
And .env.testing:
DB_HOST=127.0.0.1
DB_DATABASE=kendo_test
DB_USERNAME=homestead
DB_PASSWORD=secret
As a test, I changed kendo_test to kendo_test2, and it failed, it is using this db.
Now, When I execute an acceptance test, my test fails because row is inserted in main db, not test, And I don't know why....
Here is my test:
public function it_create_user(\AcceptanceTester $I, $scenario)
{
App::setLocale('en');
$user = factory(User::class)->make();
$I = new SimpleUser($scenario);
$I->logAsUser();
$I->dontSee(trans_choice('core.user', 2) . ' </a></li>');
$I->logout();
$I = new SuperAdmin($scenario);
$I->logAsSuperAdmin();
$I->click('#dropdown-user');
$I->click(trans_choice('core.user', 2));
$I->click(trans('core.addModel', ['currentModelName' => trans_choice('core.user', 1)]));
$I->fillField('name',$user->name );
$I->fillField('email',$user->email);
$I->fillField('firstname',$user->firstname);
$I->fillField('lastname',$user->lastname);
$I->fillField('password','111111');
$I->fillField('password_confirmation','111111');
$I->click(trans('core.save')); // <-- Here is should save it
$I->seeInCurrentUrl('/users');
$I->seeInSource(trans('msg.user_create_successful'));
$I->seeInDatabase('ken_users', ['name' => $user->name]);
}
Any Idea why???
When you click $I->click(trans('core.save')); it will be used the .env from your app and not the one from $app->loadEnvironmentFrom.
This is because when running acceptance tests you are interacting with your app via a browser.
The test being run has its own instance, as well as the app accessed by the test.
The only reason here you would use $app->loadEnvironmentFrom is to leverage Eloquent, and even then it must be on a separate connection.

How configure and use doctrine extension? (doctrine2-spatial)

I want use postrepgis in doctrine. I have find creof/doctrine2-spatial and I try use it. I have this error :
[Semantical Error] line 0, col 15 near 'ST_Contains(v.geopoint': Error: Class 'ST_Contains' is not defined.
I don't know why, I have read the documentation, I have try many differents configurations but alltimes same problem.
My config.yml
doctrine:
dbal:
driver: pdo_pgsql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
types:
geometry: CrEOF\Spatial\DBAL\Types\GeometryType
point: CrEOF\Spatial\DBAL\Types\Geometry\PointType
polygon: CrEOF\Spatial\DBAL\Types\Geometry\PolygonType
linestring: CrEOF\Spatial\DBAL\Types\Geometry\LineStringType
mapping_types:
_text: string
orm:
auto_generate_proxy_classes: "%kernel.debug%"
#naming_strategy: doctrine.orm.naming_strategy.underscore
entity_managers:
default:
dql:
numeric_functions:
ST_Contains: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STContains
Contains: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STContains
st_area: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STArea
st_geomfromtext: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\GeomFromText
st_intersects: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STIntersects
st_buffer: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STBuffer
point: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STPoint
auto_mapping: true
And my repository
public function findPointIn(){
$queryBuilder = $this->_em->createQueryBuilder();
$queryBuilder = $queryBuilder->select('v')
->where(
$queryBuilder->expr()->eq(
sprintf("ST_Contains(v.geopoint , v.geopoint)"),
$queryBuilder->expr()->literal(true)
)
);
return $queryBuilder->getQuery()->getResult();
}
According to documentation, your numeric_functions names in config.yml must be lowercase. Change to:
numeric_functions:
st_contains: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STContains
contains: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Contains
st_area: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Area
st_geomfromtext: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\GeomFromText
st_intersects: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STIntersects
st_buffer: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STBuffer
point: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\Point

Resources