php-doc-en/reference/uopz/functions/uopz-set-mock.xml

217 lines
4.9 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.uopz-set-mock" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>uopz_set_mock</refname>
<refpurpose>Use mock instead of class for new objects</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>void</type><methodname>uopz_set_mock</methodname>
<methodparam><type>string</type><parameter>class</parameter></methodparam>
<methodparam><type>mixed</type><parameter>mock</parameter></methodparam>
</methodsynopsis>
<para>
If <parameter>mock</parameter> is a string containing the name of a class then it will be instantiated instead of
<parameter>class</parameter>. <parameter>mock</parameter> can also be an object.
</para>
<note>
<para>
Only dynamic access to properties and methods will use the <parameter>mock</parameter> object.
Static access still uses the original <parameter>class</parameter>.
See <link linkend="uopz_set_mock.example.static">example</link> below.
</para>
</note>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term><parameter>class</parameter></term>
<listitem>
<para>
The name of the class to be mocked.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>mock</parameter></term>
<listitem>
<para>
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 <code>::class</code> magic constant in this case.
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
&return.void;
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>uopz 6.0.0</entry>
<entry>
Mocking static members is no longer supported by this function.
<function>uopz_redefine</function> and <function>uopz_set_return</function>,
or <link linkend="book.componere">Componere</link> can be used instead.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>uopz_set_mock</function> example</title>
<programlisting role="php">
<![CDATA[
<?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();
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
mockA
]]>
</screen>
</example>
<example>
<title><function>uopz_set_mock</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
class A {
public function who() {
echo "A";
}
}
uopz_set_mock(A::class, new class {
public function who() {
echo "mockA";
}
});
(new A)->who();
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
mockA
]]>
</screen>
</example>
<example xml:id="uopz_set_mock.example.static">
<title><function>uopz_set_mock</function> and static members</title>
<para>
As of uopz 6.0.0 mocking static members is no longer supported.
</para>
<programlisting role="php">
<![CDATA[
<?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();
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
A
A
]]>
</screen>
<para>
Output of the above example in uopz 5:
</para>
<screen>
<![CDATA[
mockA
mockA
]]>
</screen>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
<member><function>uopz_get_mock</function></member>
<member><function>uopz_unset_mock</function></member>
</simplelist>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->