PHP code example of radziuk / php-tt

1. Go to this page and download the library: Download radziuk/php-tt 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/ */

    

radziuk / php-tt example snippets

 vendor/radziuk/php-tt/bin/run.php
 vendor/radziuk/php-tt/bin/lararun.php
 vendor/radziuk/php-tt/bin/run.php src/lib

    /**
     * @php-tt-assert "hello" >>> "hello"
     */
    public function replaceMarkers(string $string): string


    /**
     * @php-tt-assert "'hello', 'world'" >>> "#1, #2"
     */
    public function replaceMarkers(string $string): string

    /**
     * @php-tt-assert "'hello', 'world'", 1 >>> "#1, 'world'"
     */
    public function replaceMarkers(string $string, int $count): string


    /**
     * @php-tt-assert "'hello', 'world'", ['hello' => 'world'] >>> "#1, 'world'"
     */
    public function replaceMarkers(string $string, array $data): string


    /**
     * @php-tt-before $object->setPattern('#%s')
     * @php-tt-assert "'hello', 'world'", ['hello' => 'world'] >>> "#1, 'world'"
     */
    public function replaceMarkers(string $string, array $data): string


    /**
     * @php-tt-mock $this->getFilenameFromDatasource >>> 'test.php'
     * @php-tt-assert 'Any.key', 'default' >>> ['default/test.php']
     */
    private function getFileForDataSource(string $dataSource, string $methodName): array
    {
        $dataDir = $this->getDataDirForMethod($methodName);
        $fileName = $this->getFilenameFromDatasource($dataSource);
        $file = $dataDir . '/' . $fileName;
        return [$file];
    }


    /**
     * @php-tt-mock $this->service->getFilenameFromDatasource >>> 'test.php'
     * @php-tt-assert 'Any.key', 'default' >>> ['default/test.php']
     */
    private function getFileForDataSource(string $dataSource, string $methodName): array
    {
        $dataDir = $this->getDataDirForMethod($methodName);
        $fileName = $this->service->getFilenameFromDatasource($dataSource);
        $file = $dataDir . '/' . $fileName;
        return [$file];
    }


    /**
     * @php-tt-mock $service->getFilenameFromDatasource >>> 'test.php'
     * @php-tt-mock getDataDirForMethod >>> 'default'
     * @php-tt-assert 'Any.key', 'default' >>> ['default/test.php']
     */
    private function getFileForDataSource(string $dataSource, string $methodName): array
    {
        $dataDir = getDataDirForMethod($methodName);
        $fileName = $service->getFilenameFromDatasource($dataSource);
        $file = $dataDir . '/' . $fileName;
        return [$file];
    }


    /**
     * @php-tt-mock @$service->getFilenameFromDatasource >>> 'test.php'
     * @php-tt-mock $this->data_dir >>> '/var/www'
     * @php-tt-assert 'Any.key', 'default' >>> ['/var/www/test.php']
     */
    private function getFileForDataSource(string $dataSource, string $methodName): array
    {
        $dataDir = $this->data_dir;
        $fileName = $service->getFilenameFromDatasource($dataSource);
        $file = $dataDir . '/' . $fileName;
        return [$file];
    }


    /**
     * @php-tt-mock self::getFilenameFromDatasource >>> 'test.php'
     * @php-tt-exact-mock tFileForDataSource(string $dataSource, string $methodName): array
    {
        $dataDir = 



    return [

        'getFileForDataSource' => [
            0 => [
                ['DoTest.test', 'methodName'],//parameters as array
                'test',//expected result
            ],
            1 => [
                ['DoTest', 'methodName'],
                'methodName'
            ]
        ],
];

    /**
     * @php-tt-mock @self::getFilenameFromDatasource >>> 'test.php'
     * @php-tt-exact-mock ring $methodName): array




    return [

        '__my_custom_key__' => [
            0 => [
                ['DoTest.test', 'methodName'],//parameters as array
                'test',//expected result
            ],
            1 => [
                ['DoTest', 'methodName'],
                'methodName'
            ]
        ],
];

    /**
     * @php-tt-mock self::getFilenameFromDatasource >>> 'test.php'
     * @php-tt-exact-mock ing $dataSource, string $methodName): array


    /**
     * @php-tt-mock self::getFilenameFromDatasource >>> 'test.php'
     * @php-tt-exact-mock tring $methodName): array



    
    return [

        'my_mock' => (function(){
            return 'hello';
        })(),
];

    /**
     * @php-tt-assert-exception "'hello', 'world'"
     */
    public function replaceMarkers(string $string): string

    /**
     * @php-tt-assert-exception "'hello', 'world'" >>> \App\My\Exception::class
     */
    public function replaceMarkers(string $string): string

    /**
     * @php-tt-assert-exception-contains "'hello', 'world'" >>> "My error text"
     */
    public function replaceMarkers(string $string): string

    /**
     * @php-tt-assert-contains "'hello', 'world'" >>> "My error text"
     */
    public function replaceMarkers(string $string): string

    /**
     * @php-tt-assert-preg "'hello', 'world'" >>> "/^.*$/"
     */
    public function replaceMarkers(string $string): string

    /**
     * @php-tt-assert-callable "hello, world" >>> [#TestData.my_callable, 'expected result']
     */
    public function replaceMarkers(string $string): string

