Server IP : 127.0.0.2 / Your IP : 3.141.202.216 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: PHPUnit Integration PHPUnit Integration =================== Mockery was designed as a simple-to-use *standalone* mock object framework, so its need for integration with any testing framework is entirely optional. To integrate Mockery, you just need to define a ``tearDown()`` method for your tests containing the following (you may use a shorter ``\Mockery`` namespace alias): .. code-block:: php public function tearDown() { \Mockery::close(); } This static call cleans up the Mockery container used by the current test, and run any verification tasks needed for your expectations. For some added brevity when it comes to using Mockery, you can also explicitly use the Mockery namespace with a shorter alias. For example: .. code-block:: php use \Mockery as m; class SimpleTest extends PHPUnit_Framework_TestCase { public function testSimpleMock() { $mock = m::mock('simplemock'); $mock->shouldReceive('foo')->with(5, m::any())->once()->andReturn(10); $this->assertEquals(10, $mock->foo(5)); } public function tearDown() { m::close(); } } Mockery ships with an autoloader so you don't need to litter your tests with ``require_once()`` calls. To use it, ensure Mockery is on your ``include_path`` and add the following to your test suite's ``Bootstrap.php`` or ``TestHelper.php`` file: .. code-block:: php require_once 'Mockery/Loader.php'; require_once 'Hamcrest/Hamcrest.php'; $loader = new \Mockery\Loader; $loader->register(); If you are using Composer, you can simplify this to just including the Composer generated autoloader file: .. code-block:: php require __DIR__ . '/../vendor/autoload.php'; // assuming vendor is one directory up .. caution:: Prior to Hamcrest 1.0.0, the ``Hamcrest.php`` file name had a small "h" (i.e. ``hamcrest.php``). If upgrading Hamcrest to 1.0.0 remember to check the file name is updated for all your projects.) To integrate Mockery into PHPUnit and avoid having to call the close method and have Mockery remove itself from code coverage reports, use this in you suite: .. code-block:: php // Create Suite $suite = new PHPUnit_Framework_TestSuite(); // Create a result listener or add it $result = new PHPUnit_Framework_TestResult(); $result->addListener(new \Mockery\Adapter\Phpunit\TestListener()); // Run the tests. $suite->run($result); If you are using PHPUnit's XML configuration approach, you can include the following to load the ``TestListener``: .. code-block:: xml <listeners> <listener class="\Mockery\Adapter\Phpunit\TestListener"></listener> </listeners> Make sure Composer's or Mockery's autoloader is present in the bootstrap file or you will need to also define a "file" attribute pointing to the file of the above ``TestListener`` class. .. caution:: PHPUnit provides a functionality that allows `tests to run in a separated process <http://phpunit.de/manual/4.0/en/appendixes.annotations.html#appendixes.annotations.runTestsInSeparateProcesses>`_, to ensure better isolation. Mockery verifies the mocks expectations using the ``Mockery::close()`` method, and provides a PHPUnit listener, that automatically calls this method for you after every test. However, this listener is not called in the right process when using PHPUnit's process isolation, resulting in expectations that might not be respected, but without raising any ``Mockery\Exception``. To avoid this, you cannot rely on the supplied Mockery PHPUnit ``TestListener``, and you need to explicitly calls ``Mockery::close``. The easiest solution to include this call in the ``tearDown()`` method, as explained previously.