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

    When manually assembling 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 names classes, func, basic, etc. are immediately striking in it. Why is this interesting?

    The fact is that at interviews they often like to ask questions both regarding some kind of syntactic (rather boring tasks (crosswords), like ++ $ i / $ i ++ 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 violate the correct OOP or to reveal the candidate’s depth of knowledge on the features (capabilities). It is precisely these moments that test color and test, and therefore, it is rather useful in my opinion to look through the tests fluently (the expected result is written in the tests). To be more convincing, try to execute the same code. Googling and finding out why it is so implemented (works) in PHP, and not otherwise.

    Here is an example, a basic question at interviews and a test from source codes.

    Файл: tests/classes/abstract_by_interface_001.phpt
    


    --TEST--
    ZE2 An abstract method may not be called
    --FILE--
    
    ===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 you can see from the description, the Fails class will fall into a fatal error, since it inherited the MyInterface interface, but did not determine the function body from the MyInterfaceFunc () interface. See the documentation for the interfaces .

    Let's move on to the next test:

    Файл: tests/classes/abstract_by_interface_002.phpt
    


    In this case, the same interface MyInterface, has a definition of the signature of the method declared as 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 you can see from the expected error, in this case, the method must also be defined in the class that inherits this interface.

    Or here's a pretty common 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--
    
    --FILE--
    
    ===DONE===
    
    --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
    



    The show () method cannot be overridden by abstract.

    Let's go to the func test directory and, as an example, a test using static and a postfix increment / decrement.

    Файл: tests/func/002.phpt
    


    --TEST--
    Static variables in functions
    --FILE--
    


    Напоследок, чтобы не сильно Вас утомлять, приведу интересный тест, по баге 28800

    --TEST--
    Bug #28800 (Incorrect string to number conversion for strings starting with 'inf')
    --FILE--
    
    --EXPECT--
    

    На этом все, спасибо за отнятое время.

    Also popular now: