phpunit

Laravel PHPUnit mock Request

泄露秘密 提交于 2021-02-19 02:04:12
问题 I'm doing a PHPUnit on my controller and I can't seem to mock the Request right. Here's the Controller: use Illuminate\Http\Request; public function insert(Request $request) { // ... some codes here if ($request->has('username')) { $userEmail = $request->get('username'); } else if ($request->has('email')) { $userEmail = $request->get('email'); } // ... some codes here } Then on the unit test, public function testIndex() { // ... some codes here $requestParams = [ 'username' => 'test', 'email'

PHPUnit test doubles

二次信任 提交于 2021-02-16 18:53:33
问题 I am starting to use PHPUnit for test my code but I have some problems with understand double tests. I try to stub a class method b to return true instead of usual behavior (false) when is called since another method I have a code like this class MyClass { function a() { return $this->b(); } function b() { return false; } } class MyClassTest extends TestCase { function testAThrowStubB() { $myClassStub = $this->getMockBuilder('\MyClass') ->getMock(); $myClassStub->expects($this->any()) -

Laravel 7: assertSee() and html entities

折月煮酒 提交于 2021-02-11 14:34:17
问题 In my test, I use assertSee(). $message = '<h1>Header</h1>'; $response = $this->get($url); $response->assertStatus(200); $response->assertSee($message); The problem is that when the $message contains html entities then the assertion gets false. I know there is an e() helper to do convert html entities in $message but now I need the opposite. How can I do it? 回答1: ->assertSee(...) is changed in Laravel 7, now it has a second parameter $response->assertSee($value, $escaped = true); Just set it

How to let PHPUnit test property initialization in PHP7.4?

為{幸葍}努か 提交于 2021-02-11 12:09:13
问题 One of the code style changes in our application when adopting PHP7.4 typed properties was to move from: if (null === $object->value) { ... } to if (empty($object->value)) { ... } Even when a typed property is nullable, the first if-statement will throw an Error . The next step was on writing tests. Where using empty() on checking whether a typed property is initialized works, the PHPUnit implementation on assertEmpty crashes with the same error. Obviously assertFalse(isset($obj->value) would

How to set a different Phpunit environment variable?

て烟熏妆下的殇ゞ 提交于 2021-02-10 16:14:41
问题 In our code we have line: if (Configure::read('environment') != 'live') { ConnectionManager::alias(Configure::read('environment'), 'default'); } This means that whenever our code is not on live, our connection is going to be set as 'default' and we have this connection in app.php I have a problem with that. Since we are using CircleCI and our PhpUnit code fails and it gets reverted every time. So I need to set for PhpUnit different environment variable (I need it to use 'test', not 'default')

Symfony 5 - PhpUnit - Database not refresh on running test

一个人想着一个人 提交于 2021-02-10 07:01:21
问题 I noticed that when I run a functional test with PhpUnit, if there is a modification in the database, it is done. However, if I'm in the middle of my test and want to recover the record that was changed, it comes back to me in its old version, which is very problematic. For example, if I have a test in which: I collect a certain item I activate it (therefore, modification in the database of its enable property) via a URL of a controller I want to check that it is active I retrieve it from the

Skip Laravel's FormRequest Validation

大城市里の小女人 提交于 2021-02-08 19:35:44
问题 I've recently added HaveIBeenPwned to my form request class to check for cracked passwords. Given that this makes an external API call, is there a way for me to skip either this validation rule or the FormRequest class altogether during testing? Here's the request I make in my test. $params = [ 'first_name' => $this->faker->firstName(), 'last_name' => $this->faker->lastName(), 'email' => $email, 'password' => '$password', 'password_confirmation' => '$password', 'terms' => true, 'invitation' =

Passing data provider to setUp() in PHPUnit

纵然是瞬间 提交于 2021-02-08 15:09:13
问题 I'm currently trying to pass data from my data provider to the setUp()-method in PHPUnit. Background: I am using PHPUnit for running frontend-tests in different browsers. The browser should be defined inside the data provider and needs to be known by the setUp()-method. I understand, that a data provider initially is executed before the setUp()-method (as setUpBeforeClass()) is called. Therefore setUp()-data can not be passed to a data provider. But it should work the other way round, shouldn

Passing data provider to setUp() in PHPUnit

陌路散爱 提交于 2021-02-08 15:08:07
问题 I'm currently trying to pass data from my data provider to the setUp()-method in PHPUnit. Background: I am using PHPUnit for running frontend-tests in different browsers. The browser should be defined inside the data provider and needs to be known by the setUp()-method. I understand, that a data provider initially is executed before the setUp()-method (as setUpBeforeClass()) is called. Therefore setUp()-data can not be passed to a data provider. But it should work the other way round, shouldn

Passing data provider to setUp() in PHPUnit

爷,独闯天下 提交于 2021-02-08 15:06:26
问题 I'm currently trying to pass data from my data provider to the setUp()-method in PHPUnit. Background: I am using PHPUnit for running frontend-tests in different browsers. The browser should be defined inside the data provider and needs to be known by the setUp()-method. I understand, that a data provider initially is executed before the setUp()-method (as setUpBeforeClass()) is called. Therefore setUp()-data can not be passed to a data provider. But it should work the other way round, shouldn