PHPackages                             radziuk/php-tt - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. radziuk/php-tt

ActiveLibrary[Testing &amp; Quality](/categories/testing)

radziuk/php-tt
==============

Docblock unit tests library, unit test methods of your class directly in your doc block

0.3.0(2y ago)050MITPHP

Since Feb 14Pushed 2y ago1 watchersCompare

[ Source](https://github.com/radziuk/php-tt)[ Packagist](https://packagist.org/packages/radziuk/php-tt)[ RSS](/packages/radziuk-php-tt/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (17)Used By (0)

Install the package

`composer require --dev radziuk/php-tt`

Run tests

`php vendor/radziuk/php-tt/bin/run.php`

Use Laravel integrated runner

`php vendor/radziuk/php-tt/bin/lararun.php`

---

### Specify the folder

[](#specify-the-folder)

By default the runner uses the "app" folder to look for tests. You can specify a custom folder

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

### Self-test the package

[](#self-test-the-package)

After the package is install you can run the selftest script included in the package

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

```

Should input something like this:

```
Info: Assertions done: 89
Info: Assertions true: 89

```

### Test examples

[](#test-examples)

Simple test, pass "hello" to the function, should return "hello"

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

Text example #2

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

Text example #3

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

Text example #4, passing an array

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

Text example #5, calling a method on the object before testing

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

Text example #6, mocking $this-&gt;... method

```
    /**
     * @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];
    }
```

Text example #7, mocking method of a dependency

```
    /**
     * @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];
    }
```

Text example #8, mocking global functions and methods

```
    /**
     * @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];
    }
```

Text example #9, mocking a property

```
    /**
     * @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];
    }
```

Text example #10, mocking anything

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

### Define your test data in a separate file

[](#define-your-test-data-in-a-separate-file)

By default, the runner is looking for tests/php-tt-data folder, so you can create your data files there

Create a data file

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

```

In your data file

```
