PHP.nl

uopz_set_mock

uopz_set_mock

Use mock instead of class for new objects

void **uopz_set_mock** string $class mixed $mock

If is a string containing the name of a class then it will be instantiated instead of . can also be an object. mock``class``mock

Opmerking: > Only dynamic access to properties and methods will use the object. Static access still uses the original . See below. mock``classexample

classThe name of the class to be mocked.

mock The mock to use in the form of a string containing the name of the class to use or an object. If a string is passed, it has to be the fully qualified class name. It is recommended to use the magic constant in this case. ::class

return.void

Voorbeeld: example

<?php
class A {
    public function who() {
        echo "A";
    }
}

class mockA {
    public function who() {
        echo "mockA";
    }
}

uopz_set_mock(A::class, mockA::class);
(new A)->who();
?>
mockA

Voorbeeld: example

<?php
class A {
    public function who() {
        echo "A";
    }
}

uopz_set_mock(A::class, new class {
                            public function who() {
                                echo "mockA";
                            }
                        });
(new A)->who();
?>
mockA

Voorbeeld: and static members

As of uopz 6.0.0 mocking static members is no longer supported.

<?php
class A {
    const CON = 'A';
    public static function who() {
        echo "A";
    }
}

uopz_set_mock(A::class, new class {
                            const CON = 'mockA';
                            public static function who() {
                                echo "mockA";
                            }
                        });
echo A::CON, PHP_EOL;
A::who();
?>
A
A

Output of the above example in uopz 5:

mockA
mockA

uopz_get_mock``uopz_unset_mock