Jump to content

Error: Call to undefined method PHPUnit\Framework\TestCase::expectedOutputString()


Recommended Posts

https://docs.phpunit.de/en/11.1/writing-tests-for-phpunit.html#writing-tests-for-phpunit-output-examples-outputtest-php

But it gives me an error

//PHPUnit 11.1.3 by Sebastian Bergmann and contributors.
//Runtime:       PHP 8.3.6

//this->expectedOutputString()
Error: Call to undefined method OutputTest::expectedOutputString()

//parent::expectedOutputString()
Error: Call to undefined method PHPUnit\Framework\TestCase::expectedOutputString()

Why?

 

Link to comment
Share on other sites

OK!!!
Allow me another question.

https://www.w3resource.com/php/PHPUnit/writing-tests-for-phpunit-outputs.php
methods setOutputCallback() and getActualOutput() are mentioned here, but they don't work for me

Error: Call to undefined method PHPUnit\Framework\TestCase::setOutputCallback()
Error: Call to undefined method PHPUnit\Framework\TestCase::getActualOutput()

Why?

Link to comment
Share on other sites

for your last two errors, here are some possibilities -

  1. you copy/pasted the code from the web where it was 'published' and it doesn't contain only ascii characters. see if deleting and  retyping the line(s) of code corrects the problem.
  2. because these frameworks have their own error reporting, you may only be seeing a snippet of the actual error information.  assuming your code is something like - $this->setOutputCallback(...), what does using var_dump($this); show?
Link to comment
Share on other sites

Posted (edited)
$ cat MyTest.php 
<?php
declare(strict_types=1);

use PHPUnit\Framework\TestCase;

final class MyTest extends TestCase {

    final public function testMy(): void {
        var_dump($this);
        $this->setOutputCallback();
        $this->getActualOutput();
    }

}

$ vendor/bin/phpunit MyTest.php
PHPUnit 11.2.0 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.6