return [
    'my_callable' => function(stdClass $result, $expected):bool {
        return $result->property === $expected;
    },
];


    /**
     * @php-tt-assert-callable "hello, world" >>> [#TestData.my_callable, 'expected result', false]
     */
    public function replaceMarkers(string $string): string

return [
    'my_callable' => function(stdClass $result, $expected, $true = true):bool {
        return $true ? $result->property === $expected : $result->property !== $expected;
    },
];


    /**
     * @php-tt-alias "property-equals" >>> #TestData.property_equals
     * @php-tt-alias "property-not-equals" >>> #TestData.property_not_equals
     * use your aliases
     * @php-tt-assert-property-equals 'parameter' >>> 'expected'
     * @php-tt-assert-property-not-equals 'parameter' >>> 'expected'
     */
    public function replaceMarkers(string $string): string

return [
    'property_equals' => function(stdClass $result, $expected):bool {
        return $result->property === $expected;
    },
    'property_not_equals' => function(stdClass $result, $expected):bool {
        return $result->property !== $expected;
    },
];

 vendor/radziuk/php-tt/bin/run.php app tests/my-folder
 vendor/radziuk/php-tt/bin/run.php 3
 vendor/radziuk/php-tt/bin/run.php custom/app 3
 vendor/radziuk/php-tt/bin/run.php custom/app custom/data 3



 new \Aradziuk\PhpTT\Tt();

$tt->run(
    __DIR__ . '/app', //dir with your classes
    __DIR__ . '/test/php-tt' // dir with your data
);

    trait MyTrait {
    /**
     * @php-tt-use-class My\Namespace\CustomClass
     * the above command will tell php-tt to use the object of My\Namespace\CustomClass for testing this method. The class should use the trait 
     * @php-tt-assert "#1, #2" >>> 'hello, world'
     */
    public function replaceMarkers(string $string): string
 vendor/radziuk/php-tt/bin/lararun.php
 artisan make:command PhpTT

    $tt = new \Radziuk\PhpTT\Tt();
    $tt->setOutputCallback('info', function (string $string) {
        $this->info($string);
    })->setOutputCallback('error', function (string $string){
        $this->error($string);
    })->setOutputCallback('alert', function (string $string){
        $this->alert($string);
    }); // use artisan generic out put
    
    $tt->run(
        app_path(),
        base_path('tests/php-tt-data')
    );

    
    protected $commands = [
        PhpTT::class,
    ];

    \Radziuk\PhpTT\Tt::enhance('greater-than', function(\ReflectionMethod $method, $object, array $params, $expected): array
    {
        $result = $method->invoke($object, ...$params);
        return [$result > $expected, $result];
    });

    /**
     * @php-tt-assert-greater-than 2, 2 >>> 3
     */
    public function multiply(int $x, int $y): int

php vendor/radziuk/php-tt/bin/selftest.php

    touch tests/php-tt-data/TestData.php
touch testrunner.php

php artisan app:php-tt