Prerequisites :

-PHP 7.3

-Visual Studio Code

  1. install slim via composer

//run this command on terminal composer require slim/slim:”3.*”

2. install phpUnit

//run this command on terminal

composer require — dev phpunit/phpunit ^7

Now phpUnit is successfully installed you can see in demo/composer.json file and in the vendor folder.

3. Now we have to create demo/phpunit.xml configuration file which is used to configure the PHPUnit

bootstrap=”vendor/autoload.php” –this is used to autoload the library. Colors=”true”- it is used to add the color to the command line.

Verbose=”true”-for showing more information about your test.

stopOnFailure = “false”- the test executed sequentially if any test fails then it stops the execution of the test.

<directory>tests</directory>-this is a tests directory where I wrote all my test cases.

4. Now we have to create our demo/tests folder in our project.

5. Now create the demo/src folder inside the src folder you have to create a file name routes.php in which you will write your Api.

In routes.php file I have created an Api for addition of two numbers.

6. Now we have to setup the autoload to autoload everything from the tests directory in composer.json file.

Write “autoload-dev”: { “psr-4”: { “Tests\\”: “tests/” } } this in your composer.json file And run command-

composer dump-autoload –o

7. Now coming to the demo/tests folder inside tests folder we have to create a Functional folder inside that I have created a BaseTestCase class which is extended from TestCase class.

Inside BaseTestCase class , we have created runApp function passing the $requesMethod(POST,GET),$requestUri(api name),$requestData(array of data), inside this function I have created mock environment objects with custom information. Mock Environment objects are only useful when writing unit tests.after that I have setting up the $request = Request::createFromEnvironment($environment) object.Now adding the requested data if settingup the response object.create an object $app = new App();

After that process the application $response = $app->process($request, $response) And return the response.

8. Now create the file for testing out api demo/tests/Functional/routesApiTest

In this we have created a class routesApiTest extended from BaseTestCase class If you are writing test case then you always have to write test as a prefix of the function like testaddNum or you can write as I wrote in my case /** * @test */.now create a function addNum inside this function I have called runApp method in which I am passing the 3 parameters first-method which is post,second-api name,third-data array.Now I am using assertEquals mathods in first parameter is what I expect and second parameter $response->getBody() what it returns.

9. Now run the command in terminal

./vendor/bin/phpunit tests


Originally published at on Aug 28, 2021.




Devstringx Technologies is highly recommended IT company for custom software development, mobile app development and automation testing services

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

AWS CI-CD Dynamic Build Badge Display on Github

Securely Using Snowflake’s Python Connector within an Azure Function

Best Coding IDE in 2022

Demystifying Workflow Attachment in IBM FileNet

Quick Guide For Flights APIs In 2022

2017 — The Year ECM Died

When is an audit not an audit ?

What Would it be Gartner Bimodal IT? #40

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
Devstringx Technologies

Devstringx Technologies

Devstringx Technologies is highly recommended IT company for custom software development, mobile app development and automation testing services

More from Medium

RxSwift: Combining Operators in Practice

How to configure SonarLint to connect to SonarQube for VS Code


Multiple SSH keys for multiple Bitbucket/GitHub accounts