datikken / zandstra

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PHP 8 Objects, Patterns, and Practice

Source Code

This directory contains the source code for PHP 8 Objects, Patterns, and Practice as well as some fairly rudimentary tests.

Running tests

In order to run the tests you will need to install PHPUnit. The easiest way to do this is to use composer (The composer.json and composer.lock files are also provided).

$ composer install

This will install PHPUnit locally. It will also configure autoload. You can then run the tests:

./vendor/bin/phpunit test/

Finding a code example

In the book, code examples are often presented as fragments or with details omitted. This is not the case in these files. Individual code examples are marked within runnable code using comments. For example here is an extract from src/ch13/batch01/SpaceMapper.php which contains code examples 13.16 and 13.17. This trick allows me to unit test the examples. I use some handrolled tools to generate and embed the code examples.

<?php

declare(strict_types=1);

namespace popp\ch13\batch01;

class SpaceMapper extends Mapper
{

    // ...

/* listing 13.16 */

        // SpaceMapper::__construct()

        $this->selectAllStmt = $this->pdo->prepare(
            "SELECT * FROM space"
        );
        $this->findByVenueStmt = $this->pdo->prepare(
            "SELECT * FROM space WHERE venue=?"
        );
/* /listing 13.16 */
    }

/* listing 13.17 */
    public function getCollection(array $raw): SpaceCollection
    {
        return new SpaceCollection($raw, $this);
    }
/* /listing 13.17 */

    protected function doCreateObject(array $raw): Space
    {
        $obj = new Space(
            (int)$raw['id'],
            $raw['name']
        );

    // ...

Of course, using comments makes it hard to locate an individual code example. For this reason I have included a contents file (named contents.txt). This is also autogenerated by a production tool, and should make it a breeze to find a particular example. Here for example is an extract that shows you how to find some examples in Chapter 13:

13.13: 
    src/ch13/batch01/Mapper.php
13.14: 
    src/ch13/batch01/SpaceMapper.php
13.15: 
    src/ch13/batch01/SpaceMapper.php
13.16: 
    src/ch13/batch01/SpaceMapper.php
13.17: 
    src/ch13/batch01/Venue.php
13.18: 
    src/ch13/batch03/Runner.php
13.19: 
    src/ch13/batch03/ObjectWatcher.php
13.20: 
    src/ch13/batch03/Mapper.php
13.21: 
    src/ch13/batch01/SpaceMapper.php

About

License:Other


Languages

Language:PHP 99.7%Language:Shell 0.2%Language:Hack 0.1%Language:HTML 0.0%