Changeset 1284

Show
Ignore:
Timestamp:
01/23/08 14:44:47 (5 months ago)
Author:
mikey
Message:

changed phpunit performance optimization test to keep bc
added test cases

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/experiments/general/PHPUnit/phpunit_mock.patch

    r1283 r1284  
    11Index: PHPUnit/Framework/TestCase.php 
    22=================================================================== 
    3 --- PHPUnit/Framework/TestCase.php  (revision 2263
     3--- PHPUnit/Framework/TestCase.php  (revision 2269
    44+++ PHPUnit/Framework/TestCase.php  (working copy) 
    5 @@ -621,18 +621,28 @@ 
     5@@ -217,6 +217,12 @@ 
     6     protected $mockObjects = array(); 
     7  
     8     /** 
     9+     * @var    Array 
     10+     * @access protected 
     11+     */ 
     12+    protected static $mockClasses; 
     13
     14+    /** 
     15      * Constructs a test case with the given name. 
     16      * 
     17      * @param  string $name 
     18@@ -621,18 +627,28 @@ 
    619             throw new InvalidArgumentException; 
    720         } 
     
    1528-          $callAutoload 
    1629-        ); 
    17 +        if ('' == $mockClassName) { 
    18 +            $mockClassName = 'Mock' . $className; 
    19 +        } 
    20   
    21 -        $mockClass  = new ReflectionClass($mock->mockClassName); 
    22 -        $mockObject = $mockClass->newInstanceArgs($arguments); 
    23 +        if (class_exists($mockClassName, false) === false) { 
     30+        $key = md5($className . serialize($methods)); 
     31+        if (isset(self::$mockClasses[$key]) === false) { 
    2432+            $mock = PHPUnit_Framework_MockObject_Mock::generate( 
    2533+              $className, 
     
    3038+              $callAutoload 
    3139+            ); 
     40+             
     41+            self::$mockClasses[$key] = $mock->mockClassName; 
    3242+        } 
    3343  
     44-        $mockClass  = new ReflectionClass($mock->mockClassName); 
     45-        $mockObject = $mockClass->newInstanceArgs($arguments); 
     46  
    3447+        if (count($arguments) === 0) { 
    35 +            $mockObject = new $mockClassName(); 
     48+            $mockObject = new self::$mockClasses[$key](); 
    3649+        } else { 
    37 +            $mockClass  = new ReflectionClass($mockClassName); 
     50+            $mockClass  = new ReflectionClass(self::$mockClasses[$key]); 
    3851+            $mockObject = $mockClass->newInstanceArgs($arguments); 
    3952+        } 
     
    4255  
    4356         return $mockObject; 
     57Index: PHPUnit/Tests/_files/PartialMockTestClass.php 
     58=================================================================== 
     59--- PHPUnit/Tests/_files/PartialMockTestClass.php   (revision 0) 
     60+++ PHPUnit/Tests/_files/PartialMockTestClass.php   (revision 0) 
     61@@ -0,0 +1,71 @@ 
     62+<?php 
     63+/** 
     64+ * PHPUnit 
     65+ * 
     66+ * Copyright (c) 2002-2008, Sebastian Bergmann <sb@sebastian-bergmann.de>. 
     67+ * All rights reserved. 
     68+ * 
     69+ * Redistribution and use in source and binary forms, with or without 
     70+ * modification, are permitted provided that the following conditions 
     71+ * are met: 
     72+ * 
     73+ *   * Redistributions of source code must retain the above copyright 
     74+ *     notice, this list of conditions and the following disclaimer. 
     75+ * 
     76+ *   * Redistributions in binary form must reproduce the above copyright 
     77+ *     notice, this list of conditions and the following disclaimer in 
     78+ *     the documentation and/or other materials provided with the 
     79+ *     distribution. 
     80+ * 
     81+ *   * Neither the name of Sebastian Bergmann nor the names of his 
     82+ *     contributors may be used to endorse or promote products derived 
     83+ *     from this software without specific prior written permission. 
     84+ * 
     85+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
     86+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
     87+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
     88+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
     89+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
     90+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
     91+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
     92+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
     93+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
     94+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
     95+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
     96+ * POSSIBILITY OF SUCH DAMAGE. 
     97+ * 
     98+ * @category   Testing 
     99+ * @package    PHPUnit 
     100+ * @author     Sebastian Bergmann <sb@sebastian-bergmann.de> 
     101+ * @author     Frank Kleine <mikey@stubbles.net> 
     102+ * @copyright  2002-2008 Sebastian Bergmann <sb@sebastian-bergmann.de> 
     103+ * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License 
     104+ * @version    SVN: $Id: AnInterface.php 1985 2007-12-26 18:11:55Z sb $ 
     105+ * @link       http://www.phpunit.de/ 
     106+ * @since      File available since Release 3.2.12 
     107+ */ 
     108+ 
     109+require_once 'PHPUnit/Util/Filter.php'; 
     110+ 
     111+PHPUnit_Util_Filter::addFileToFilter(__FILE__); 
     112+ 
     113+/** 
     114+ * 
     115+ * 
     116+ * @category   Testing 
     117+ * @package    PHPUnit 
     118+ * @author     Sebastian Bergmann <sb@sebastian-bergmann.de> 
     119+ * @author     Frank Kleine <mikey@stubbles.net> 
     120+ * @copyright  2002-2008 Sebastian Bergmann <sb@sebastian-bergmann.de> 
     121+ * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License 
     122+ * @version    Release: @package_version@ 
     123+ * @link       http://www.phpunit.de/ 
     124+ * @since      Class available since Release 3.2.12 
     125+ */ 
     126+class PartialMockTestClass 
     127+{ 
     128+    public function doSomething() { } 
     129+     
     130+    public function doAnotherThing() { } 
     131+} 
     132+?> 
     133Index: PHPUnit/Tests/Framework/MockObjectTest.php 
     134=================================================================== 
     135--- PHPUnit/Tests/Framework/MockObjectTest.php  (revision 2269) 
     136+++ PHPUnit/Tests/Framework/MockObjectTest.php  (working copy) 
     137@@ -47,6 +47,7 @@ 
     138 require_once 'PHPUnit/Framework/TestCase.php'; 
     139  
     140 require_once '_files/AnInterface.php'; 
     141+require_once '_files/PartialMockTestClass.php'; 
     142  
     143 /** 
     144  * 
     145@@ -55,6 +56,7 @@ 
     146  * @package    PHPUnit 
     147  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de> 
     148  * @author     Patrick M??ller <elias0@gmx.net> 
     149+ * @author     Frank Kleine <mikey@stubbles.net> 
     150  * @copyright  2002-2008 Sebastian Bergmann <sb@sebastian-bergmann.de> 
     151  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License 
     152  * @version    Release: @package_version@ 
     153@@ -145,5 +147,29 @@ 
     154  
     155         $this->assertEquals('something', $mock->doSomething()); 
     156     } 
     157+ 
     158+    public function testMockClassOnlyGeneratedOnce() 
     159+    { 
     160+        $mock1 = $this->getMock('AnInterface'); 
     161+        $mock2 = $this->getMock('AnInterface'); 
     162+        $this->assertEquals(get_class($mock1), get_class($mock2)); 
     163+    } 
     164+ 
     165+    public function testMockClassDifferentForPartialMocks() 
     166+    { 
     167+        $mock1 = $this->getMock('PartialMockTestClass'); 
     168+        $mock2 = $this->getMock('PartialMockTestClass', array('doSomething')); 
     169+        $mock3 = $this->getMock('PartialMockTestClass', array('doSomething')); 
     170+        $mock4 = $this->getMock('PartialMockTestClass', array('doAnotherThing')); 
     171+        $mock5 = $this->getMock('PartialMockTestClass', array('doAnotherThing')); 
     172+        $this->assertNotEquals(get_class($mock1), get_class($mock2)); 
     173+        $this->assertNotEquals(get_class($mock1), get_class($mock3)); 
     174+        $this->assertNotEquals(get_class($mock1), get_class($mock4)); 
     175+        $this->assertNotEquals(get_class($mock1), get_class($mock5)); 
     176+        $this->assertEquals(get_class($mock2), get_class($mock3)); 
     177+        $this->assertNotEquals(get_class($mock2), get_class($mock4)); 
     178+        $this->assertNotEquals(get_class($mock2), get_class($mock5)); 
     179+        $this->assertEquals(get_class($mock4), get_class($mock5)); 
     180+    } 
     181 } 
     182 ?>