PageRenderTime 53ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/getting-started/php/index.md

https://github.com/dchester/rockplatform.github.com
Markdown | 181 lines | 115 code | 66 blank | 0 comment | 0 complexity | b2f0121c5f12711cc5abeaeb24ffc9dc MD5 | raw file
  1. ---
  2. layout: default
  3. title: Getting Started with PHP
  4. ---
  5. # Getting Started with PHP
  6. This guide walks you through setting up a basic PHP web application with tests.
  7. 1. Create and switch to project directory
  8. $ mkdir php-example
  9. $ cd php-example
  10. 1. Create `.rock.yml`
  11. runtime: php54
  12. 1. Create `php.ini`
  13. extension=dom.so
  14. 1. Create `composer.json`
  15. {
  16. "require": {
  17. "EHER/PHPUnit": "*",
  18. "slim/slim": "*"
  19. }
  20. }
  21. 1. Build project
  22. $ rock build
  23. 1. Create `public` directory
  24. $ mkdir public
  25. 1. Create `public/web.php`
  26. <?php
  27. require __DIR__ . '/../vendor/autoload.php';
  28. function greeting() {
  29. return 'Hello World';
  30. }
  31. function app() {
  32. $app = new Slim();
  33. $app->get('/', function () {
  34. echo greeting();
  35. });
  36. return $app;
  37. }
  38. 1. Create `public/index.php`
  39. <?php
  40. require __DIR__ . '/web.php';
  41. app()->run();
  42. 1. Create `bin` directory
  43. $ mkdir bin
  44. 1. Create `bin/hello-world`
  45. #!/usr/bin/env bash
  46. exec php -S "${HTTP_HOST-127.0.0.1}:${HTTP_PORT-8000}" -c "${PROJECT_PATH}/php.ini" -t "${PROJECT_PATH}/public"
  47. 1. Make it executable
  48. $ chmod 755 bin/hello-world
  49. 1. Start `hello-world` and kill it using Ctrl+C
  50. $ rock run hello-world
  51. PHP 5.4.4 Development Server started at Wed Aug 1 18:00:00 2012
  52. Listening on 127.0.0.1:8000
  53. Document root is /home/vagrant/php-example/public
  54. Press Ctrl-C to quit.
  55. ^C
  56. 1. Update `.rock.yml` to include a run alias that defaults to port 9000
  57. runtime: php54
  58. run: HTTP_PORT=${HTTP_PORT-9000} hello-world
  59. 1. Run and kill it using Ctrl+C
  60. $ rock run
  61. PHP 5.4.4 Development Server started at Wed Aug 1 18:05:00 2012
  62. Listening on 127.0.0.1:9000
  63. Document root is /home/vagrant/php-example/public
  64. Press Ctrl-C to quit.
  65. ^C
  66. 1. Create `phpunit.xml`
  67. <phpunit bootstrap="vendor/autoload.php">
  68. <testsuites>
  69. <testsuite name='Simple tests'>
  70. <directory suffix='.php'>./tests</directory>
  71. </testsuite>
  72. </testsuites>
  73. </phpunit>
  74. 1. Create `tests` directory
  75. $ mkdir tests
  76. 1. Create `tests/basic.php`
  77. <?php
  78. require __DIR__ . '/../public/web.php';
  79. class Test extends PHPUnit_Framework_TestCase {
  80. public function testGreeting() {
  81. $this->assertEquals('Hello World', greeting());
  82. }
  83. }
  84. 1. Run tests
  85. $ rock test
  86. PHPUnit 3.6.10 by Sebastian Bergmann.
  87. Configuration read from /home/vagrant/php-example/phpunit.xml
  88. .
  89. Time: 0 seconds, Memory: 2.75Mb
  90. OK (1 test, 1 assertion)
  91. 1. Update `.rock.yml` to include a simple frontpage test
  92. runtime: php54
  93. run: HTTP_PORT=${HTTP_PORT-9000} hello-world
  94. test_frontpage: |
  95. # start server
  96. rock run &>/dev/null &
  97. # give it a second to start
  98. sleep 1
  99. # get frontpage body
  100. body="$( curl -s 'http://127.0.0.1:9000/' )"
  101. # kill server
  102. kill %1
  103. # check response body
  104. if [[ "$body" != 'Hello World' ]]; then
  105. die "ERROR: '$body' != 'Hello World'"
  106. else
  107. echo 'OK'
  108. fi
  109. 1. Run `frontpage` tests
  110. $ rock test frontpage
  111. OK
  112. 1. Clean project root, run deployment build and run tests to ensure build worked
  113. $ rock clean
  114. $ rock build deployment
  115. $ rock test