Memory App
Programming
 





<?php

    function isEven($n) { return $n%2 == 0; }
    function isOdd($n) { return    $n%2 != 0; }

    {
        ini_set("include_path", ini_get("include_path") . ";PHPUnit-1.3.2");
        require "PHPUnit.php";

        class TestCase extends PHPUnit_TestCase {
            
            public function testEven() {
                $this->assertEquals(isEven(2), true);
                $this->assertEquals(isEven(3), false);
            }

            public function testOdd() {
                $this->assertEquals(isOdd(2), false);
                $this->assertEquals(isOdd(3), true);
            }
        }

        $suite = new PHPUnit_TestSuite("TestCase"); // automaticaly add methods that starts with 'test'
        print PHPUnit::run($suite)->toHTML();

        // TestCase TestCase->testEven() passed
        // TestCase TestCase->testOdd() passed
    }

PHPUnit-4.8.21 (phar)


<?php
    include("PHPUnit-4.8.21phpunit-4.8.21.phar");
    include("PHPUnit-4.8.21TestSuite.php");

    class TestCase extends PHPUnit_Framework_TestCase 
    {
        public function test() 
        {
            $this->assertTrue(isEven(2));
            $this->assertFalse(isEven(3));
            $this->assertTrue(isEven(43));
        }
    }

    function isEven($n) 
    {
        return $n % 2 == 0;
    }

    $test = new TestSuite("TestCase");
    $result = $test->run();
    echo $result->getMessage();
(1/2)

TestSuite.php


<?php
    class TestSuite extends PHPUnit_Framework_TestSuite  
    {
        protected $result;

        public function run(PHPUnit_Framework_TestResult $result = NULL)
        {
             $this->result = parent::run($result); //var_dump($result);

             return $this; // retrun (TestSuite) object
        }

        public function getMessage() 
        {            
            $failures = $this->getNodeValue($this->result, "failures");
            
            $message = isset($failures[0]) 

                ? $this->getNodeValue($failures[0], "failedTest/statusMessage") 

                : key($this->getNodeValue($this->result, "passed")) . " passed";

            return $message;
        }

        protected function getNodeValue($obj, $path) {
            
            foreach(explode("/", $path) as $node) {

                $reflector = new ReflectionObject($obj); // use Reflection to get nodes from object
                
                try {
                    $node = $reflector->getProperty($node);

                } catch (Exception $e) {

                    $node = $reflector->getParentClass()->getProperty($node);
                }

                $node->setAccessible(true);

                $obj = $node->getValue($obj);
            }

            return $obj;
        }
    }
http://pear.php.net/package/PHPUnit/download

Questions



Top Reference > Programming
0/0 (80)  
Not Logged