phpunit

PHPUnit: How do I mock this file system?

非 Y 不嫁゛ 提交于 2020-01-01 08:32:42
问题 Consider the following scenario (this is not production code): class MyClass { public function myMethod() { // create a directory $path = sys_get_temp_dir() . '/' . md5(rand()); if(!mkdir($path)) { throw new Exception("mkdir() failed."); } // create a file in that folder $myFile = fopen("$path/myFile.txt", "w"); if(!$myFile) { throw new Exception("Cannot open file handle."); } } } Right, so what's the problem? Code coverage reports that this line is not covered: throw new Exception("Cannot

Is there a way test STDERR output in PHPUnit?

只愿长相守 提交于 2020-01-01 08:14:45
问题 I have a class that outputs to STDERR , but I am having trouble finding a way to get PHPUnit to test its output. The class, PHPUnit_Extensions_OutputTestCase , also did not work. 回答1: I don't see a way to buffer stderr as you can with stdout , so I would refactor your class to move the call that does the actual output to a new method. This will allow you to mock that method during testing to validate the output or subclass with one that buffers. For example, say you have a class that lists

PHPUnit - getallheaders not work

孤人 提交于 2020-01-01 08:05:10
问题 I'm testing my code, and i have some problem with header. In each api i use $headers = getallheaders(); to get that, and this works fine when i test with the app or crhome postman extension. When i lauch my test, like this $client = $this->createClient(); $client->request('GET', '/api/shotcard', ['qrcode'=>'D0m1c173'], [], ['HTTP_API_TOKEN' => 'abc123'] ); $this->assertEquals(200, $client->getResponse()->getStatusCode()); where i try to shot a card with that qrcode with a user with that test

Using PHPUnit to test cookies and sessions, how?

 ̄綄美尐妖づ 提交于 2020-01-01 04:41:05
问题 With PHPUnit it's quite easy to test raw PHP code, but what about code that heavily relies on cookies? Sessions could be a good example. Is there a method that doesn't require me to setup $_COOKIE with data during my test? It feels like a hacky way of doing things. 回答1: This is a common problem with code, especially lagacy PHP code. The common technique used is to further abstract the COOKIE/SESSION variables in related objects and using inversion of control technique(s) to pull those

Test query execution time in laravel

雨燕双飞 提交于 2020-01-01 04:35:31
问题 How to test in laravel/phpunit how long query took to execute? Is it possible so that it does not depend on something else? 回答1: The oldest way is the best one: $start = microtime(true); // Execute the query $time = microtime(true) - $start; 回答2: Since Laravel 5.2 listen has changed to only except one argument: \DB::listen(function ($query) { // $query->sql // $query->bindings // $query->time }); Docs: https://laravel.com/docs/5.2/database 回答3: You could listen to executing query like this

Run PHPUnit tests on change

陌路散爱 提交于 2020-01-01 01:53:09
问题 I'd like to run my PHPUnit tests (or at least a subset of them) whenever a file changes on disk. Very similar to what you can do with "grunt watch". I have a project in which I have both JS and PHP, and am using Grunt. There I shell out to PHPUnit to have it run on top of my JS tests using grunt watch. While that works just fine, it seems like an awful lot of hassle to do this in a PHP only project. I'd need to introduce grunt, and add a dependency on node. Plus I have a lot of such PHP

Temporary Doctrine2 fixtures for testing with phpunit

南楼画角 提交于 2019-12-31 14:31:16
问题 I have an application built on Symfony2 + Doctrine2 which I want to create some tests for (using phpunit). For example if I want to test a unique validator against a record in the DB, I want to create a record I can work with, but after the test I don't need it anymore. So is there a way to create temporary (or virtual) fixtures or do I have to manually create and delete them? 回答1: You can use Doctrine DataFixture and put this code in your setUp method of a unit test class: $loader = new

How to Read / Improve C.R.A.P Index Calculated by PHP

假装没事ソ 提交于 2019-12-31 07:52:07
问题 I just started working with PHPUnit and its colorful code coverage reports. I understand all the numbers and percentages save one: The C.R.A.P index. Can anyone offer me a solid explanation of what it means, how to analyze it and how to lower it? 回答1: @Toader Mihai offered a solid explanation. (+1 from me) How to lower it: Write less complex code OR write better tested code. (See the graph below) Better tested Code ? In this context this just means: A higher code coverage and usually results

Unable to globally install older version of phpunit phar

喜欢而已 提交于 2019-12-31 03:43:11
问题 As given in https://phpunit.de/manual/current/en/installation.html#installation.phar.verification, the steps to globally install the PHAR are: $ wget https://phar.phpunit.de/phpunit.phar $ chmod +x phpunit.phar $ sudo mv phpunit.phar /usr/local/bin/phpunit $ phpunit --version PHPUnit x.y.z by Sebastian Bergmann and contributors. I followed the above, but with the URL for the older version, i.e. https://phar.phpunit.de/phpunit-old.phar (because our PHP version is older). I ran the following

PHPUnit with a Zend Framework 2 module

做~自己de王妃 提交于 2019-12-30 10:49:59
问题 I'm struggling in getting a PHPUnit test to work with ZF2. My directory structure looks as follows project - src - config, data, module, public, vendor - init_autoloader.php - test - bootstrap.php - SimpleTest.php The application itself works well. Now for running PHPUnit tests, my bootstrap.php looks as follows putenv('ZF2=../src/vendor/zendframework/zendframework/library'); $loader = include '../src/vendor/autoload.php'; include '../src/init_autoloader.php'; This works for ZF2 related