phpunit

In phpunit what is the difference between __construct versus setup?

て烟熏妆下的殇ゞ 提交于 2019-12-23 07:12:24
问题 I am curious to know it is good practice to create object in test class __construct or we should always use setup/teardown approach ( or setUpBeforeClass/tearDownAfterClass approach)? I aware of the fact set/teardown gets called for each test so will it do any good if I put my objec creation code in it? e.g. //mytestclass.php class MyTestClass extends PHPUnit_Framework_TestCase { private $obj; protected function setUp() { $this->obj = new FooClass(); } public testFooObj() { //assertions for

Is there a way to disable code coverage in PHPUnit for a single test?

╄→гoц情女王★ 提交于 2019-12-23 06:48:09
问题 Every time I run a single unit test in PHPUnit, a code coverage report is also generated. I have an older computer here at work, and if I could disable the code coverage when I don't need it, that would put less strain on my CPU. Is there a way to disable the code coverage on a per-test basis? I couldn't find any command-line switch that would do it. 回答1: How about making a copy of your phpunit.xml, removing the <logging> stanza from it, then doing: phpunit --configuration new.xml 回答2: Since

Mocking an authed user in CakePHP

£可爱£侵袭症+ 提交于 2019-12-23 06:16:09
问题 I am writing a CakePHP Unit Test for one of my Controllers. The Controller has several calls to the AuthComponent::user() method, to read data of the currently logged in user. There are 3 usages: AuthComponent::user() (no params, fetches the entire array) AuthComponent::user('id') (fetches the user id) AuthComponent::user('name') (fetches the username) I have tried two ways of mocking the AuthComponent in my test: // Mock the Controller and the Components $this->controller = $this->generate(

Examples on Zend_Rest_Controller Unit Testing

我们两清 提交于 2019-12-23 03:22:23
问题 I have found a bunch of examples how to unit test Zend_Controller, but I'm looking for examples on Zend_Rest_Controller Unit Testing. Any help is really appreciated. Thank you! 回答1: So, basically your question is how to emulate calling PUT and DELETE in your controller tests? Since this apparently doesn't work: $this->request->setMethod('PUT'); You can access both these actions with plain HTTP POST by providing _method parameter. So to call PUT : $this->request->setMethod('POST'); $this-

PHPUnit, Netbeans and Symfony2 and wrong tests location

雨燕双飞 提交于 2019-12-23 03:17:11
问题 I try to understand PHPUnit tests in Netbeans. I set correct phpunit.bat path and the phpunit-skelgen.bat file as well. When I try to create test for some file, using the netbeans option such us "create test file" I get correct directory structure, but the files are placed incorrectly. Next, following the tutorials from web I set the test folder (to jobeet/tests ), the phpunit bootstrap to jobeet\app\bootstrap.php.cache and the xml configuration file to jobeet\app\phpunit.xml.dist Please see

PHPUnit test a method that returns an objects property

*爱你&永不变心* 提交于 2019-12-23 02:38:57
问题 public function thisMethod { $example = $this->methodReturnsObject()->this1->that2->there3->id; return $example; } How would you test thisMethod in PHPUnit? Obviously I could write an expectation that methodReturnsObject() will return something... but what? That object has properties associated with it, but how would you even mock that value? 回答1: The answer is "You don't". Unit testing should test each class in isolation, what you are trying to do there is not a unit test. As I said in my

“pause” being ignored

↘锁芯ラ 提交于 2019-12-22 12:17:37
问题 I read that I must be able to run all unit tests in my site with a single command, so I created a bat file to do it. Even with pause before the end, after the phpunit command, the result of the unit tests flashes in the screen. @echo off cd c:\ cd xampp cd htdocs cd light cd myworks echo on set /p site=The site: set /p version=The version: set /p location=The location: phpunit "%site% v%version%/%location%" pause My other batch file with java %folder%/%file% runs the java software, then

Too many connection during unit testing

空扰寡人 提交于 2019-12-22 10:59:44
问题 I have a project with a lot of tests class like class MyTest extends BaseTestCase { public function __construct() { parent::__construct(); $this->em = $this->get('doctrine')->getManager(); } public function setUp() { $this->init(); //load sql data for the tests $path = $this->get('kernel')->locateResource('@Bundle/Data/Test.sql'); $content_file_sql_data = file_get_contents($path); $stmt = $this->em->getConnection()->prepare($content_file_sql_data); $stmt->execute(); $stmt->closeCursor(); } /*

phpunit does not find any tests with director xml tag, but does with (some) file tags

时光毁灭记忆、已成空白 提交于 2019-12-22 10:46:34
问题 I have this config: <?xml version="1.0" encoding="UTF-8"?> <phpunit backupGlobals = "false" backupStaticAttributes = "false" colors = "false" convertErrorsToExceptions = "true" convertNoticesToExceptions = "true" convertWarningsToExceptions = "true" processIsolation = "false" stopOnFailure = "false" stopOnError = "false" stopOnIncomplete = "false" syntaxCheck = "false" bootstrap = "test_bootstrap.php" > <testsuites> <testsuite name="UnitTests"> <file>unit/api/2/ApiControllerTest.php</file>

How can I reference external data providers in phpunit?

左心房为你撑大大i 提交于 2019-12-22 10:37:01
问题 I am trying to run some tests using a common data provider in PHPUnit. See below test: namespace AppBundle\Tests\Controller; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use AppBundle\Tests\DataProvider\XmlDataProvider; class DefaultControllerTest extends WebTestCase { /** * @dataProvider XmlDataProvider::xmlProvider * @covers ReceiveController::receiveAction() * @param string */ public function testReceive($xml) { $client = static::createClient([], ['HTTP_HOST' => 'mt.host']);