User Tools

Site Tools


kurs:entwickeln_mit_phpunit

Die Klasse

<?php

require_once 'PEAR/Exception.php';

class Employee {
  var $id, $last_name, $salary;
  var $db;

  function Employee() {
    $this->db_connect();
  }

  private function db_connect() {
    $this->db = oci_connect('hr', 'hr', 'orcl');
  }

  function set_last_name($last_name) {
    $this->last_name = $last_name;
  }

  function read() {
	$q = oci_parse($this->db,
      "select last_name ln, salary
	     from employees
         where last_name = :b_last_name");
    oci_bind_by_name
	   ($q, ":b_last_name", $this->last_name);
    $r = oci_execute($q, OCI_DEFAULT);
    $row = oci_fetch_array($q, OCI_ASSOC);
    if (!isset($row['LN'])) {
	  throw new PEAR_Exception("no employee ".$this->last_name
	    , 404);
	}
	
	$this->salary = $row['SALARY'];
	
  }

  function store() {}

  function __set($u, $a) {
    die("ERROR: you tried to set $u with $a");
  }
  
  function __get($u) {
    die("ERROR: you tried to get $u");
  }

  function __call($u, $a) {
    die("ERROR: you tried to call $u with $a");
  }
}

phpunit –skeleton-test Employee

<?php

require_once 'C:\xampp\htdocs\employee\Employee.php';

/**
 * Test class for Employee.
 * Generated by PHPUnit on 2012-01-11 at 15:27:31.
 */
class EmployeeTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var Employee
     */
    protected $object;

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        $this->object = new Employee;
    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    protected function tearDown()
    {
    }

    /**
     * @covers {className}::{origMethodName}
     * @todo Implement testSet_last_name().
     */
    public function testSet_last_name()
    {
	    $name = 'Bumsti';
        $this->object->last_name = $name;
		$this->assertEquals($this->object->last_name, $name);
    }

    /**
     * @covers {className}::{origMethodName}
     * @todo Implement testRead().
     */
    public function testStore()
    {
        // Remove the following lines when you implement this test.
        $this->markTestIncomplete(
          'This test has not been implemented yet.'
        );
    }

    /**
     * @covers {className}::{origMethodName}
     * @todo Implement testStore().
     */
    public function testRead()
    {	   
	   $name = 'Fay';
       $this->object->set_last_name($name);
	   $this->object->read();
	   $this->assertEquals(6000, $this->object->salary);
	}
	
	public function testReadFail()
	{
	   $this->setExpectedException('PEAR_Exception');
	   $name = 'Bumsti';
       $this->object->set_last_name($name);
	   $this->object->read();
	   
    }
}
?>

phpunit EmployeeTest.php

kurs/entwickeln_mit_phpunit.txt · Last modified: 2014/09/10 21:22 (external edit)