PHP code examples

Updated: 12 March 2024

Read contents of text file into a variable

$file_pointer = fopen('raw_bb_sample.txt', 'r') or die('Unable to open file!');
$sample = fread($file_pointer, filesize('raw_bb_sample.txt'));
fclose($file_pointer);
var_dump($sample);

Symfony testing

Updated: 10 December 2024

See https://symfony.com/doc/current/testing.html

Unit Tests

  1. Test a Class.
  2. Test a method in a Class.
  3. Equivalent to standard PHPUnit unit tests.
  4. Does not hit the database – use mocks instead.

Integration Tests

  1. Tests a larger part of the app than Unit tests e.g. a combination of Services.
  2. Might use the Kernel to fetch services from the dependency injection container.
  3. Integration test classes should extend KernelTestCase.
  4. May involve mocking of Class dependencies.
  5. May interact with a test database, populated via fixtures.

Application Tests

  1. Checks integration of all layers of the app, from routing to views, excluding on page javascript.
  2. Has a specific workflow:
    1. Make request.
    2. Interact with page.
    3. Test response.
  3. Probably won’t involve mocking dependencies.
  4. Probably will interact with a test database.
  5. Integration test classes should extend WebTestCase.

End to End Tests

  1. Test the application as a whole, including on page JavaScript code.
  2. Involves a real browser instead of the test client.
  3. See Symfony Panther component.

The output of make:test supplies a useful summary

root@5b8333e33939:/var/www/html# bin/console make:test

 Which test type would you like?:
  [TestCase       ] basic PHPUnit tests
  [KernelTestCase ] basic tests that have access to Symfony services
  [WebTestCase    ] to run browser-like scenarios, but that do not execute JavaScript code
  [ApiTestCase    ] to run API-oriented scenarios
  [PantherTestCase] to run e2e scenarios, using a real-browser or HTTP client and a real web server

Composer tool itself

Updated: 27 July 2024

Install Composer phar

curl -sS https://getcomposer.org/installer | php

Circumvent php memory limit

php -d memory_limit=-1 composer.phar install

Up / downgrade Composer itself

composer self-update --help
composer self-update --1
composer self-update --2

Composer in Docker

Updated: 17 January 2023

Require a new project as a dependency

docker run --rm \
    -v $(pwd):/app \
    composer/composer:latest \
    require slim/slim "^3.0"

Install all dependencies in a project

docker run --rm --interactive --tty --volume $PWD:/app composer install

PHP in Docker

Updated: 17 May 2024

Pre-configured Docker images (including Xdebug) from Jetbrains https://github.com/JetBrains/phpstorm-docker-images


Run a php script

docker run -it --rm --name my-running-script \
-v "$PWD":/usr/src/myapp \
-w /usr/src/myapp \
php:7.4-cli php script.php

Run the php development server

docker run -it --rm --name php-server \
-v "$PWD":/usr/src/myapp \
-w /usr/src/myapp \
-p 8000:8000 \
php:8.2-cli php -S 0.0.0.0:8000