Donnerstag, 18. Dezember 2014

How to use PHPUnit fixtures in your Yii2 development

Often you need to create extra database data to run your PHPUnit test, when you're creating your application with Yii2 framework. The best is, that you're doing this work only once and save this with your test, so that you can re-use this test data on every test run. This data is called "fixtures". I want to introduce you my approach of using fixtures with CSV files, which are containing the data which will be written into your database, as you're fireing your PHPUnit test. For programming and running PHPUnit tests I prefer PhpStorm. But you can test your code also with PHPUnit by command line of some configuration in Netbeans or Eclipse with PHP configuration.

PhpMyAdmin and MySQL workbench are allowing you to export CSV files with your database data and the column-names in the first line of the export. The contents of the first line are the names of your properties, which are defined in your Yii model. Ok, let's say you have such exports, you need to have your PHPUnit test.



At the beginning you see, that I use the ModelFixtureDataCreator(); class for creating my fixtures for the PHPUnit test. The folder structure in my project is the same as in the namespace. I'll explain you the ModelFixtureDataCreator(); class later in detail. The first method in my test is setUp(); which works like a constructor in PHPUnit. I require a file which is booting Yii2 for me, so that I can use it in my unit tests.



This code is very easy, it handles just the paths for me, so that I don't have to mind from which folder I start my tests. This file is just booting Yii's console application. It's just the file you use to run your Gii application from the command line.

I've modified it a bit for my needs (you're free to modify yours as you wish):



Then I determine the name of my test class and pass it finally to my ModelFixtureDataCreator(); class, where I call the create-method statically:



The magic from the ModelFixtureDataCreator(); class is, that it uses "convention over configuration". Once you have the following folder structure the class is able to create all fixture data in relation to your test.

- your yii2 application folder
-- tests folder
--- fixtures folder
---- folder exactly named like your test without the file extension (f.e. ExportControllerTest)
----- the csv files exactly named like your models (f.e. MyModel.csv)

Downwards you can see the code from ModelFixtureDataCreator(); class with it's comments.



The class is currently only writing fixture data into your database. By the tearDown(); method from your PHPUnit test you're able to implement logic, which is deleting the fixture data after your test has been runned.

Do you have any ideas for this approach? Share them!






Mittwoch, 17. Dezember 2014

Eclipse mit Plugins zur PHP IDE rüsten




Hallo zusammen,
da ihr viele meist Remote arbeitet und da auch eine PHP IDE nützlich ist, ist Eclipse nicht schlecht. Im Gegensatz zu Netbeans oder PhpStorm lädt Eclipse mit dem Remote System Explorer Plugin die Dateien nicht herunter. Zudem sind Calltips für PHP Befehle und das Springen zu Funktionsdeklarationen möglich (Remote sobald die Datei mit den Deklarationen einmal geöffnet wurde). Man springt z.B. mit STRG + Klick mit linker Maustaste auf Funktionsaufruf im Code. 

Das Cacheing der Deklarationen klappt mit dem letzten Punkt in der unten aufgeführten Anleitung. Probierts mal aus. Ist eine gute OpenSource IDE für PHP Entwicklung, gerade wenn ihr Remote arbeitet, nicht einzelne Projekte anlegen und den Code herunterladen wollt.