CRUD Unit Testing in Laravel 5

Part I: Positive Unit Testing

Let’s start with the CREATE test.

PHPUnit 6.5.7 by Sebastian Bergmann and contributors.E                                                                   1 / 1 (100%)Time: 700 ms, Memory: 26.00MBThere was 1 error:1) Tests\Unit\Carousels\CarouselUnitTest::it_can_create_a_carousel
Error: Class 'Tests\Unit\Carousels\CarouselRepository' not found
PHPUnit 6.5.7 by Sebastian Bergmann and contributors.E                                                                   1 / 1 (100%)Time: 898 ms, Memory: 26.00MBThere was 1 error:1) Tests\Unit\Carousels\CarouselUnitTest::it_can_create_a_carousel
App\Shop\Carousels\Exceptions\CreateCarouselErrorException: PDOException: SQLSTATE[HY000]: General error: 1 no such table: carousels in /Users/jsd/Code/shop/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:77
PHPUnit 6.5.7 by Sebastian Bergmann and contributors..                                                                   1 / 1 (100%)Time: 696 ms, Memory: 26.00MBOK (1 test, 6 assertions)

Show the carousel after it is created

Now, lets try to test the READ.

PHPUnit 6.5.7 by Sebastian Bergmann and contributors.E                                                                   1 / 1 (100%)Time: 688 ms, Memory: 26.00MBThere was 1 error:1) Tests\Unit\Carousels\CarouselUnitTest::it_can_show_the_carousel
InvalidArgumentException: Unable to locate factory with name [default] [App\Shop\Carousels\Carousel].
PHPUnit 6.5.7 by Sebastian Bergmann and contributors.E                                                                   1 / 1 (100%)Time: 708 ms, Memory: 26.00MBThere was 1 error:1) Tests\Unit\Carousels\CarouselUnitTest::it_can_show_the_carousel
Error: Call to undefined method App\Shop\Carousels\Repositories\CarouselRepository::findCarousel()
$carousel = factory(Carousel::class)->create();
$carousel = factory(Carousel::class)->make();
PHPUnit 6.5.7 by Sebastian Bergmann and contributors..                                                                   1 / 1 (100%)Time: 932 ms, Memory: 26.00MBOK (1 test, 6 assertions)

Update Carousel test

PHPUnit 6.5.7 by Sebastian Bergmann and contributors..                                                                   1 / 1 (100%)Time: 932 ms, Memory: 26.00MBOK (1 test, 6 assertions)

Finally, we test the delete

➜ git: phpunit --filter=CarouselUnitTest::it_can_delete_the_carousel
PHPUnit 6.5.7 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)Time: 916 ms, Memory: 26.00MBOK (1 test, 1 assertion)

--

--

Senior Fullstack Engineer, React Native developer and HL7 Health Informatics professional

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jeff Simons Decena

Senior Fullstack Engineer, React Native developer and HL7 Health Informatics professional