Skip to main content
added 119 characters in body
Source Link
John Joseph
  • 951
  • 5
  • 14

You can do this by catching the exception thrown by trying to instantiate a new object from the singleton.

Try the following (PHP 7):

class Single { private function __construct() {} } class SingleTest extends \PHPUnit_Framework_TestCase { function testCannotCallConstructor() { try { $single = new Single(); $this->fail('Should never be called!'); } catch (\Throwable $e) { $this->assertNotEmpty($e->getMessage()); }    //alternative: $this->expectException(\Error::class); $single = new Single();  } } 

You can do this by catching the exception thrown by trying to instantiate a new object from the singleton.

Try the following (PHP 7):

class Single { private function __construct() {} } class SingleTest extends \PHPUnit_Framework_TestCase { function testCannotCallConstructor() { try { $single = new Single(); $this->fail('Should never be called!'); } catch (\Throwable $e) { $this->assertNotEmpty($e->getMessage()); } } } 

You can do this by catching the exception thrown by trying to instantiate a new object from the singleton.

Try the following (PHP 7):

class Single { private function __construct() {} } class SingleTest extends \PHPUnit_Framework_TestCase { function testCannotCallConstructor() { try { $single = new Single(); $this->fail('Should never be called!'); } catch (\Throwable $e) { $this->assertNotEmpty($e->getMessage()); }    //alternative: $this->expectException(\Error::class); $single = new Single();  } } 
Source Link
John Joseph
  • 951
  • 5
  • 14

You can do this by catching the exception thrown by trying to instantiate a new object from the singleton.

Try the following (PHP 7):

class Single { private function __construct() {} } class SingleTest extends \PHPUnit_Framework_TestCase { function testCannotCallConstructor() { try { $single = new Single(); $this->fail('Should never be called!'); } catch (\Throwable $e) { $this->assertNotEmpty($e->getMessage()); } } }