0
votes

I am trying to add some existing test cases to a pre existing project Here is the API class

    <?php
namespace MyApp\Api;

use MyApp\ApiBase;
use MyApp\Ethereum as Eth;
use PHPUnit\Runner\Exception;

/**
 * Class Ethereum
 * @package MyApp\Api
 */
class Ethereum extends ApiBase {

    /**
     * Function to get balances
     * @param array $addresses
     * @param string $tag
     * @return array
     */
    public function getBalances(array $addresses, string $tag = 'latest') {
        $data = [];

        foreach ($addresses as $addr) {
            // validate address
            if (!Eth::validateAddress($addr)) {
                continue;
            }

        }

        return $data;
    }

}

The service class

<?php
namespace MyApp;

use MyApp\Ethereum\GethIpc;
use MyApp\Ethereum\GethWebsocket;
use PHPUnit\Runner\Exception;

/**
 * Class Ethereum
 * @package MyApp
 */
class Ethereum {
    public static $subscriptions = [];

    /**
     * Ethereum constructor.
     */
    public function __construct() {
        $this->connection = new GethWebsocket();
        $connect = $this->connection->connect();
    }


    /**
     * Function to validate an address
     * @param string $address
     * @return bool
     */
    public static function validateAddress(string $address) {
        return preg_match("/^(0x)?[0-9a-fA-F]{40}$/", $address) !== 0;
    }


}

My test class

<?php
declare(strict_types=1);

namespace MyApp\Test;

use MyApp\Api\Ethereum;
use MyApp\Ethereum as Eth;
use PHPUnit\Framework\TestCase;
use PHPUnit\Runner\Exception;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
/**
 * @covers MyApp\Api\Ethereum
 */ 
final class EthereumTest extends MockeryTestCase {

    protected $ethereumApi;
    protected $ethereum_address;

    //Setup method called before every method 
    protected function setUp(): void {
        $this->ethereumApi = new Ethereum();
        $this->ethereum_address = '0x0000000000000000000000000000000' . rand(100000000, 999999999);

        //Mockery::globalHelpers();

        //$mock = mock(MyApp\Ethereum::class);
    }


    public function testGetBalances_ValidEthereumAddress(): void {

        $mockEthereumService = Mockery::mock("Eth");
        $mockEthereumService->shouldReceive('validateAddress')->once()->with($this->ethereum_address)->andReturn(true);
        //$mockEthereumService->shouldReceive('msg')->once()->with($this->ethereum_address)->andReturn(true);

        $addresses = [$this->ethereum_address];
        $result = $this->ethereumApi->getBalances($addresses);
        $this->assertNotEmpty($result);
    }

    public function tearDown()
    {
        Mockery::close();
    }
}

Everytime I run the test class - the mock is not working and the actual service class method is being called

Can anyone offer assistance on how I should get this mock example working correctly?

1

1 Answers

1
votes

Not having experience with Mockery myself, but after looking into the documentation

  1. http://docs.mockery.io/en/latest/reference/creating_test_doubles.html#overloading
  2. http://docs.mockery.io/en/latest/cookbook/mocking_hard_dependencies.html

I'd assume you need to use the "overload" prefix and the full classname and remove the use-statement for MyApp\Ethereum from your test case.