what method/function need to have test case ?

Hi

I am new to testing so I am not sure what class methods or functions need to have test cases and how to do the test. You can throw me a book about testing but I find it is easy if I take step by step in a practical manner.

here is my sample code




class Project extends \yii\db\ActiveRecord

{

    use \common\traits\commonTrait;

    

    const STATUS_ESTIMATED = 1;

    const STATUS_AWARDED = 2;

    const STATUS_COMPLETED = 3;

   

    public function __toString() {

        return $this->name;

    }

    

    /*

     * Return String - Project Location

     */

    public function getLocation() {

        $loc = array( (string) $this->city, (string) $this->state, (string) $this->country);

        

        return implode(', ',array_filter($loc));

    }

    

    /**

     * @return \yii\db\ActiveQuery

     */

    public function getCountry()

    {

        return $this->hasOne(Country::className(), ['id' => 'country_id']);

    }

    

    public function getClientPOs()

    {

        return $this->hasMany(ClientPo::className(), ['project_id' => 'id']);

    }

    

    /* Get Client POs in Text */

    public function getClientPOsText($glue = ', ')

    {

        $text = '';

        $clientPOs = $this->clientPOs;

       

       

        if (empty($clientPOs)) return $text;

        

        foreach($clientPOs as $clientPO) {

            $text[] = $clientPO->name;

        }


        return implode($glue , $text);

    }

    

      

    // $startDate, $endDate is YYYY-MM-DD format

    public function calculateActualQuantity($costCodeId = null, $startDate = null, $endDate = null)

    {

        $query = ClaimEntry::find()

            ->joinWith('claim', TRUE, 'INNER JOIN')

            ->joinWith('claimingRule', TRUE, 'INNER JOIN');

        

        $query->andFilterWhere([

            'claim.project_id' => $this->id,

            'claim.cost_code_id' => $costCodeId]);

        

        $query->andFilterWhere([

            'between', 'claim.date', $startDate, $endDate

        ]);

        

        return $query->sum('[[claim_amount]] * [[percent]]/100');

    }

    

}




Do I need to test and how do I test them ?

  • __toString() ?

  • getCountry() ?

  • getClientPOs() ?

  • getClientPOsText() ?

  • calculateActualQuantity() ?

help please

Well, you generally don’t need to test core functionality.

That said, if you want to be really sure that you’ve built your relationships properly and have full coverage, go ahead and test the relationships.

You certainly should test the calculateActualQuantity method.

Good luck with that ;)

Read a book. Search the net. Write some tests. Read blog posts, etc.

Codeception forum is a good place.

And read more blog posts about testing.

StackOverflow - Tags - try and enter ‘testing’ into it … ;)

thanks for reply guys. I try your advises.