22. Oct 2022 |

Laravel associative array to collection for unit test

I need to test helper method that accepts Illuminate\Database\Eloquent\Collection as a parameter. But I can not figure out how to prepare it inside a unit test. My logic says it should be simple to create Collection from assoc array. But I just can’t get it to work.

At the moment, not to get stuck, I will probably use feature testing (because if feature (API endpoint) works then probably my helper method also works). Though, it feels stupid workaround.

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.

12. Oct 2022 |

php versions

As I use composer packages more and more I need to switch between php versions used in terminal. Some notes here for myself.

  • which php – where php is installed
  • php -v – which php is currently in use in terminal
  • ls -la /usr/bin/php* – which php versions are installed in the system

Install php on Ubuntu

sudo update-alternatives --config php – see options and switch between versions.

For installation I took commands from this instruction.

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Install php7.4 with few common PHP modules (also Digital Ocean post)

sudo apt-get install -y php7.4 php7.4-cli php7.4-json php7.4-common php7.4-mysql php7.4-zip php7.4-gd php7.4-mbstring php7.4-curl php7.4-xml php7.4-bcmath

Digital Ocean gives following suggestions for packages:

sudo apt-get install -y php8.1 php8.1-cli php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl php8.1-xml php8.1-bcmath

For uninstalling just use:

sudo apt remove php7.4 etc or the php -m to list all loaded PHP modules.

sudo apt autoremove

Composer broke down

StackOverflow solution and install composer the old way.

10. Oct 2022 |

XPath

Some wp allimport XPath filters for myself.

[sku[1] = "WT806"]
[brand[1] = "WOLF"]
[BrandName[1] = "TRIANGLE" and ImageId[1][string()]]

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.

16. Aug 2022 |

Ubuntu 22.04.1 LTS – Chrome is crashing, external drives don’t mount

I updated from the previous Ubuntu to the latest 22.04.1 LTS (Jammy Jellyfish). Having some issues now.

Chrome is crashing on Ubuntu 22.04.1 LTS

Chrome browser is crashing very often. Window just becomes non-responsive. I can close the window and open new ones but not use the current one.

External drives do not mount

Problem with mounting Windows partition and external harddrive.

Unable to access location. Error mounting [..] wrong fs type, bad option, bad superblock on [..], missing codepage or helper program, or other error.”

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.