Mix Prophecy Mock Framework with PHPUnit Mock Builder to Create Mocks for Not Existing Classes and Magic Methods

Submitted by Peter Majmesku on Wed, 08/16/2017 - 12:20

Prophecy is great as long as you are creating mocks for classes or methods, which are existing in your code base. The benefit is, that the usage is a bit more convenient compared with the PHPUnit mock builder (e.g. method definition and parameter handling). Therefor Prophecy is integrated into PHPUnit by default.

If you want to create mocks for classes, which are not existing in your code base or if you want to mock magic methods, this will become a problem. You could create a real class, just to have the class/method. But this is not an nice solution. That's why mixing mocking functionality from the Prophecy mocking framework and the PHPUnit mocking framework makes sense.

Here I am not questioning, if it is good to create tests for classes, that do not belong to your code base. I am just showing a way to handle this.

The following example works with PHPUnit 4.8.35 and up. The Prophecy mocking framework is part of PHPUnit.

// Mock the object via Prophecy - generally more convenient than with PHPUnit itself
$entityManager $this->prophesize(EntityManager::class);

// Create the mock object by PHPUnit, for a class that does not exist.
$shippingMethodRepository $this->getMockBuilder('ShippingMethod')

// Set the not existing method.

// Define that getRepository() can retrieve any arguments and get a "real" object to test with.

// Mock a few methods with Prophecy as you like

// Get the mocked object, which will fake behavior

// Will return 'Hermes'

MySQL: Quickly Select Data with Multiple Conditions from One Table

Submitted by Peter Majmesku on Fri, 08/04/2017 - 11:09

Selecting data from one table with multiple conditions with Subselects is slow. Joins are quicker. Here is an example:

select * from OrderMetaData
Join OrderMetaData as a on OrderMetaData.id = a.Order_id and a.name = 'store' and a.value = 128
Join OrderMetaData as b on OrderMetaData.id = b.Order_id and b.name = 'processing_status' and b.value = 4;

This query takes 0,44 seconds in a table with > 800.000 entries. The sub-select takes 17 seconds.


CSS: Place Content in the Middle of a Div-Element via Flexbox

Submitted by Peter Majmesku on Mon, 07/31/2017 - 15:03

There are many approaches and often they are tricky. The simplest approach is the following and it does not need any additional wrapper-elements. Just place the CSS inside the selector, which is containing your content. The content will be positioned in the horizontal and vertical middle of the container. Mind that the Flexbox CSS feature is fully supported since IE 11.

display: flex;
justify-content: center;
align-items: center;

More nice Flexbox solutions can be found here.


Dumping Object/Array Stacks in Symfony

Submitted by Peter Majmesku on Mon, 07/10/2017 - 13:54

I prefer using XDebug for debugging PHP code. But sometimes there's a huge nesting in the project, which makes the application very slow. Then XDebug can become pain. So there's an alternative for inspecting the contents of objects:


It works for single objects and Doctrine collections and should prevent browser displaying issues you are having.

Debugging eMails in Symfony 2/3 and Drupal 7/8

Submitted by Peter Majmesku on Fri, 06/30/2017 - 22:33

The best tool for debugging eMails locally in my opinion is Mailcatcher. It catches all mails being sent via a specific port and shows them to you via an neat interface in your webbrowser. Firstly install Mailcatcher. It's programmed in Ruby and I could install it easily on Ubuntu and Mac. The instructions can be found at https://mailcatcher.me/.

Symfony 2/3

In the full stack edition it comes with symfony/swiftmailer-bundle. Make sure these settings are in your parameters.yml + parameters.yml.dist file.

mailer_transport: smtp
mailer_user: ~
mailer_password: ~

Afterwards you can send a test mail via the console:

bin/console swiftmailer:email:send

The mails will be shown to you like in the screenshot below.

The Mailcatcher Interface in Webbrowser

Drupal 7/8

Beside Drupal 8 install the "SMTP Authentication Support". You can find it's project webpage at https://www.drupal.org/project/smtp. Then visit the following page in your Drupal 8 instance: http://your-neat-webpage.dev/admin/config/system/smtp. Make sure the following settings are set in the config form (there's also a possibility to set them in the settings.php file).

SMTP server:
SMTP backup server: LEAVE BLANK
SMTP port: 1025
Use encrypted protocol: No




Check "Enable debugging".

First part of settings form in Drupal 8
Second part of settings form in Drupal 8

Doctrine Query Builder: Filter Entity Types Based on Discriminator Column

Submitted by Peter Majmesku on Wed, 06/14/2017 - 09:36
<?php $queryBuilder $this->entityManager->createQueryBuilder()

$queryBuilder->where('mbt INSTANCE OF Bundle:SomeType'); 

MySQL: Write All Executed Queries Into a Log File

Submitted by Peter Majmesku on Tue, 05/23/2017 - 13:54
  1. Open the MySQL config file, which is located at /etc/mysql/my.cnf on Linux systems.
  2. Then write the following lines at the end of that file.

general_log_file = /var/log/mysql/general_mysql.log
general_log = on

Restart your MySQL server (e.g. by executing "sudo service mysql restart" on Ubuntu Linux systems). Now you can follow all new queries as they are executed by this command:

tail -f /var/log/mysql/general_mysql.log

You can leave this program in command line by pressing CTRL+C.

For disabling, just set "general_log" to "off" or comment the out the previous lines in /etc/mysql/my.cnf by writing the # character at the beginng of the lines, save and restart the MySQL server again.

Update: Alternative setting by "general_log" variable

Recently the approach from above was not possible on my MariaDB server in Linux Mint 18.1. The log file was just not written. Even if I have created it. Then I could enable the general_log by variable. For this login into your MariaDB/MySQL server via command line. Then enable the general_log:

SET GLOBAL general_log = On;

Check all your variables which have something to do with "log" by this command:

show variables like '%log%';

The filename of the general_log is stored in that variable: "general_log_file". The default general log file location on Ubuntu/Debian based Linux systems (like Linux Mint) is: 


These commands can help you, to see the setting quickly:

peter@computer ~/Dev/ $ mysql -uroot -ppassword -e "show variables like '%log%';" | grep general_log
mysql: [Warning] Using a password on the command line interface can be insecure.
general_log     ON
general_log_file        /var/log/mysql/general_mysql.log
peter@computer ~/Dev/ $