skip to Main Content

We have 2 scenarios:

  1. Account Binding (registering a bank account number to a service)
  2. Verify OTP (input the otp code sent via customer’s mobile phone)

Some response from the Account Binding used in the second scenario, but I need to "inject" the OTP Code value before the second step.

What I’ve done so far in Codeception is using CodeceptionLibActorSharedPause:

    public function testVerificationFlow()
    {
        $faker = FakerFactory::create();
        
        $sequence = '0000';
        $partnerReferenceNo = $faker->numerify('###########') . $sequence;
        $bankAccountNo      = $faker->numerify('##########');
        $bankCardNo         = $faker->randomNumber(4, true);
        $limit              = 250000.00;
        $email              = $faker->email();
        $custIdMerchant     = $faker->numerify('##########');
        
        $response = $this->autopay->accountBinding(
            $partnerReferenceNo,
            $bankAccountNo,
            $bankCardNo,
            $limit,
            $email,
            $custIdMerchant
        );

        codecept_debug($response);
        $this->assertEquals($response->responseCode, self::RESP_CODE_ACCOUNT_BINDING);
        
        $otpCode = '';
        
        // how to inject the value of $otpCode here?
        $this->pause();

        codecept_debug($otpCode);

        $verifyOtpResponse = $this->autopay->verifyOtp(
            $partnerReferenceNo,
            $response->originalReferenceNo,
            $response->chargeToken,
            $otpCode,
        );

        codecept_debug($verifyOtpResponse);
        $this->assertEquals($verifyOtpResponse->responseCode, self::RESP_CODE_OTP_VERIFY);

    }

The HoaConsole project is abandoned since years ago

 Execution PAUSED, starting interactive shell...
  Type in commands to try them:
  - ENTER to continue
  - TAB to auto-complete
  - F5 to stash a command
  - F6 to toggle auto-stashing of successful commands
  - F8 to view stashed commands
  - F10 to clear stashed commands

I still have no clue what to use when pausing the unit test. Any insight / google keyword to search is appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    I just realized that HoaConsole is using readline() to "pause" the process.

    $otpCode = readline('Please input the OTP before run testVerifyOtp: ');
    
    $verifyOtpResponse = $this->autopay->verifyOtp(
        $partnerReferenceNo,
        $response->referenceNo,
        $response->additionalInfo->chargeToken,
        (string) $otpCode,
    );
    
    codecept_debug($verifyOtpResponse);
    $this->assertEquals($verifyOtpResponse->responseCode, self::RESP_CODE_OTP_VERIFY);
    
    Please input the OTP before run testVerifyOtp: 364612
    ✔ AutopayTest: Verification flow (9.85s)
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    
    Time: 00:09.862, Memory: 10.00 MB
    
    OK (1 test, 2 assertions)
    

  2. This would be my solution in a pure program.

    class OTP_Verify_Response {
        
        protected $data_file = false;
        protected $questions = array();
        protected $responses = array();
        
        public function __construct($data_file) {
            if (!empty($data_file) and file_exists($data_file)) {
                $this->data_file = realpath($data_file);
            }
            if (!empty($this->data_file)) {
                $data = file_get_contents($this->data_file);
                $data = json_decode($data, true);
            } else {
                $data = array();
            }
            $this->questions = $data['questions'];
            $this->responses = $data['responses'];
        }
        
        public function doQuestions() {
            if ($question_answer = $this->_askQuestion("Please input the OTP before run testVerifyOtp:", "testVerifyOtp")) {
                $this->_testVerifyOtp($question_answer); // This was answered
            } else {
                return false; // We need a answer 
            }
            // Do another question ...
            return $this->closeDataFile();
        }
        
        protected function _testVerifyOtp($question_answer) {
            // do something with $question_answer
        }
        
        protected function _askQuestion($question_text, $question_field) {
            if (empty($question_text)) {
                return false;
            }
            if (!empty($_POST) and array_key_exists($question_field, $_POST)) {
                $question_answer = $_POST[$question_field];
                $this->_storeAnswer($question_text, $posted_answer);
            } elseif ($question_text and !empty($question_text)) {
                $question_answer = $this->_answerQuestion($question_text);
            } else {
                $question_answer = false;
            }
            if (!empty($question_answer) {
                return $question_answer;
            }
            echo "<div>{$question_text}? <input name='{$question_field}' value='{$_POST[$question_field]}' /></div>";
            return false;
        }
        
        protected function _answerQuestion($question_text) {
            $question_hash = $this->_createQuestionHash($question_text);
            if ($this->responses[$question_hash]) {
                return $this->responses[$question_hash];
            }
            return false;
        }
        
        protected function _storeAnswer($question_text, $posted_answer) {
            $question_hash = $this->_createQuestionHash($question_text);
            $this->questions[$question_hash] = $question_text;
            $this->responses[$question_hash] = $posted_answer;
            return $posted_answer;
        }
        
        protected function _createQuestionHash($question_text) {
            $question_hash = md5($question_text);
            return $question_hash;
        }
        
        public function update_store_data() {
            $store_data = array();
            $store_data['questions'] = $this->questions;
            $store_data['responses'] = $this->responses;
            if (!empty($this->data_file)) {
                return file_put_contents($this->data_file, json_encode($store_data));
            }
            return false;
        }
        
        public function closeDataFile() {
            if (!empty($this->data_file) and file_exists($this->data_file)) {
                return unlink($this->data_file);
            }
            return false;
        }
        
    }
    

    The idea is to use the POST as well as a DATA File and save each question / response

    $verify_item = new OTP_Verify_Response("/tempFile.tmp");
    $verify_item->doQuestions();
    

    (You would need to wrap this in a <form></form>) but ideally it would work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search