When using mock in unit tests, the following error occurred: Mock queue is empty . I searched differently - but did not find a solution.

The error occurs when I call the sendRequest method twice in one function.

In the first case ( POST request), it sends data from the mock file. The second ( PUT request) request issues the Mock queue is empty .

In condition:

 if ($auth == false) if (!is_null($this->mockSubscriber)) $client->getEmitter()->attach($this->mockSubscriber->getMock($stream . '_' . $method));` 

both requests come.

Through debug can see that the mock read both answer files during initialization.

I send requests through guzzlehttp/guzzle 5.3.0

 class ClientTest extends PHPUnit_Framework_TestCase{ protected $clientWallet; protected function setUp(){ $url = "***"; $userName = "***"; $password = "***"; $grantType = "***"; $appKey = '***'; $cfgData = new PayClient\Configuration( [ 'url' => $url, 'username' => $userName, 'password' => $password, 'grantType' => $grantType, 'appKey' => $appKey ] ); /* * WalletTopUp logger and mocks */ // Initialize logger. $logger = new \Monolog\Logger("WalletTopUp"); $logger->pushHandler(new \Monolog\Handler\StreamHandler("WalletTopUp_test.log")); // Initialize mock subscriber Wallet. $mockWallet = null; $useMockResponseWallet = true; if ($useMockResponseWallet) { $mockWallet = new MockSubscriber( __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'mocks'); } // Create Wallet Top Up Client $this->clientWallet = new \maviance\WalletTopUpClient\WalletTopUpClient($cfgData, $mockWallet); $this->clientWallet->addLogger($logger); $this->clientWallet->init(); } /* * Test Wallet Top Up client * */ function testWalletPurchase() { $purchaseData = new PurchaseRequestData(); $purchaseData->setCustomerPhonenumber('0712999267'); $purchaseData->setAmount(50); $purchaseResponse = $this->clientWallet->purchase('wallet', 56438, '', $purchaseData); } } 

Class WalletTopUpClient :

 class WalletTopUpClient{ function __construct(Configuration $config, MockSubscriber $mock = null) { $this->cfgData = $config; $this->mockSubscriber = $mock; $this->getAuthToken(); } public function purchase($service, $destinationId, $srcRefId, PurchaseRequestData $inputData) { $body = [ 'Stream' => $service, 'Amount' => $inputData->getAmount(), 'PhoneNumber' => $inputData->getCustomerPhonenumber(), 'PaymentTypeID' => $this::PAYMENT_TYPE_ID ]; $response = $this->sendRequest($service, 'POST', $this->cfgData->getUrl() . '/api/payments/POST', $body); $data = json_decode($response->getBody()->getContents(), true); $body = [ 'Stream' => $service, 'TransactionID' => $data['TransactionID'], 'PhoneNumber' => $inputData->getCustomerPhonenumber() ]; $response = $this->sendRequest($service, 'PUT', $this->cfgData->getUrl() . '/api/payments/PUT', $body); $data = json_decode($response->getBody()->getContents(), true); return new PurchaseResponse( new PurchaseStatus(PurchaseStatus::SUCCESS), \DateTime::createFromFormat('Ymd H:i:s.u', $data['TransactionDate']), $srcRefId, $data['ReceiptNumber'], $data['TransactionStatus']); } function sendRequest($stream, $method, $url, $body = null, $auth = false) { // Determine body. $body = is_array($body) && !$auth ? utils\Utils::toString($body) : $body; // Get http client $client = $this->getClient(); if ($auth == false) if (!is_null($this->mockSubscriber)) $client->getEmitter()->attach($this->mockSubscriber->getMock($stream . '_' . $method)); // Create request. $request = $client->createRequest($method, $url, ['body' => $body]); if (!$auth) { $request->setHeader('Content-Type', 'application/x-www-form-urlencoded'); $request->setHeader('Authorization', '**** ' . $this->getAuthToken()->access_token); $request->setHeader('app_key', $this->cfgData->getAppKey()); } $response = null; try { $response = $client->send($request); } catch (RequestException $e) { $this->logger->error("Error sending request: " . $e->getMessage()); throw new FatalErrorException($e->getMessage(), $e->getCode()); } catch (\Exception $e) { $this->logger->error("Error sending request: " . $e->getMessage()); throw new FatalErrorException($e->getMessage()); } return $response; } } 

    1 answer 1

    Everything, the problem is solved! It was necessary after the request to execute detach (), so that the listener would be reset to zero. I give the full function code

     function sendRequest($stream, $method, $url, $body = null, $auth = false) { // Determine body. $body = is_array($body) && !$auth ? utils\Utils::toString($body) : $body; // Get http client $client = $this->getClient(); $mock = null; if ($auth == false) if (!is_null($this->mockSubscriber)) $client->getEmitter()->attach(($mock = $this->mockSubscriber->getMock($stream . '_' . $method))); // Create request. $request = $client->createRequest($method, $url, ['body' => $body]); if (!$auth) { $request->setHeader('Content-Type', 'application/x-www-form-urlencoded'); $request->setHeader('Authorization', 'bearer ' . $this->getAuthToken()->access_token); $request->setHeader('app_key', $this->cfgData->getAppKey()); } $response = null; try { $response = $client->send($request); } catch (RequestException $e) { $this->logger->error("Error sending request: " . $e->getMessage()); throw new FatalErrorException($e->getMessage(), $e->getCode()); } catch (\Exception $e) { $this->logger->error("Error sending request: " . $e->getMessage()); throw new FatalErrorException($e->getMessage()); } // Detach mock subscriber. if (isset($mock)) $client->getEmitter()->detach($mock); return $response; }