Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

PHPUnit\Framework\MockObject\Invocation as Invocation because the name is already in use Error #394

Closed
misyaath opened this issue Dec 10, 2017 · 4 comments
Assignees
Labels

Comments

@misyaath
Copy link

Q A
phpunit-mock-objects version 5.0.4
PHPUnit version 6.5
PHP version 7.0.0
Installation Method Composer / PHAR

Actual class

namespace TDD;

class Receipt
{


    public function __construct()
    {
    }

    public function total( array $items = array(), $coupon )
    {
        $sum = array_sum( $items );
        if ( !is_null( $coupon ) ) {
            return $sum - ( $sum * $coupon );
        }
        return $sum;
    }

    public function tax( $amount, $tax )
    {
        return $amount * $tax;
    }

    public function postTaxTotal( $items, $tax, $coupon )
    {

        $subTotal = $this->total( $items, $coupon );

        return $subTotal + $this->tax( $subTotal, $tax );
    }
}

Test Class


namespace TDD\Tests;
require dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . "vendor" . DIRECTORY_SEPARATOR . "autoload.php";
use PHPUnit\Framework\TestCase;
use TDD\Receipt;

class ReceiptTest extends TestCase
{

    public function setUp()
    {
    }

    public function tearDown()
    {

    }


    public function testPostTaxTotal()
    {
        $items = array( 0, 2, 5, 8 );
        $tax = 0.20;
        $coupon = null;
        $receipt = $this->getMockBuilder( "TDD\Receipt" )
            ->setMethods( array( "tax", "total" ) )
            ->getMock();

        $receipt->expects( $this->once() )
            ->method( "total" )
            ->with( $items, $coupon )
            ->will( $this->returnValue( 10.00 ) );

        $receipt->expects( $this->once() )
            ->method( "tax" )
            ->with( 10.00, $tax )
            ->will( $this->returnValue( 1.00 ) );

        $result = $receipt->postTaxTotal( array( 0, 2, 5, 8 ), 0.20, null );
        $this->assertEquals( 11.00, $result );
    }

}

When I run Return
PHPUnit 6.5.3 by Sebastian Bergmann and contributors.

Fatal error: Cannot use PHPUnit\Framework\MockObject\Invocation as Invocation because the name is already in use in C:\wamp64\www\unit\vendor\phpunit\phpunit-mock-objects\src\Matcher\MethodName.php on line 14

Above error I could not find the error where and why ?

@sebastianbergmann
Copy link
Owner

sebastianbergmann commented Dec 10, 2017

I cannot reproduce this with PHP 7.2.0.

<?php
namespace TDD;

class Receipt
{
    public function __construct()
    {
    }

    public function total( array $items = array(), $coupon )
    {
        $sum = array_sum( $items );

        if ( !is_null( $coupon ) ) {
            return $sum - ( $sum * $coupon );
        }

        return $sum;
    }

    public function tax( $amount, $tax )
    {
        return $amount * $tax;
    }

    public function postTaxTotal( $items, $tax, $coupon )
    {
        $subTotal = $this->total( $items, $coupon );

        return $subTotal + $this->tax( $subTotal, $tax );
    }
}
<?php
namespace TDD\Tests;

require __DIR__ . '/Receipt.php';;

use PHPUnit\Framework\TestCase;

use TDD\Receipt;

class ReceiptTest extends TestCase
{
    public function testPostTaxTotal()
    {
        $items = array( 0, 2, 5, 8 );
        $tax = 0.20;
        $coupon = null;
        $receipt = $this->getMockBuilder( Receipt::class )
            ->setMethods( [ 'tax', 'total' ] )
            ->getMock();

        $receipt->expects( $this->once() )
            ->method( 'total' )
            ->with( $items, $coupon )
            ->will( $this->returnValue( 10.00 ) );

        $receipt->expects( $this->once() )
            ->method( 'tax' )
            ->with( 10.00, $tax )
            ->will( $this->returnValue( 1.00 ) );

        $result = $receipt->postTaxTotal( array( 0, 2, 5, 8 ), 0.20, null );

        $this->assertEquals( 11.00, $result );
    }
}
$ phpunit ReceiptTest
PHPUnit 6.5.3 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 21 ms, Memory: 4.00MB

OK (1 test, 2 assertions)

@sebastianbergmann sebastianbergmann changed the title HPUnit\Framework\MockObject\Invocation as Invocation because the name is already in use Error PHPUnit\Framework\MockObject\Invocation as Invocation because the name is already in use Error Dec 10, 2017
@sebastianbergmann sebastianbergmann self-assigned this Dec 10, 2017
@sebastianbergmann
Copy link
Owner

I was able to reproduce the issue with PHP 7.0.0.

@sebastianbergmann
Copy link
Owner

And now I remember https://bugs.php.net/bug.php?id=66773 again.

@sebastianbergmann
Copy link
Owner

Closed via #391.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants