Server IP : 127.0.0.2 / Your IP : 18.218.137.145 Web Server : Apache/2.4.18 (Ubuntu) System : User : www-data ( ) PHP Version : 7.0.33-0ubuntu0.16.04.16 Disable Function : disk_free_space,disk_total_space,diskfreespace,dl,exec,fpaththru,getmyuid,getmypid,highlight_file,ignore_user_abord,leak,listen,link,opcache_get_configuration,opcache_get_status,passthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,php_uname,phpinfo,posix_ctermid,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid,posix,_getppid,posix_getpwnam,posix_getpwuid,posix_getrlimit,posix_getsid,posix_getuid,posix_isatty,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_setpgid,posix_setsid,posix_setuid,posix_times,posix_ttyname,posix_uname,pclose,popen,proc_open,proc_close,proc_get_status,proc_nice,proc_terminate,shell_exec,source,show_source,system,virtual MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/html/vendor/mockery/mockery/docs/reference/ |
Upload File : |
.. index:: single: Pass-By-Reference Method Parameter Behaviour Preserving Pass-By-Reference Method Parameter Behaviour ======================================================= PHP Class method may accept parameters by reference. In this case, changes made to the parameter (a reference to the original variable passed to the method) are reflected in the original variable. A simple example: .. code-block:: php class Foo { public function bar(&$a) { $a++; } } $baz = 1; $foo = new Foo; $foo->bar($baz); echo $baz; // will echo the integer 2 In the example above, the variable $baz is passed by reference to ``Foo::bar()`` (notice the ``&`` symbol in front of the parameter?). Any change ``bar()`` makes to the parameter reference is reflected in the original variable, ``$baz``. Mockery 0.7+ handles references correctly for all methods where it can analyse the parameter (using ``Reflection``) to see if it is passed by reference. To mock how a reference is manipulated by the class method, you can use a closure argument matcher to manipulate it, i.e. ``\Mockery::on()`` - see the ":doc:`argument_validation`" chapter. There is an exception for internal PHP classes where Mockery cannot analyse method parameters using ``Reflection`` (a limitation in PHP). To work around this, you can explicitly declare method parameters for an internal class using ``/Mockery/Configuration::setInternalClassMethodParamMap()``. Here's an example using ``MongoCollection::insert()``. ``MongoCollection`` is an internal class offered by the mongo extension from PECL. Its ``insert()`` method accepts an array of data as the first parameter, and an optional options array as the second parameter. The original data array is updated (i.e. when a ``insert()`` pass-by-reference parameter) to include a new ``_id`` field. We can mock this behaviour using a configured parameter map (to tell Mockery to expect a pass by reference parameter) and a ``Closure`` attached to the expected method parameter to be updated. Here's a PHPUnit unit test verifying that this pass-by-reference behaviour is preserved: .. code-block:: php public function testCanOverrideExpectedParametersOfInternalPHPClassesToPreserveRefs() { \Mockery::getConfiguration()->setInternalClassMethodParamMap( 'MongoCollection', 'insert', array('&$data', '$options = array()') ); $m = \Mockery::mock('MongoCollection'); $m->shouldReceive('insert')->with( \Mockery::on(function(&$data) { if (!is_array($data)) return false; $data['_id'] = 123; return true; }), \Mockery::any() ); $data = array('a'=>1,'b'=>2); $m->insert($data); $this->assertTrue(isset($data['_id'])); $this->assertEquals(123, $data['_id']); \Mockery::resetContainer(); }