PHP code example of petrkotek / phpunit-naughtytestdetector

1. Go to this page and download the library: Download petrkotek/phpunit-naughtytestdetector 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/ */

    

petrkotek / phpunit-naughtytestdetector example snippets


namespace MyProject\TestUtils\MyMetricFetcher;

use PetrKotek\NaughtyTestDetector\MetricFetcher;

class MyMetricFetcher implements MetricFetcher
{
    private $db;
    
    public function __construct()
    {
        $this->db = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
    }

    /**
     * @return array
     */
    public function fetchMetrics()
    {
        $result = mysqli_query($this->db, 'SELECT COUNT * FROM my_table');
        $row = mysqli_fetch_row($result);
        
        return ['records' => $row[0];
    }
}

xml
<phpunit bootstrap="vendor/autoload.php">
    ...
    <listeners>
        <listener class="PetrKotek\NaughtyTestDetector\PHPUnit\Listeners\NaughtyTestListener">
            <arguments>
                <!-- Class name of your own MetricFetcher -->
                <string>MyProject\TestUtils\MyMetricFetcher</string>
                <!-- Optional constructor arguments for the metric fetcher -->
                <array>
                    <element>
                        <string>hello world</string>
                    </element>
                </array>
                <!-- Optionally specify levels on which MetricFetcher should be executed -->
                <!-- Note: values below are the default ones -->
                <array>
                    <element key="executeOnTestLevel">
                        <boolean>false</boolean>
                    </element>
                    <element key="executeOnTestSuiteLevel">
                        <boolean>true</boolean>
                    </element>                
                    <element key="executeOnGlobalLevel">
                        <boolean>false</boolean>
                    </element>
                </array>
            </arguments>
        </listener>
    </listeners>
</phpunit>