object(MyTest)#307 (41) {
  ["backupGlobals":"PHPUnit\Framework\TestCase":private]=>
  bool(false)
  ["backupGlobalsExcludeList":"PHPUnit\Framework\TestCase":private]=>
  array(0) {
  }
  ["backupStaticProperties":"PHPUnit\Framework\TestCase":private]=>
  bool(false)
  ["backupStaticPropertiesExcludeList":"PHPUnit\Framework\TestCase":private]=>
  array(0) {
  }
  ["snapshot":"PHPUnit\Framework\TestCase":private]=>
  NULL
  ["backupGlobalErrorHandlers":"PHPUnit\Framework\TestCase":private]=>
  array(1) {
    [0]=>
    object(PHPUnit\Runner\ErrorHandler)#220 (6) {
      ["baseline":"PHPUnit\Runner\ErrorHandler":private]=>
      NULL
      ["enabled":"PHPUnit\Runner\ErrorHandler":private]=>
      bool(true)
      ["originalErrorReportingLevel":"PHPUnit\Runner\ErrorHandler":private]=>
      int(32767)
      ["source":"PHPUnit\Runner\ErrorHandler":private]=>
      object(PHPUnit\TextUI\Configuration\Source)#64 (20) {
        ["baseline":"PHPUnit\TextUI\Configuration\Source":private]=>
        NULL
        ["ignoreBaseline":"PHPUnit\TextUI\Configuration\Source":private]=>
        bool(false)
        ["includeDirectories":"PHPUnit\TextUI\Configuration\Source":private]=>
        object(PHPUnit\TextUI\Configuration\FilterDirectoryCollection)#66 (1) {
          ["directories":"PHPUnit\TextUI\Configuration\FilterDirectoryCollection":private]=>
          array(0) {
          }
        }
        ["includeFiles":"PHPUnit\TextUI\Configuration\Source":private]=>
        object(PHPUnit\TextUI\Configuration\FileCollection)#39 (1) {
          ["files":"PHPUnit\TextUI\Configuration\FileCollection":private]=>
          array(0) {
          }
        }
        ["excludeDirectories":"PHPUnit\TextUI\Configuration\Source":private]=>
        object(PHPUnit\TextUI\Configuration\FilterDirectoryCollection)#40 (1) {
          ["directories":"PHPUnit\TextUI\Configuration\FilterDirectoryCollection":private]=>
          array(0) {
          }
        }
        ["excludeFiles":"PHPUnit\TextUI\Configuration\Source":private]=>
        object(PHPUnit\TextUI\Configuration\FileCollection)#41 (1) {
          ["files":"PHPUnit\TextUI\Configuration\FileCollection":private]=>
          array(0) {
          }
        }
        ["restrictDeprecations":"PHPUnit\TextUI\Configuration\Source":private]=>
        bool(false)
        ["restrictNotices":"PHPUnit\TextUI\Configuration\Source":private]=>
        bool(false)
        ["restrictWarnings":"PHPUnit\TextUI\Configuration\Source":private]=>
        bool(false)
        ["ignoreSuppressionOfDeprecations":"PHPUnit\TextUI\Configuration\Source":private]=>
        bool(false)
        ["ignoreSuppressionOfPhpDeprecations":"PHPUnit\TextUI\Configuration\Source":private]=>
        bool(false)
        ["ignoreSuppressionOfErrors":"PHPUnit\TextUI\Configuration\Source":private]=>
        bool(false)
        ["ignoreSuppressionOfNotices":"PHPUnit\TextUI\Configuration\Source":private]=>
        bool(false)
        ["ignoreSuppressionOfPhpNotices":"PHPUnit\TextUI\Configuration\Source":private]=>
        bool(false)
        ["ignoreSuppressionOfWarnings":"PHPUnit\TextUI\Configuration\Source":private]=>
        bool(false)
        ["ignoreSuppressionOfPhpWarnings":"PHPUnit\TextUI\Configuration\Source":private]=>
        bool(false)
        ["ignoreSelfDeprecations":"PHPUnit\TextUI\Configuration\Source":private]=>
        bool(false)
        ["ignoreDirectDeprecations":"PHPUnit\TextUI\Configuration\Source":private]=>
        bool(false)
        ["ignoreIndirectDeprecations":"PHPUnit\TextUI\Configuration\Source":private]=>
        bool(false)
        ["deprecationTriggers":"PHPUnit\TextUI\Configuration\Source":private]=>
        array(2) {
          ["functions"]=>
          array(0) {
          }
          ["methods"]=>
          array(0) {
          }
        }
      }
      ["sourceFilter":"PHPUnit\Runner\ErrorHandler":private]=>
      object(PHPUnit\TextUI\Configuration\SourceFilter)#219 (0) {
      }
      ["deprecationTriggers":"PHPUnit\Runner\ErrorHandler":private]=>
      array(2) {
        ["functions"]=>
        array(0) {
        }
        ["methods"]=>
        array(0) {
        }
      }
    }
  }
  ["backupGlobalExceptionHandlers":"PHPUnit\Framework\TestCase":private]=>
  array(0) {
  }
  ["runClassInSeparateProcess":"PHPUnit\Framework\TestCase":private]=>
  NULL
  ["runTestInSeparateProcess":"PHPUnit\Framework\TestCase":private]=>
  NULL
  ["preserveGlobalState":"PHPUnit\Framework\TestCase":private]=>
  bool(false)
  ["inIsolation":"PHPUnit\Framework\TestCase":private]=>
  bool(false)
  ["expectedException":"PHPUnit\Framework\TestCase":private]=>
  NULL
  ["expectedExceptionMessage":"PHPUnit\Framework\TestCase":private]=>
  NULL
  ["expectedExceptionMessageRegExp":"PHPUnit\Framework\TestCase":private]=>
  NULL
  ["expectedExceptionCode":"PHPUnit\Framework\TestCase":private]=>
  NULL
  ["providedTests":"PHPUnit\Framework\TestCase":private]=>
  array(1) {
    [0]=>
    object(PHPUnit\Framework\ExecutionOrderDependency)#302 (4) {
      ["className":"PHPUnit\Framework\ExecutionOrderDependency":private]=>
      string(6) "MyTest"
      ["methodName":"PHPUnit\Framework\ExecutionOrderDependency":private]=>
      string(6) "testMy"
      ["shallowClone":"PHPUnit\Framework\ExecutionOrderDependency":private]=>
      bool(false)
      ["deepClone":"PHPUnit\Framework\ExecutionOrderDependency":private]=>
      bool(false)
    }
  }
  ["data":"PHPUnit\Framework\TestCase":private]=>
  array(0) {
  }
  ["dataName":"PHPUnit\Framework\TestCase":private]=>
  string(0) ""
  ["methodName":"PHPUnit\Framework\TestCase":private]=>
  string(6) "testMy"
  ["groups":"PHPUnit\Framework\TestCase":private]=>
  array(1) {
    [0]=>
    string(7) "default"
  }
  ["dependencies":"PHPUnit\Framework\TestCase":private]=>
  array(0) {
  }
  ["dependencyInput":"PHPUnit\Framework\TestCase":private]=>
  array(0) {
  }
  ["iniSettings":"PHPUnit\Framework\TestCase":private]=>
  array(0) {
  }
  ["locale":"PHPUnit\Framework\TestCase":private]=>
  array(0) {
  }
  ["mockObjects":"PHPUnit\Framework\TestCase":private]=>
  array(0) {
  }
  ["status":"PHPUnit\Framework\TestCase":private]=>
  object(PHPUnit\Framework\TestStatus\Unknown)#298 (1) {
    ["message":"PHPUnit\Framework\TestStatus\TestStatus":private]=>
    string(0) ""
  }
  ["numberOfAssertionsPerformed":"PHPUnit\Framework\TestCase":private]=>
  int(0)
  ["testResult":"PHPUnit\Framework\TestCase":private]=>
  NULL
  ["output":"PHPUnit\Framework\TestCase":private]=>
  string(0) ""
  ["outputExpectedRegex":"PHPUnit\Framework\TestCase":private]=>
  NULL
  ["outputExpectedString":"PHPUnit\Framework\TestCase":private]=>
  NULL
  ["outputBufferingActive":"PHPUnit\Framework\TestCase":private]=>
  bool(true)
  ["outputBufferingLevel":"PHPUnit\Framework\TestCase":private]=>
  int(1)
  ["outputRetrievedForAssertion":"PHPUnit\Framework\TestCase":private]=>
  bool(false)
  ["doesNotPerformAssertions":"PHPUnit\Framework\TestCase":private]=>
  bool(false)
  ["customComparators":"PHPUnit\Framework\TestCase":private]=>
  array(0) {
  }
  ["testValueObjectForEvents":"PHPUnit\Framework\TestCase":private]=>
  object(PHPUnit\Event\Code\TestMethod)#81 (7) {
    ["file":"PHPUnit\Event\Code\Test":private]=>
    string(15) "/app/MyTest.php"
    ["className":"PHPUnit\Event\Code\TestMethod":private]=>
    string(6) "MyTest"
    ["methodName":"PHPUnit\Event\Code\TestMethod":private]=>
    string(6) "testMy"
    ["line":"PHPUnit\Event\Code\TestMethod":private]=>
    int(8)
    ["testDox":"PHPUnit\Event\Code\TestMethod":private]=>
    object(PHPUnit\Event\Code\TestDox)#293 (3) {
      ["prettifiedClassName":"PHPUnit\Event\Code\TestDox":private]=>
      string(2) "My"
      ["prettifiedMethodName":"PHPUnit\Event\Code\TestDox":private]=>
      string(2) "My"
      ["prettifiedAndColorizedMethodName":"PHPUnit\Event\Code\TestDox":private]=>
      string(2) "My"
    }
    ["metadata":"PHPUnit\Event\Code\TestMethod":private]=>
    object(PHPUnit\Metadata\MetadataCollection)#297 (1) {
      ["metadata":"PHPUnit\Metadata\MetadataCollection":private]=>
      array(0) {
      }
    }
    ["testData":"PHPUnit\Event\Code\TestMethod":private]=>
    object(PHPUnit\Event\TestData\TestDataCollection)#292 (2) {
      ["data":"PHPUnit\Event\TestData\TestDataCollection":private]=>
      array(0) {
      }
      ["fromDataProvider":"PHPUnit\Event\TestData\TestDataCollection":private]=>
      NULL
    }
  }
  ["wasPrepared":"PHPUnit\Framework\TestCase":private]=>
  bool(true)
  ["failureTypes":"PHPUnit\Framework\TestCase":private]=>
  array(0) {
  }
  ["expectedUserDeprecationMessage":"PHPUnit\Framework\TestCase":private]=>
  array(0) {
  }
  ["expectedUserDeprecationMessageRegularExpression":"PHPUnit\Framework\TestCase":private]=>
  array(0) {
  }
}
E                                                                   1 / 1 (100%)

