21. Oct 2022 |

php artisan test – No tests executed

I spend my time so that you don’t have to.

If you are getting the response “No tests executed” from php artisan test then please check that your test classes have their names in singular. ContactTest instead of ContactTests.

I spent like 3h to figure out why my Feature tests don’t run from command line while working well in phpStorm. The reason was that my class names were in plural.

 

18. Oct 2022 |

Laravel – run PHPUnit test on separate database (with phpStorm)

In phpStorm see Settings -> PHP -> Test Frameworks -> Test Runner. Check if you have default configuration file phpunit.xml checked. No need to change anything in this file.

Create .env.testing and copy whole DB_ block from .env (not just parts that are different). Change values to your test database values.

Then run php artisan config:clear which will clear configuration cache.

Everything should work 🙂

I spent several hours figuring it out. Hopefully you don’t.

9. Oct 2022 |

Class ‘phpunit’ could not be found

Created new Laravel project by composer create-project laravel/laravel example-app. Trying to run tests/Feature/ExampleTest.php in PhpStorm and I get an error Class 'phpunit' could not be found in '/home/robert/phpunit-test-app/vendor/phpunit/phpunit/phpunit'. What does it even mean?! 😀 There are no answers on Google at the moment and this post will probably rank well.

Solution

For some reason my CLI interpreter did not make any sense. It took me quite a few hours to figure it out.

30. Aug 2022 |

Eestiblogid.eu uuendus – Eloquent ja e-mailid

Panin lÔpuks uue blogi lisamise vormi tööle (lehe allosas). See ei tee muud, kui saadab mulle e-mailile uue blogi lisamise info.

Pisut oodatust kauem kulus aega sellele, et .env failis e-maili saatmise konfiguratsiooni paika saada.  Arenduse ajal kasutasin mailtrap.io teenust aga toodangus tahtsin ikka Zone enda SMTPd kasutada. Siin nĂ€idises oleva konfiguratsiooniga tekkis pildil olev viga. PĂ”hjuseks, et mul oli Laravel 8 → Laravel 9 uuendamise kĂ€igus puudu jÀÀnud mail.php failis rida 'verify_peer_name' => false (StackOverflow, Github).

Lisaks asendasin (peamiselt Ôppimise eesmÀrgil) Query Builder andmebaasipÀringud Eloquent ORM pÀringutega, mis eeldatavasti vÔiksid olla kiiremad (eeldusel, et Eloquent oskab natuke nutikama pÀringu kokku panna kui ma ehk ise oskaks) ja paremini loetavad (kuigi nad on kohati pÀris Ôudsed). NÀiteks selline nÀeb vÀlja rubriigi pÀring.

PostsController show method query.

TĂ€iendasin mudeleid seostega (et Eloquent neid kasutada oskaks).

Kui .NETis (koolis) arendus kĂ€is nii, et teed mudeli+seosed ja mudeli pĂ”hjal scaffoldid migrationi (me enamasti isegi ei sĂŒvenenud, mida ĂŒldse toodeti sinna), siis Laravelis teen mĂ”lemad kĂ€sitsi ja nad ĂŒksteisest midagi ei tea. Migration on ainult andmebaasi muudatusteks (mÀÀrad baasis indeksid ja seosed). Model on Eloquenti pĂ€ringute jaoks ja ta ei tea midagi sellest, mis baasis toimub.

Lisaks kirjutasin PostsSeeder‘i, mis Fakeri abil teeb kiiresti palju postitusi, et saaksin vaadata, kuidas rakendus 100k vĂ”i miljoni postitusega töötab. Arenduse andmebaas on Dockeri konteinerina mul pisikeses Hetzneri CPX11 purgis (2 vCPU, 2GB RAM ja 40GB ketast).

Kellaaegadega tegelesin ka. Avastasin, et blogide RSS annab UTC ajad. SellepÀrast vÔis jÀÀda mulje, et Eestis on vÀga palju varahommikusi blogijaid aga tegelt oli lihtsalt kellaaeg vale. Need on vaja kuidagi Europe/Tallinn aegadeks saada. Lahendasin hetkel nii, et Carbon teeb need mallis korda aga ma sellega lÔpuni rahul ei saa olla, sest mul on andmebaasis kuupÀev ja kellaaeg eraldi. Natuke pean veel nuputama, kuidas seda veakindlalt lahendada.

