📜 ⬆️ ⬇️

Preparing for PHP interviews using tests (phpt) from PHP source

When manually building PHP (in this case I am considering version 7.0.7), you must run the make test command before make install, which runs all the tests in the tests folder, after which you can send the result from the command line. If you look at this folder, then folders with the name classes, func, basic, etc. are immediately conspicuous in it ... Why is this interesting?

The fact is that at interviews they often like to ask questions regarding both syntactic (rather boring tasks (crosswords), like ++ $ i / $ i ++ that are in general terms), and general ones, for example, OOP. Popular moments are the inheritance of interfaces, abstract classes, the essence of which in general is to break the correct OOP or to reveal the depth of knowledge of a candidate on features (capabilities). It is these moments of color that check the tests, and therefore, it is quite useful in my opinion to look at fluently tests (the expected result is written in the tests). For greater persuasiveness, try running similar code. Googling and find out why this is how it is implemented (works) in PHP, and not otherwise.

Here for an example, a basic question on interviews and the test from source codes.

: tests/classes/abstract_by_interface_001.phpt 

')
 --TEST-- ZE2 An abstract method may not be called --FILE-- <?php class Root { } interface MyInterface { function MyInterfaceFunc(); } abstract class Derived extends Root implements MyInterface { } class Leaf extends Derived { function MyInterfaceFunc() {} } var_dump(new Leaf); class Fails extends Root implements MyInterface { } ?> ===DONE=== --EXPECTF-- object(Leaf)#%d (0) { } Fatal error: Class Fails contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (MyInterface::MyInterfaceFunc) in %sabstract_by_interface_001.php on line %d 

As can be seen from the description, the Fails class will fall into a fatal error, since it inherits the MyInterface interface, but did not determine the function body from the MyInterfaceFunc () interface. See the interface documentation.

Let us proceed to the following test:

 : tests/classes/abstract_by_interface_002.phpt 


In this case, the same interface MyInterface has the definition of the signature of a method declared static.

 interface MyInterface { static function MyInterfaceFunc(); } .... class Fails extends Root implements MyInterface { } ?> ===DONE=== --EXPECTF-- object(Leaf)#%d (0) { } Fatal error: Class Fails contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (MyInterface::MyInterfaceFunc) in %sabstract_by_interface_002.php on line %d 

As can be seen from the expected error, in this case, the method must also be defined in the class that inherits this interface.

Or here's a fairly frequent question about whether an abstract method can be defined in a regular class.

 : tests/classes/abstract_derived.phpt 


 --TEST-- ZE2 A derived class with an abstract method must be abstract         --SKIPIF-- <?php if (version_compare(zend_version(), '2.0.0-dev', '<')) die('skip ZendEngine 2 needed'); ?> --FILE-- <?php class base { } class derived extends base { abstract function show(); } ?> ===DONE=== <?php exit(0); ?> --EXPECTF-- Fatal error: Class derived contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (derived::show) in %sabstract_derived.php on line %d 


Or here is one great test:

 : tests/classes/abstract_redeclare.phpt 


 <?php class pass { function show() { echo "Call to function show()\n"; } } class fail extends pass { abstract function show(); } echo "Done\n"; // Shouldn't be displayed ?> 

The show () method cannot be overridden by the abstract.

Let's go to the test directory of func and how, for example, test using static and postfix increment / decrement.

 : tests/func/002.phpt 


 --TEST-- Static variables in functions --FILE-- <?php function blah() { static $hey=0,$yo=0; echo "hey=".$hey++.", ",$yo--."\n"; } blah(); blah(); blah(); if (isset($hey) || isset($yo)) { echo "Local variables became global :(\n"; } --EXPECT-- hey=0, 0 hey=1, -1 hey=2, -2 


Finally, in order not to tire you too much, I’ll give an interesting test on bug 28800

 --TEST-- Bug #28800 (Incorrect string to number conversion for strings starting with 'inf') --FILE-- <?php $strings = array('into', 'info', 'inf', 'infinity', 'infin', 'inflammable'); foreach ($strings as $v) { echo ($v+0)."\n"; } ?> --EXPECT-- 

That's all, thanks for taking the time.

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


All Articles