PHP code example of lucinda / unit-testing

1. Go to this page and download the library: Download lucinda/unit-testing library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lucinda / unit-testing example snippets





    new Lucinda\UnitTest\ConsoleController(__DIR__."/unit-tests.xml", "local");
} catch (Lucinda\UnitTest\Exception $e) {
    echo $e->getMessage().PHP_EOL;
}

new Lucinda\UnitTest\ConsoleController(
    __DIR__."/unit-tests.xml",
    "local",
    Test\App\Service\UserServiceTest::class
);


namespace Test\App\Service;
    
class UserServiceTest
{
    public function create()
    {
    }
}


namespace Test\App\Service;

use App\Service\UserService;
use Lucinda\UnitTest\Result;
use Lucinda\UnitTest\Validator\Arrays;
use Lucinda\UnitTest\Validator\Strings;

class UserServiceTest
{
    public function create(): Result
    {
        $service = new UserService();
        $id = $service->create("[email protected]");

        return (new Strings($id))->assertNotEmpty("Created user id should not be empty");
    }

    /**
     * @return Result[]
     */
    public function list(): array
    {
        $service = new UserService();
        $users = $service->list();

        return [
            (new Arrays($users))->assertNotEmpty("Users list should not be empty"),
            (new Arrays($users[0]))->assertContainsKey("email", "First user should expose email")
        ];
    }
}

new Lucinda\UnitTest\Result(true, "optional message");

return (new Lucinda\UnitTest\Validator\Integers($count))->assertGreater(0);

return (new Lucinda\UnitTest\Validator\Files(__DIR__."/fixture.json"))
    ->assertContains('"enabled":true');

use Lucinda\UnitTest\Result;
use Lucinda\UnitTest\Validator\Integers;
use Lucinda\UnitTest\Validator\SQL;
use Lucinda\UnitTest\Validator\SQL\ResultValidator;

return SQL::getInstance()->assertPreparedStatement(
    "SELECT COUNT(*) FROM users WHERE active = :active",
    [":active" => "1"],
    new class implements ResultValidator {
        public function validate(\PDOStatement $statementResults): Result
        {
            return (new Integers((int) $statementResults->fetchColumn()))->assertGreater(0);
        }
    }
);

use Lucinda\UnitTest\Result;
use Lucinda\UnitTest\Validator\Integers;
use Lucinda\UnitTest\Validator\URL;
use Lucinda\UnitTest\Validator\URL\DataSource;
use Lucinda\UnitTest\Validator\URL\Response;
use Lucinda\UnitTest\Validator\URL\ResultValidator;

$dataSource = new DataSource("https://example.com/health");
$dataSource->setRequestMethod("GET");
$dataSource->addRequestHeader("Accept", "application/json");

return (new URL($dataSource))->assert(
    new class implements ResultValidator {
        public function validate(Response $response): Result
        {
            return (new Integers($response->getStatus()))->assertEquals(200);
        }
    }
);
console
php test.php
text
src/Service/UserService.php      App\Service\UserService
tests/Service/UserServiceTest.php Test\App\Service\UserServiceTest