We recently upgraded phpunit from a very old version to the current 3.6.x version (at time of writing). During the upgrade I noticed that assertType is no longer supported in many of our tests which were testing if something was a string, an array, or an object. So I had to write a quick script to update assertType to assertInternalType and figured I would post it for others if they needed to do the same.


$testsPath = '/path/to/your/tests';

$iter = new RecursiveDirectoryIterator($testsPath, FilesystemIterator::NEW_CURRENT_AND_KEY | FilesystemIterator::SKIP_DOTS);

foreach (new RecursiveIteratorIterator($iter) as $file) {

    $extension = pathinfo($file->getFilename(), PATHINFO_EXTENSION);

    // Only want to deal with php scripts
    if ($extension !== 'php') {

        echo sprintf("Skipped: %s\n", $file->getRealPath());

        continue;
    }

    echo sprintf("Parsing: %s\n", $file->getRealPath());

    $contents = file($file->getRealPath());

    foreach ($contents as &$content) {
        if ((stripos($content, 'assertType') !== false) &&
            ((stripos($content, 'string') !== false) || (stripos($content, 'array') !== false)) || (stripos($content, 'float') !== false) || (stripos($content, 'object') !== false)) {
            $content = str_ireplace('assertType', 'assertInternalType', $content);
        }
    }

    file_put_contents($file->getRealPath(), $contents, LOCK_EX);
}