Artisan Développeur

Qualité, amélioration continue, agilité.

Results for tag "symfony"

3 Articles

Mochajs locally in your symfony project

How to install node and mocha locally to unit test you javascript code

I will install packages in the require-dev section of my composer.json file, because in this case, I only use nodejs and mocha in the dev, preprod and test environments and they use the requierdev.
I don’t want to install that on my production environment.

To do that, I use two composer packages:

mouf/nodejs-installer

Packagist: https://packagist.org/packages/mouf/nodejs-installer
Github: https://github.com/thecodingmachine/nodejs-installer

koala-framework/composer-extra-assets

Packagist: https://packagist.org/packages/koala-framework/composer-extra-assets
Github: https://github.com/koala-framework/composer-extra-assets

Here the installation

You have to add this to composer.json file:

"require": {
    "mouf/nodejs-installer": "^1.0",
    "koala-framework/composer-extra-assets": "~1.1"
},
"extra": {
    "require-dev-npm": {
        "mocha": "*"
    }
}

To use mocha locally you have to add one file to the bin directory of your symfony project

In the script section, both in « post_install_cmd » and « post_update_cmd »

"touch bin/mocha && chmod +x bin/mocha && echo '#!/bin/bash \n DIR=$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" && pwd ) \n export PATH=$DIR/../vendor/nodejs/nodejs//bin:$PATH \n ../node_modules/.bin/mocha \"$@\"' > bin/mocha"

Example:

],
    "scripts": {
        "post-root-package-install": [
            "SymfonyStandard\\Composer::hookRootPackageInstall"
        ],
        "post-install-cmd": [
            [...] //do not copy/paste, it's just an example !
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::removeSymfonyStandardFiles",
            "touch bin/mocha && chmod +x bin/mocha && echo '#!/bin/bash \n DIR=$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" && pwd ) \n export PATH=$DIR/../vendor/nodejs/nodejs//bin:$PATH \n ../node_modules/.bin/mocha \"$@\"' > bin/mocha"
        ],
        "post-update-cmd": [
            [...]  //do not copy/paste, it's just an example !
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::removeSymfonyStandardFiles",
            "touch bin/mocha && chmod +x bin/mocha && echo '#!/bin/bash \n DIR=$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" && pwd ) \n export PATH=$DIR/../vendor/nodejs/nodejs//bin:$PATH \n ../node_modules/.bin/mocha \"$@\"' > bin/mocha"
        ]
    },

This line will create the mocha file and set the execution permission.

To actually install these:

composer install

Create a js directory src/Tests

With you IDE, or

mkdir src/Tests/Js

Finally use it just like that

Run all tests files:

