PHP Unit Tests: Is it possible to test for a Fatal Error?
No. Fatal error stops the execution of the script.
And it's not really necessary to test a singleton in that way. If you insist on checking if constructor is private, you can use ReflectionClass:getConstructor()
public function testCannotInstantiateExternally(){ $reflection = new \ReflectionClass('\My\Namespace\MyClassName'); $constructor = $reflection->getConstructor(); $this->assertFalse($constructor->isPublic());}
Another thing to consider is that Singleton classes/objects are an obstacle in TTD since they're difficult to mock.
Here's a complete code snippet of Mchl's answer so people don't have to go through the docs...
public function testCannotInstantiateExternally(){ $reflection = new \ReflectionClass('\My\Namespace\MyClassName'); $constructor = $reflection->getConstructor(); $this->assertFalse($constructor->isPublic());}
You can use a concept like PHPUnit's process-isolation.
This means the test code will be executed in a sub process of php. This example shows how this could work.
<?php// get the test code as string$testcode = '<?php new '; // will cause a syntax error// put it in a temporary file$testfile = tmpfile();file_put_contents($testfile, $testcode);exec("php $tempfile", $output, $return_value);// now you can process the scripts return value and output// in case of an syntax error the return value is 255switch($return_value) { case 0 : echo 'PASSED'; break; default : echo 'FAILED ' . $output;}// clean upunlink($testfile);