1

I am having the below PHP class file. How do I create a TestCase or TestClass for this

<?php

class CRM_History_Page_HistoryPage extends CRM_Core_Page {

  public function run() {
    // Get User name from the contact id
    $query="select display_name from civicrm_contact where id={$_GET['cid']}";    
    $dao = CRM_Core_DAO::executeQuery($query);
    $dao->fetch();
    $username=$dao->display_name;
    CRM_Utils_System::setTitle(ts("$username - Membership Periods"));

    $this->assign('periods',$this->getMembershipPeriod($_GET['membership_id'])); 
    parent::run();
  }
  public function getMembershipPeriod($membership_id) {

    $query="SELECT pr.start_date,pr.end_date, p.contribution_id,p.membership_id 
        FROM civicrm_membershipperiod pr 
        INNER JOIN civicrm_membership_payment p ON pr.membership_id= p.membership_id
        where p.membership_id=$membership_id";

 $query="SELECT pr.start_date,pr.end_date
        FROM civicrm_membershipperiod pr 
        where pr.membership_id=$membership_id";

    $dao = CRM_Core_DAO::executeQuery($query);
    $i=0;$cid=$_GET['cid'];
    $validation_array=array();
    while ($dao->fetch()) {
      $data=[];
      $data['sno']=++$i;
      $data['start_date']=$dao->start_date;
      $data['end_date']=$dao->end_date;

//      $link="<a title='View Contribution' class='action-item crm-hover-button' href='./contact/view/contribution?reset=1&id=$contribution_id&cid=$cid&action=view&context=home&selectedChild=contribute'>Deatails</a>";

      $membership[]=$data;
  }

    return $membership;
  }
}

I have wrote a test class as below

<?php

use Civi\Test\HeadlessInterface;
use Civi\Test\HookInterface;
use Civi\Test\TransactionalInterface;



#use PHPUnit\Framework\TestCase;

/**
 * @covers Email
 */
class CRM_History_Page_HistoryPageTest extends \PHPUnit_Framework_TestCase
{
    public function testCanBeCreatedFromRun()
    {
        $this->assertInstanceOf(
            CRM_History_Page_HistoryPage::class,
            CRM_History_Page_HistoryPage::run(111)
        );
    }
}

But it gives the below error

root@mani-Lenovo-G560:/opt/lampp/htdocs/drupal/sites/all/modules/civicrm/membership.history# phpunit4 --bootstrap CRM/History/Page/HistoryPage.php tests/phpunit/CRM/History/TestClass.php

Fatal error: Class 'CRM_Core_Page' not found in /opt/lampp/htdocs/drupal/sites/all/modules/civicrm/membership.history/CRM/History/Page/HistoryPage.php on line 3
root@mani-Lenovo-G560:/opt/lampp/htdocs/drupal/sites/all/modules/civicrm/membership.history# 

Is there any dependency files missing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.