From 6e98e4220ae8e4e2b6743e802a4d9b30e9c87786 Mon Sep 17 00:00:00 2001 From: anktd Date: Wed, 19 Jun 2024 18:35:58 +0530 Subject: [PATCH] more test cases --- tests/BlockonomicsTest.php | 161 ++++++++++++++++++++++++++++++++++++- 1 file changed, 158 insertions(+), 3 deletions(-) diff --git a/tests/BlockonomicsTest.php b/tests/BlockonomicsTest.php index e889308e..06ccbc45 100755 --- a/tests/BlockonomicsTest.php +++ b/tests/BlockonomicsTest.php @@ -16,7 +16,7 @@ protected function setUp(): void { parent::setUp(); wp::setUp(); // $this->blockonomics = new TestableBlockonomics(); - $this->blockonomics = m::mock('TestableBlockonomics')->makePartial(); + $this->blockonomics = m::mock(TestableBlockonomics::class, ['ZJ4PNtTnKqWxeMCQ6smlMBvj3i3KAtt2hwLSGuk9Lyk'])->makePartial(); // Mock WordPress get_option function wp::userFunction('get_option', [ 'return' => function($option_name) { @@ -42,7 +42,33 @@ protected function setUp(): void { ]); wp::userFunction('wp_remote_retrieve_body', [ 'return' => function($response) { - return json_encode(isset($response['response']['body']) ? $response['response']['body'] : []); + return isset($response['body']) ? $response['body'] : []; + } + ]); + // Mock WC() function + wp::userFunction('WC', [ + 'return' => function() { + return new class{ + public function api_request_url($endpoint) { + return "https://localhost:8888/wordpress/wc-api/WC_Gateway_Blockonomics/"; + //TODO: look for variations in http cases + } + }; + } + ]); + + // Mock add_query_arg function i.e. appends query arguments to a URL + wp::userFunction('add_query_arg', [ + 'return' => function($args, $url) { + if (!is_array($args)) { + $args = []; + } + return $url . '?' . http_build_query($args); + } + ]); + wp::userFunction('is_wp_error', [ + 'return' => function($thing) { + return ($thing instanceof \WP_Error); } ]); } @@ -169,7 +195,23 @@ private function getMockResponse($type) { 'code' => 200, 'message' => 'OK' ], - 'body' => json_encode([]) // Simulate no stores added + 'body' => json_encode([]) // no stores added + ]; + case 'one_store_different_callback': + return [ + 'response' => [ + 'code' => 200, + 'message' => 'OK' + ], + 'body' => json_encode([['address' => 'zpub6o4sVoUnjZ8qWRtWUFHL9TWKfStSo2rquV6LsHJWNDbrEP5L2CAG849xpXJXzsm4iNTbKGS6Q4gxK6mYQqfd1JCP3KKYco2DBxTrjpywWxt', 'tag' => 't_shirt_store_wordpress', 'callback' => 'http://localhost:8888/wordpress/wc-api/WC_Gateway_Blockonomics/?secret=30d8ea3494a820e37ffb46c801a6cce96cc2023e']]) + ]; + case 'one_store_no_callback': + return [ + 'response' => [ + 'code' => 200, + 'message' => 'OK' + ], + 'body' => json_encode([['address' => 'zpub6o4sVoUnjZ8qWRtWUFHL9TWKfStSo2rquV6LsHJWNDbrEP5L2CAG849xpXJXzsm4iNTbKGS6Q4gxK6mYQqfd1JCP3KKYco2DBxTrjpywWxt', 'tag' => 't_shirt_store_wordpress', 'callback' => '']]) ]; default: return [ @@ -189,6 +231,17 @@ private function mockGetCallbacks(array $cryptos, $responseType){ ->andReturn($mockedResponse); } } + private function mockUpdateCallbackResponse() { + wp::userFunction('wp_remote_post', [ + 'return' => [ + 'response' => [ + 'code' => 200, + 'message' => 'OK' + ], + 'body' => '' + ] + ]); + } // Testing Test Setup functionality // Case 1: Incorrect API Key is set and only BCH is enabled @@ -264,6 +317,108 @@ public function testBTCandBCHWithNoStore() { $this->assertEquals(['btc' => 'Please add a new store on blockonomics website', 'bch' => 'Please add a new store on blockonomics website'], $result); } + // API key is correct, 1 xpub is added and callback url is not set + public function testBTCWithOneStoreNoCallback() { + // Mock active currencies to return only BTC + $this->mockActiveCurrencies(['btc']); + + // Simulate the response where one store is added without any callback URL + $this->blockonomics->shouldReceive('get_callbacks') + ->with('btc') + ->andReturn($this->getMockResponse('one_store_no_callback')); + + // Mock the update callback response + $this->mockUpdateCallbackResponse(); + + // Execute testSetup and capture the results + $result = $this->blockonomics->testSetup(); + + // Assert that no errors are returned + $this->assertFalse($result['btc']); + } + + public function testBCHWithOneStoreNoCallback() { + // Mock active currencies to return only BCH + $this->mockActiveCurrencies(['bch']); + + // Simulate the response where one store is added without any callback URL + $this->blockonomics->shouldReceive('get_callbacks') + ->with('bch') + ->andReturn($this->getMockResponse('one_store_no_callback')); + + // Mock the update callback response + $this->mockUpdateCallbackResponse(); + + // Execute testSetup and capture the results + $result = $this->blockonomics->testSetup(); + + // Assert that no errors are returned + $this->assertFalse($result['bch']); + } + + public function testBTCandBCHWithOneStoreNoCallback() { + // Mock active currencies to return only BTC + $this->mockActiveCurrencies(['btc','bch']); + + // Simulate the response where one store is added without any callback URL + $this->blockonomics->shouldReceive('get_callbacks') + ->with('btc') + ->andReturn($this->getMockResponse('one_store_no_callback')); + + $this->blockonomics->shouldReceive('get_callbacks') + ->with('bch') + ->andReturn($this->getMockResponse('one_store_no_callback')); + + // Mock the update callback response + $this->mockUpdateCallbackResponse(); + + // Execute testSetup and capture the results + $result = $this->blockonomics->testSetup(); + + // Assert that no errors are returned + $this->assertFalse($result['btc']); + $this->assertFalse($result['bch']); + } + + public function testBTCWithOneStoreDifferentCallback() { + // Mock active currencies to return only BTC + $this->mockActiveCurrencies(['btc']); + + // Simulate the response where one store is added with a different callback URL + $this->blockonomics->shouldReceive('get_callbacks') + ->with('btc') + ->andReturn($this->getMockResponse('one_store_different_callback')); + + // Mock the update callback response to capture its input parameters + $callbackUrl = null; + $crypto = null; + $xpub = null; + + $this->blockonomics->shouldReceive('update_callback') + ->with(m::on(function($arg) use (&$callbackUrl) { + $callbackUrl = $arg; + return true; + }), m::on(function($arg) use (&$crypto) { + $crypto = $arg; + return true; + }), m::on(function($arg) use (&$xpub) { + $xpub = $arg; + return true; + })) + ->andReturn((object)[ + 'response_code' => 200, + 'response_message' => 'OK' + ]); + + // Execute testSetup and capture the results + $result = $this->blockonomics->testSetup(); + error_log("Result from testSetup: " . print_r($result, true)); + + // Assert that the expected error message is returned + $this->assertEquals('Please add a new store on blockonomics website', $result['btc']); + + } + protected function tearDown(): void { wp::tearDown(); parent::tearDown();