Simplest way to run/test a php function in Laravel-4 ( Helloworld unit testing! )

Install phpunit:
1. Go to the root directory of your project (where the 'artisan' file is located).
2. Open 'composer.json. Include phpunit dependency in the JSON.
 Save and close the file. After adding the file content should look like:
  1. {
  2. "name": "laravel/laravel",
  3. "description": "The Laravel Framework.",
  4. "keywords": ["framework", "laravel"],
  5. "license": "MIT",
  6. "require": {
  7. "laravel/framework": "4.0.*",
  8. "geoip/geoip": "~1.14"
  9. },
  10. "require-dev": {
  11. "phpunit/phpunit": "3.7.*"
  12. },
  13. "autoload": {
  14. "classmap": [
  15. "app/commands",
  16. "app/controllers",
  17. "app/models",
  18. "app/database/migrations",
  19. "app/database/seeds",
  20. "app/tests/TestCase.php"
  21. ]
  22. },
  23. "scripts": {
  24. "post-install-cmd": [
  25. "php artisan optimize"
  26. ],
  27. "post-update-cmd": [
  28. "php artisan clear-compiled",
  29. "php artisan optimize"
  30. ],
  31. "post-create-project-cmd": [
  32. "php artisan key:generate"
  33. ]
  34. },
  35. "config": {
  36. "preferred-install": "dist"
  37. },
  38. "minimum-stability": "dev"
  39. }

3. Run the following command in that directory :
 
  1. composer update

 and wait for completion.

4. Now after that run the command "vendor\bin\phpunit" ("vendor/bin/phpunit" for linux)
 for running default tests
On Windows run:
  1. vendor\bin\phpunit
On Linux run:
  1. vendor/bin/phpunit
You will see something like this:


PHPUnit 3.7.29 by Sebastian Bergmann.

Configuration read from YOUR_PROJECT_DIR\phpunit.xml

.

Time: 188 ms, Memory: 5.50Mb

←[30;42m←[2KOK (1 test, 1 assertion)
←[0m←[2K

7. Now go to the directory YOUR_PROJECT_DIR/app/tests/
8. Open the file 'ExampleTest.php'. It's content should look something like this:

  1. <?php
  2. class ExampleTest extends TestCase {
  3. /**
  4. * A basic functional test example.
  5. *
  6. * @return void
  7. */
  8. public function testBasicExample()
  9. {
  10. $crawler = $this->client->request('GET', '/');
  11. $this->assertTrue($this->client->getResponse()->isOk());
  12. }
  13. }
  14. 7. Now write your own test function and call it in testBasicExample():
  15. class ExampleTest extends TestCase {
  16. /**
  17. * A basic functional test example.
  18. *
  19. * @return void
  20. */
  21. public function testBasicExample()
  22. {
  23. $crawler = $this->client->request('GET', '/');
  24. $this->assertTrue($this->client->getResponse()->isOk());
  25. self::my_test();//your method
  26. }
  27. public function my_test()
  28. {
  29. echo 'Hi I want to show you some test results :)';
  30. }
  31. }

8. Save and close the file and run again 'vendor\bin\phpunit'.And you will see output like this:

PHPUnit 3.7.29 by Sebastian Bergmann.

Configuration read from F:\laravel\laravelScrapbook\phpunit.xml

.Hi I want to show you test results :)

Time: 172 ms, Memory: 5.50Mb

←[30;42m←[2KOK (1 test, 1 assertion)
←[0m←[2K

No comments:

Post a Comment