11. Aug 2022 |

Weird stuff in Laravel

Laravel migrations run in the alphabetical order of their file name

“General error: 1005 Can’t create table [..] Foreign key constraint is incorrectly formed”

This happened when the migration of my in-between table ran before the table its FKs were linked to. First I thought I can not use Laravel’s foreign key constraints but at some point I figured it out. Renaming the migration file helped.

21. Jul 2022 |

Eestiblogid.eu uuendus

Kirjutasin eestiblogid.eu lehe ĂŒmber Laraveli raamistikule. Kasutajaliidese jaoks hetkel kasutusel blade mallid (hea mugav kasutada). VĂ€ga suurt Ă€riloogikat ei teinud aga vĂ€hemalt viewmodelid pĂŒĂŒdsin ikka kasutusele vĂ”tta.

Reader (ehk see tĂŒkk, mis blogisid loeb) on Laravelist eraldi. Endiselt lihtsakoeline php rakendus (index.php, functions.php ja kaks mudelit) aga pĂŒĂŒdsin valmisteeke kasutada seekord. Kasutusele said vĂ”etud laminas/laminas-feed, laminas/laminas-http, doctrine/dbal (docs). DI implementeerimisest (php-di abil) kahjuks jĂ”ud ĂŒle ei kĂ€inud. EesmĂ€rk olnuks andmebaasiĂŒhenduste kasutust viisakamaks muuta 🙂

Asub Zone serveris, minu muude asjade hulgas. Laraveli deployment sinna on vÀga mugav. Pmst repo fetch ja pull privaatses kaustas. Rakenduse kiirus on pÀris normaalne. Andmebaas on hetkel veel arendusserveris (pisike Hetzneri VPS) aga tÔstan vasti Zonesse ringi.

Andmebaasi kolimisel Zone’sse lĂ€ksid tĂ€hed katki.

Andmebaasi kolimisel tekkis selline viga (mida arenduskeskkonnas ei esinenud). Esialgu oli vaja kohendada Doctrine‘i ja lisada selle konfiguratsiooni charset (mille valik vajas natuke otsimist, kasutusele lĂ€ks utf8mb4). Selle tulemusel sai andmebaasi kirjutamise esmapilgul korda. SeejĂ€rel lĂ€ks katki Laravel. PĂ”hjuseks see, et andmebaasitabelite collation ei olnud see, mida Laravel (enda konfuguratsioonis) kasutab. Tabeli collation’i muutmisel lĂ€ks Laravel ka korda. Arendamise ajal (kuna arenduskeskkonna baasis vigu ei esinenud) ei osanud nendele asjadele tĂ€helepanu pöörata.

6. Feb 2022 |

Eestiblogid.eu – my hobby project

To improve my coding skills in a safe environment I should start working more on the hobby project eestiblogid.eu. I currently has ~150 weekly users so if I mess something up it should not be huge problem 🙂

Currently it is just 2 small services which I call Reader and Writer.

Reader is a small independent PHP application which basically reads the given list of XML files (RSS feeds) every hour and checks if there is anything new. If there is something new then entry is made to the database. Rules for detecting “new” are very primitive. Therefore, quite often you can see duplicate posts. Reader will probably stay as it is. I should just make the code cleaner from time to time, implement some logging etc.

Writer is also a small php application (using Twig for templating and Bootstrap for styling) to generate views for the front end and reading data from database.

There is no admin panel at the moment. I go straight to the database to edit things (remove, deactivate, delete etc).

Up next

What I would like to do now is to prepare REST endpoints for the front end. Probably it will be php backend built with Laravel or Lumen using their Eloquent ORM.

Front end should be built with React together with admin views for myself (using JWT for authentication).

I try to keep you informed of the progress under this eestiblogid.eu tag.

Update 09.03.22

It has been less than a month. I have not done anything (too busy with my internship). But, meanwhile, Lumen has become extinct (not being developed any further) and I am most probably considering GraphQL (with Lighthouse) insead of REST. So probably it is good that I have not done much.