sebastianbergmann/phpunit-mock-objects

Fatal error on running mocking example

micaherne opened this issue · 3 comments

Q A
phpunit-mock-objects version 5.0.2
PHPUnit version 6.5.1
PHP version 7.0.3
Installation Method Composer

I'm seeing this error on trying to create a mock object with a willReturn() value:

Fatal error: Cannot use PHPUnit\Framework\MockObject\Invocation as Invocation because the name is already in use in (my root directory)\vendor\phpunit\phpunit-mock-objects\src\Matcher\MethodName.php on line 14

This is even happening on trying to run example 9.1 / 9.2 in the PHPUnit Manual:

use PHPUnit\Framework\TestCase;

class StubTest extends TestCase
{
	public function testStub()
	{
		// Create a stub for the SomeClass class.
		$stub = $this->createMock(SomeClass::class);

		// Configure the stub.
		$stub->method('doSomething')
			 ->willReturn('foo');

		// Calling $stub->doSomething() will now return
		// 'foo'.
		$this->assertEquals('foo', $stub->doSomething());
	}
}

class SomeClass
{
	public function doSomething()
	{
		// Do something.
	}
}

I cannot reproduce this:

$ cat Test.php 
<?php
use PHPUnit\Framework\TestCase;

class StubTest extends TestCase
{
	public function testStub()
	{
		// Create a stub for the SomeClass class.
		$stub = $this->createMock(SomeClass::class);

		// Configure the stub.
		$stub->method('doSomething')
			 ->willReturn('foo');

		// Calling $stub->doSomething() will now return
		// 'foo'.
		$this->assertEquals('foo', $stub->doSomething());
	}
}

class SomeClass
{
	public function doSomething()
	{
	}
}
$ phpunit Test
PHPUnit 6.5.1 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 38 ms, Memory: 4.00MB

OK (1 test, 1 assertion)

OK, thanks for checking. I'll try rebuilding my environment.

@sebastianbergmann In general this error is dependent on order of file includes and PHP version (it was fixed in PHP 7.0.13).

So to reproduce the error you need PHP < 7.0.13 and include /src/Matcher/Invocation.php before including /src/Matcher/MethodName.php.

Example of failure in https://ci.appveyor.com/project/ondrejmirtes/phpstan/build/945/job/pouu1q1k8qo31iaa


EDIT: That's not enough to reproduce this. Reading the original bug report, it's also opcache dependent.