Time: 00:00.006, Memory: 6.00 MB

There was 1 error:

1) MyTest::testMy
Error: Call to undefined method MyTest::setOutputCallback()

/app/MyTest.php:10

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

 

Edited by rick645
Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...

PHPUnit Version Compatibility: The method expectedOutputString() might not be available in the version of PHPUnit you are using (PHPUnit 11.1.3 in your case). PHPUnit evolves over time, and methods can be added, deprecated, or removed between versions.

Incorrect Method Name: Double-check the method name expectedOutputString(). In some cases, method names can change slightly between versions or might be case-sensitive.

Namespace or Scope Issue: Ensure that you are extending the correct class (PHPUnit\Framework\TestCase for PHPUnit tests) and that your test class is properly set up with the correct namespaces and inheritance.

Here’s an example of how expectOutputString() is typically used in PHPUnit tests:

use PHPUnit\Framework\TestCase;

class OutputTest extends TestCase
{
    public function testOutput()
    {
        $expectedOutput = "Expected output";
        $this->expectOutputString($expectedOutput);
        
        // Perform actions that should produce the expected output
        
        echo $expectedOutput;
    }
}

Ensure that your test class extends PHPUnit\Framework\TestCase and that your PHPUnit installation is properly set up and accessible within your development environment.

Best Regard

Danish Hafeez | QA Assistant

ICTInnovations

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.