Add constant to layout in PHPUnit

Is it possible to add a class constant to a layout using PHPUnit?

Here is an example:

class SomeTest extends PHPUnit_Framework_TestCase { public function setUp() { $mock = $this->getMock( 'SomeClass' ); // Here I'd like to add a constant to $mock; something like // $mock::FOOBAR; } } 

Do any of you know how I can make this work?

thanks!

+6
source share
1 answer

This question had some unanswered time, but I ran into this problem. This is not possible; however, at least one dirty job:

In the test file

 <?php class SomeClass { const FOOBAR = 'foobar'; } class SomeTest extends PHPUnit_Framework_TestCase { public function setUp() { $mock = $this->getMock( 'SomeClass' ); } } // tests ?> 

Then you use your mocked object to mock functionality, and you use the class constant in the same way as you originally did. For instance:

 // Call a method on mocked object // (would need to add this method to your mock, of course) $mock->doSomething(); // Use the constant $fooBar = SomeClass::FOOBAR; 

This is dirty, so I'm sure that everything can be confusing if you use some kind of autoload that tries to load the actual SomeClass class, but it will work β€œfine” if you don't Download the original SomeClass .

I am definitely interested in hearing other solutions, as well as getting some feedback on how dirty this is.

+2
source

Source: https://habr.com/ru/post/946615/


All Articles