bin/mocha src/Tests/Js/*

Or specific one:

bin/mocha src/Tests/Js/file.js

That’s it ! You can now unit test easily your javascript code !

As always, if you have question,  better practice, suggestion comment !

Don’t miss 500 internal error anymore with symfony and monolog

Sometimes I read my server logs … Like some people read the newspaper, in fact server logs are like the app newspaper 🙂

Every morning I am reading my logwatch email.

But I can miss errors, and even if I see them, it is the next day in my logs email, and it is really BAD

The solution is simple, use monolog to send you errors directly

https://symfony.com/doc/current/logging/monolog_email.html

My configuration is (config_prod.yml):

[pastacode lang= »markup » manual= »monolog%3A%0A%20%20%20%20handlers%3A%0A%20%20%20%20%20%20%20%20main%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20type%3A%20%20%20stream%0A%20%20%20%20%20%20%20%20%20%20%20%20path%3A%20%20%20%22%25kernel.logs_dir%25%2F%25kernel.environment%25.log%22%0A%20%20%20%20%20%20%20%20%20%20%20%20level%3A%20%20error%0A%20%20%20%20%20%20%20%20mail%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20type%3A%20%20%20%20%20%20%20%20%20fingers_crossed%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20500%20errors%20are%20logged%20at%20the%20critical%20level%0A%20%20%20%20%20%20%20%20%20%20%20%20action_level%3A%20critical%0A%20%20%20%20%20%20%20%20%20%20%20%20%20handler%3A%20%20%20%20%20%20deduplicated%0A%20%20%20%20%20%20%20%20deduplicated%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20type%3A%20%20%20%20deduplication%0A%20%20%20%20%20%20%20%20%20%20%20%20handler%3A%20swift%0A%20%20%20%20%20%20%20%20swift%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20type%3A%20%20%20%20%20%20%20swift_mailer%0A%20%20%20%20%20%20%20%20%20%20%20%20from_email%3A%20’email%40myserv.myserv’%0A%20%20%20%20%20%20%20%20%20%20%20%20to_email%3A%20%20%20’mymorningemail%40myserv.myserv’%0A%20%20%20%20%20%20%20%20%20%20%20%20subject%3A%20%20%20%20’You%20have%20to%20work%20!%20An%20Error%20Occurred!%20%25%25message%25%25’%0A%20%20%20%20%20%20%20%20%20%20%20%20level%3A%20%20%20%20%20%20debug%0A%20%20%20%20%20%20%20%20%20%20%20%20formatter%3A%20%20monolog.formatter.html%0A%20%20%20%20%20%20%20%20%20%20%20%20content_type%3A%20text%2Fhtml » message= » » highlight= » » provider= »manual »/]

 

 

Symfony error: 500 Internal Server Error – SyntaxErrorException

This article is at first a reminder for me, and if it can helps someone it’s even better 😉

When you work with a ORM (Doctrine for me, with Symfony) you have to think data as entities, objects instead of  database tables.

You are moving away from database (mariaDB in my case) and his constraints, it is a good thing for severals points, but there is one you can forget about and time to time you fall into his trap, and you get a really pretty error message.

An exception occurred while executing ‘INSERT INTO my_table (createdAt, updatedAt, name, commercialName, VAT,  oneField, anotherField, foo, bar, again, andAgain, default) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)’ with params [« 2018-02-23 13:55:24 », « 2018-02-23 13:55:24 », « my company », « really nice company », « 20 », null, « 1 », « 0 », null, null, null, null, 0]:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘default) VALUES (‘2018-02-23′ at line 1’
500 Internal Server Error – SyntaxErrorException
2 linked Exceptions:

PDOException

No more suspense here, the trap is quite simple and very basic, it’s the reserved keywords.

Reserved Keywords needs to be managed correctly if you want to use them, below see a real life example:

I need to know if a company is the default one for my user, I add a property name « default » (I use annotations for the doctrine mapping) and bam, SQL is broken.

[pastacode lang= »php » manual= »%20%20%20%20%2F**%0A%20%20%20%20%20*%20%40ORM%5CColumn(type%3D%22boolean%22%2C%20nullable%3Dfalse)%0A%20%20%20%20%20*%2F%0A%20%20%20%20private%20%24default%20%3D%20false%3B » message= »doctrine field  » highlight= » » provider= »manual »/]

The above  error message is pretty clear when you think about the keywords, because Doctrine use the property name to create the database field, the new field is:

[pastacode lang= »sql » manual= »%60default%60%20tinyint(1)%20NOT%20NULL » message= » » highlight= » » provider= »manual »/]

And when you want to insert a company:

[pastacode lang= »sql » manual= »INSERT%20INTO%20my_table%20(createdAt%2C%20updatedAt%2C%20%5B…%5D%2C%20default%20)%20″ message= » » highlight= » » provider= »manual »/]

« default » is not quoted, and mariaDb rise an error.

Fortunately we have several solutions, and my personal (subjective) choice:

Rename your property to something not reserved like « defaultCompany »

[pastacode lang= »php » manual= »%24company-%3EisDefaultCompany()%3B » message= » » highlight= » » provider= »manual »/]

I don’t like that, because the entity is a Company, if you name your variable $company to store it, it is not nice to read.

Quote the field name (in the annotations – doc here)

[pastacode lang= »php » manual= »%20%20%20%20%2F**%0A%20%20%20%20%20*%20%40ORM%5CColumn(name%3D%22%60default%60%22%2C%20type%3D%22boolean%22%2C%20nullable%3Dfalse)%0A%20%20%20%20%20*%2F%0A%20%20%20%20private%20%24default%20%3D%20false%3B » message= » » highlight= » » provider= »manual »/]

I don’t like this one either, it is better than the first but it can lead to complications (see the linked documentation)

Force a field name with the annotations

[pastacode lang= »php » manual= »%20%20%20%20%2F**%0A%20%20%20%20%20*%20%40ORM%5CColumn(name%3D%22defaultCompany%22%2C%20type%3D%22boolean%22%2C%20nullable%3Dfalse)%0A%20%20%20%20%20*%2F%0A%20%20%20%20private%20%24default%20%3D%20false%3B » message= » » highlight= » » provider= »manual »/]

Since we need to add the name of the database field if we want to quote it, I prefer have a non reserved keyword, unquoted, and never be worried by some obscure bug or malfunction because I want so bad to use this field name.

With Doctrine you are almost never in the database, it is not a big deal to have a « defaultCompany » field in the « company » table.

It is bother me when I read my code, but I don’t spend time in the SQL, I prefer have this little ugliness in my DB than in my code.