我正在创建一个Codeigniter库,我该如何测试代码?

时间:2014-11-03 13:10:15

标签: php codeigniter

我正在使用codeigniter为轮盘赌系统构建一个库。

我希望使代码非常高效且防黑客。

  1. 是否有可用于测试代码的工具,如功能测试等?
  2. 代码效率高吗?如果不是如何测试/提高效率?
  3. 这是代码

        <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    /**
    * Name:  Prad Roulette
    *
    * Version: 0.1
    *
    * Author: Pradyummna Reddy
    *         prad@hireprad.co.uk
    *         
    *
    * Location: https://github.com/pradyummna/prad_roulette
    *
    * Created:  21/10/2014
    *
    * Description:  The library can be used for roulette system developers
    *
    * Requirements: PHP5 or above
    *
    */
    
    class Prad_roulette
    {
    
        /**
         *
         * Function to find if the number is a valid roulette number
         *
         * @param  integer  $number The number to which has to be checked
         *
         * @return boolean 
         *
         * Anchor: 1num
         */
         function isValidRouletteNumber($number)
        {
            if(ctype_digit($number) && $number < 37 && $number >= 0 ){return true;}else{return false;}
        }
    
    
        /**
         * Function to finds the number's color` in a roulette table
         *
         * @param  integer  $number The number to which the colour is to be determined
         *
         * @return string
         *
         * Anchor : 2color
         */
        function findColour($number)
        {
    
            // checking the input numbers
            if(!($this->isValidRouletteNumber($number))){return 'InValidInput';}
    
            $wheel = array('0','32','15','19','4','21','2','25','17','34','6','27','13','36','11','30','8','23','10','5','24','16','33','1','20','14','31','9','22','18','29','7','28','12','35','3','26');
    
            $numberInArray = array_keys($wheel, $number);
    
            if(isset($numberInArray[0]))
            {
                if($numberInArray[0] == '0')
                {$blackRred='ZERO';}
                else{
                        if($numberInArray[0] % 2 == 0)
                        {
                            //its black!!!
                            $blackRred='Black';
                        }
                        else{ $blackRred = 'Red'; }
                    }
            }
            else
            {
                $blackRred = 'OUT';
            }
    
        return $blackRred;  
        }
    
        /**
         * Function to finds the number's neighbours left to them
         *
         * @param  integer  $number The number to which the left neighbours is to be determined
         * @param  integer  $howManyNeighbours The total neighbours to be found
         * @return array
         *
         * Anchor: neiLside
         */
        function neighboursLeft($number,$howManyNeighbours)
        {   
            // checking the input numbers
            if(!($this->isValidRouletteNumber($number)) || !($this->isValidRouletteNumber($howManyNeighbours))){return array('InValidInput');}
    
            $x = $howManyNeighbours;
            $wheel = array('0','32','15','19','4','21','2','25','17','34','6','27','13','36','11','30','8','23','10','5','24','16','33','1','20','14','31','9','22','18','29','7','28','12','35','3','26');
    
            $numberInArray = array_keys($wheel, $number);
    
            $neighboursLeft = null;
            if(isset($numberInArray[0]))
            {
                $currentNumber = $numberInArray[0];
    
                //finding the end of the array
                $startValueInArray = reset($wheel);
                //$endArrayKey = key($wheel);               
                //reset($wheel);
    
                //moving to the current position value position in the array
                while (key($wheel) !== $numberInArray[0]) next($wheel);
    
                $neighboursLeft[0] = 'In';
                for($i=0;$i<$x;$i++)
                {
                    if(current($wheel) != $startValueInArray)
                    {
                        //echo next($wheel);echo '::'.$i; echo '<br/>';
                        $neighboursLeft[$i+1] =  prev($wheel);
                    }
                    else
                    {
                        end($wheel);
                        //echo current($wheel);echo '::'.$i;echo '<br/>';
                        $neighboursLeft[$i+1] =  current($wheel);
                    }
                }
            }
            else
            {
                $neighboursLeft = array('OUT');
            }
            //$neighboursLeft[0] can be OUT or In 
            return $neighboursLeft;
    
        }
    
        /**
         * Function to finds the number's neighbours right to them
         *
         * @param  integer  $number The number to which the right neighbours is to be determined
         * @param  integer  $howManyNeighbours The total neighbours to be found
         * @return array
         *
         * Anchor: neirside
         */
        function neighboursRight($number,$howManyNeighbours)
        {
    
            // checking the input numbers
            if(!($this->isValidRouletteNumber($number)) || !($this->isValidRouletteNumber($howManyNeighbours))){return array('InValidInput');}
    
    
            $x = $howManyNeighbours;
            $wheel = array('0','32','15','19','4','21','2','25','17','34','6','27','13','36','11','30','8','23','10','5','24','16','33','1','20','14','31','9','22','18','29','7','28','12','35','3','26');
    
            $numberInArray = array_keys($wheel, $number);
    
            $neighboursRight = null;
            if(isset($numberInArray[0]))
            {
                $currentNumber = $numberInArray[0];
    
                //finding the end of the array
                $endValueInArray = end($wheel);
                //$endArrayKey = key($wheel);               
                reset($wheel);
    
                //moving to the current position value position in the array
                while (key($wheel) !== $numberInArray[0]) next($wheel);
    
                $neighboursRight[0] = 'In';
                for($i=0;$i<$x;$i++)
                {
                    if(current($wheel) != $endValueInArray)
                    {
                        //echo next($wheel);echo '::'.$i; echo '<br/>';
                        $neighboursRight[$i+1] =  next($wheel);
                    }
                    else
                    {
                        reset($wheel);
                        //echo current($wheel);echo '::'.$i;echo '<br/>';
                        $neighboursRight[$i+1] =  current($wheel);
                    }
                }
    
            }
            else
            {
                $neighboursRight = array('OUT');
            }
    
            // $neighboursRight[0] can be OUT or In
            return $neighboursRight;
    
        }
    
        /**
         * Function to finds the number's neighbours right to them
         *
         * @param  array  $numbers The number to which the right neighbours is to be determined
         * @param  array  $moneyOnNumbers The total neighbours to be found
         * @param  integer $winningNumber The winning number
         * @return array payoutAmount, Profit, Invested amount
         *
         * Anchor: xPayoutProfit
         */
        function payoutProfit($numbers,$moneyOnNumbers,$winningNumber)
        {
            // checking the input numbers
            if(!($this->isValidRouletteNumber($number)) || !($this->isValidRouletteNumber($winningNumber))){return array('InValidInput');}
    
            //find the payout
    
                if(in_array($winningNumber,$numbers))
                {
                    $amountOnNumber = $moneyOnNumbers[array_search($winningNumber)];
                    $payoutAmount = $amountOnNumber * 36;
                }
                else
                {
                    $payoutAmount = 0;
                }           
                $totalInvested = 0;
    
                // find the amount invested on it
                foreach($moneyOnNumbers as $money)
                {
                    $totalInvested = $totalInvested + $money;
                }           
    
                //calculate profit
                $profit = $payoutAmount - $totalInvested;
    
                //return
                return array($payoutAmount,$totalInvested,$profit);
        }
    
    
        /**
         * Function to finds the number's neighbour right to it
         *
         * @param  integer  $number The number to which the right xth neighbour is to be determined
         * @param  integer  $distanceToXthNumber Distance to the xth neighbour
         *
         * @return integer number in the xth position
         *
         * Anchor: XthNeiRside
         */
        function findXthNumberOnRight($number,$distanceToXthNumber)
        {
            // checking the input numbers
            if(!($this->isValidRouletteNumber($number)) || !($this->isValidRouletteNumber($distanceToXthNumber))){return array('InValidInput');}
            if($distanceToXthNumber != '0')
            {
                $xthNumber = $this->neighboursRight($number,$distanceToXthNumber);
                $xthNumber = $xthNumber[$distanceToXthNumber];
            }
            else
            {
                return $number;
            }
    
            //find the payout
            return $xthNumber;
        }
    
        /**
         * Function to finds the number's xth neighbour left to it
         *
         * @param  integer  $number The number to which the left xth neighbour is to be determined
         * @param  integer  $distanceToXthNumber Distance to the xth neighbour
         *
         * @return integer number in the xth position
         *
         * Anchor: XthNeiLside
         */
        function findXthNumberOnLeft($number,$distanceToXthNumber)
        {
            // checking the input numbers
            if(!($this->isValidRouletteNumber($number)) || !($this->isValidRouletteNumber($distanceToXthNumber))){return array('InValidInput');}
            if($distanceToXthNumber != '0')
            {
                $xthNumber = $this->neighboursLeft($number,$distanceToXthNumber);
                $xthNumber = $xthNumber[$distanceToXthNumber];
            }
            else
            {
                return $number;
            }
    
            //find the payout
            return $xthNumber;
        }
    }
    
    
    
    
    
    //1000861505 
    

    库中的功能 1.检查数字是否为有效数字的函数(输入:数字|输出:真/假)[锚:1num]

    1. 基于给定数字返回颜色的功能(输入:数字|输出:颜色)[锚点:2color]
    2. 找到给定数字右边的邻居的功能(输入:数字,邻居数量|输出:数字数组)[anchor:neirside]
    3. 找到给定数字左边的邻居的功能(输入:数字,邻居数量|输出:数字数组)[anchor:neiLside]
    4. 为游戏返回利润n payoutAmount的函数(输入:数组(投入数字),数组(投入金额),中奖号码|输出:数组(amountReceivedAtTheEnd,profit,lossAmount,investAmount))[anchor:xPayoutProfit]
    5. 在右边找到第x个邻居的功能(输入:number,distanceToXthNumber |输出:数字)[anchor:XthNeiRside]
    6. 在左侧找到第x个邻居的功能(输入:number,distanceToXthNumber |输出:数字)[anchor:XthNeiLside]

    7. 找到邻居距离的功能(时钟方式)(输入:number1,number2 |输出:中间的总数)

    8. 找到左边邻居距离的功能(反时钟)(输入:number1,number2 |输出:中间的总数)
    9. 知道该数字属于哪个函数(输入:数字|输出:打数或0)
    10. 查看数字是偶数还是奇数的函数(输入:数字|输出:偶数r奇数r 0)
    11. 找到Low1to18OrHigh19to36的功能(输入:中奖号码|输出:低或高或0)
    12. 查找第1个第2个或第3个列号的功能(输入:中奖号码|输出:列号或0)
    13. 返回决赛的功能(1,11,21,31,2,22,32 ..)给定的数字(输入:中奖号码|输出:决赛号码)
    14. 返回车轮扇区的功能(输入:中奖号码|输出:扇区名称[零游戏,零点邻居..])

1 个答案:

答案 0 :(得分:5)

您应该执行以下操作将其用作库 的 1。将库文件放在application / third_party文件夹中。

enter image description here

2.在application / libraries文件夹中创建另一个文件并扩展如下所示的类。

enter image description here

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 require_once APPPATH."/third_party/Prad_roulette.php"; 

class Roulette extends Prad_roulette { 

}

注意:如果需要在类中使用构造函数,请确保扩展父构造函数:

class Roulette extends Prad_roulette {
    public function __construct()
    {
        parent::__construct();
    }
}

3.最后加载您的库。

$this->load->library('roulette');

要检查库是否已加载,您可以执行method_exits函数,如下所示

if(method_exists($this->roulette,'isValidRouletteNumber')){ /* isValidRouletteNumber is the method of Prad_roulette class. */
        echo "Library is loaded successfully";
}else{
        echo "Couldn't load the library";
}