Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

(Using Pest) If the first test doesn't use PHPMockery, but subsequent tests do, they don't work #21

Open
@Gerben-T

Description

Pest 3.7.4
Mockery 1.6.12
php-mock-mockery 1.5.0

Given the following test file
test.php

<?php
namespace TEST;
use Mockery;
class ABC {
	function test() {
		$result = json_encode([]);
		if($result === false) {
			return false;
		}
		return $result;
	}
}
afterEach(function () {
	Mockery::close();
});
it('should return the json encoded array', function () {
	$abc = new ABC();
	$result = $abc->test();
	expect($result)->toBe('[]');
});
it('should return false when json_encode fails', function () {
	$abc = new ABC();
	\phpmock\mockery\PHPMockery::mock(__NAMESPACE__, 'json_encode')
		->andReturn(false);
	$result = $abc->test();
	expect($result)->toBe(false);
});

And running using

 php vendor/pestphp/pest/bin/pest test.php

You'll notice that the second test fails with

 Failed asserting that '[]' is identical to false.

However when you run

 php vendor/pestphp/pest/bin/pest test.php --filter "/should return false when json_encode fails/"

it succeeds

✓ it should return false when json_encode fails

I've done some testing and this happens only when the first test doesn't use PHPMockery::mock().

This setup works

<?php
namespace TEST;
use Mockery;
class ABC {
	function test() {
		$result = json_encode([]);
		if($result === false) {
			return false;
		}
		return $result;
	}
}
afterEach(function () {
	Mockery::close();
});
it('should return false when json_encode fails', function () {
	$abc = new ABC();
	\phpmock\mockery\PHPMockery::mock(__NAMESPACE__, 'json_encode')
		->andReturn(false);
	$result = $abc->test();
	expect($result)->toBe(false);
});
it('should return the json encoded array', function () {
	$abc = new ABC();
	$result = $abc->test();
	expect($result)->toBe('[]');
});

and it also doesn't matter if there are other tests after that that use PHPMockery::mock()

<?php
namespace TEST;
use Mockery;
class ABC {
	function test() {
		$result = json_encode([]);
		if($result === false || $result === 'somethingunique') {
			return false;
		}
		return $result;
	}
}
afterEach(function () {
	Mockery::close();
});
it('should return false when json_encode fails', function () {
	$abc = new ABC();
	\phpmock\mockery\PHPMockery::mock(__NAMESPACE__, 'json_encode')
		->andReturn(false);
	$result = $abc->test();
	expect($result)->toBe(false);
});
it('should return the json encoded array', function () {
	$abc = new ABC();
	$result = $abc->test();
	expect($result)->toBe('[]');
});
it('should return false when json_encode returns something unique', function () {
	$abc = new ABC();
	\phpmock\mockery\PHPMockery::mock(__NAMESPACE__, 'json_encode')
		->andReturn('somethingunique');
	$result = $abc->test();
	expect($result)->toBe(false);
});

What could be the problem here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /