Test Layout and PHP Magic __get

I'm having trouble trying to simulate objects using the __get and __set methods (using simpletest ).

Writing mock answers for __get doesn't smell right - the tests seem too tightly bound to the implementation. Any recommendations for testing, or should I completely avoid magic methods?

+4
source share
1 answer

I had the same problem and found a solution in SimpleTest test cases:

From mock_objects_test.php:

class ClassWithSpecialMethods { function __get($name) { } function __set($name, $value) { } function __isset($name) { } function __unset($name) { } function __call($method, $arguments) { } function __toString() { } } Mock::generate('ClassWithSpecialMethods'); 

... snip ...

 function testReturnFromSpecialAccessor() { $mock = new MockClassWithSpecialMethods(); $mock->setReturnValue('__get', '1st Return', array('first')); $mock->setReturnValue('__get', '2nd Return', array('second')); $this->assertEqual($mock->first, '1st Return'); $this->assertEqual($mock->second, '2nd Return'); } function testcanExpectTheSettingOfValue() { $mock = new MockClassWithSpecialMethods(); $mock->expectOnce('__set', array('a', 'A')); $mock->a = 'A'; } 

A bit clumsy, but it works. On the other hand, I think you better avoid them ... the big corporate system I'm working on uses them a lot, and it's a nightmare to understand / visualize / debug / do something with!

+3
source

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


All Articles