Пример #1
0
 public function testReturnsNullIfNoClassesForMethod()
 {
     // entry conditions
     $mixins = MF_Obj_MixinsManager::getMixinsFor('Test_ObjExt');
     // try and get a method that does not exist
     $this->assertNull($mixins->getClassnamesForMethod('doesNotExist'));
 }
Пример #2
0
 public function testCanAddAMixin()
 {
     // entry conditions
     MF_Obj_MixinsManager::destroy();
     // var_dump("Slate wiped clean; starting again");
     $this->fixture = new Test_ObjExt();
     $this->assertEquals('Test_ObjExt', get_class($this->fixture));
     $this->assertEquals(0, $this->fixture->mixinsCount);
     // change state
     __mf_extend('Test_ObjExt', 'Test_Obj_ExtMixin');
     $this->assertEquals(1, $this->fixture->mixinsCount);
     $this->assertEquals('Test_Obj_ExtMixin', $this->fixture->doSomethingInTheMixin());
     // we deliberately extend the base class of the fixture,
     // rather than the class of the fixture, as it is much
     // more of a torture test!
     __mf_extend('Test_ObjBase', 'Test_Obj_BaseMixin');
     // __mf_extend('Test_ObjExt', 'Test_Obj_BaseMixin');
     // retest
     $this->assertEquals(2, $this->fixture->mixinsCount);
     // $this->assertEquals($expectedMethods, $this->fixture->getMixinMethods());
     $this->assertEquals('Test_Obj_ExtMixin', $this->fixture->doSomethingInTheMixin());
     $this->assertEquals('Test_Obj_BaseMixin', $this->fixture->doSomethingInTheBaseMixin());
     // add in another mixin
     __mf_extend('Test_ObjExt', 'Test_Obj_ParentMixin');
     // retest
     $this->assertEquals(3, $this->fixture->mixinsCount);
     // now add in a mixin that's a child class of an existing
     // mixin that is in use
     __mf_extend('Test_ObjExt', 'Test_Obj_ChildMixin');
     // retest
     $this->assertEquals(4, $this->fixture->mixinsCount);
 }
Пример #3
0
/**
 * Add a mixin to the class hierarchy, or extend one object with
 * a decorator
 *
 * @param mixed $classOrObject class or object to extend
 * @param string $extensionClassOrObject class to mix into $classOrObject
 */
function __mf_extend($classOrObject, $extensionClassOrObject)
{
    if (is_object($classOrObject)) {
        // we are adding a decorator to an object
        constraint_mustBeExtensible($classOrObject);
        constraint_mustBeObject($extensionClassOrObject);
        $classOrObject->addDecorator($extensionClassOrObject);
    } else {
        // we are adding a mixin to the class hierarchy
        MF_Obj_MixinsManager::extend($classOrObject)->withClass($extensionClassOrObject);
    }
}
Пример #4
0
 public function testCanGetRawMixinsLists()
 {
     // entry conditions
     $mixins = MF_Obj_MixinsManager::getRawMixins();
     $this->assertEquals(0, MF_Obj_MixinsManager::$mixinAutoInc);
     $this->assertTrue(is_array($mixins));
     if (is_array($mixins)) {
         $this->assertEquals(0, count($mixins));
     }
     // make the changes
     __mf_extend('Test_ObjExt', 'Test_Obj_ExtMixin');
     // retest
     $mixins = MF_Obj_MixinsManager::getRawMixins();
     $this->assertEquals(1, MF_Obj_MixinsManager::$mixinAutoInc);
     $this->assertTrue(is_array($mixins));
     if (is_array($mixins)) {
         $this->assertEquals(1, count($mixins));
         $this->assertTrue(isset($mixins['Test_ObjExt']));
     }
 }
Пример #5
0
 public function updateMixinsCountFromMixins($extensionClass)
 {
     $classHierarchy = MF_Obj_MixinsManager::getBaseclassesForClass($extensionClass);
     foreach ($classHierarchy as $classname) {
         // var_dump($this->name . ": Looking at the mixins count for $classname; current mixins count is " . $this->mixinsCount);
         $mixins = MF_Obj_MixinsManager::getMixinsFor($classname);
         if ($mixins == null) {
             continue;
         }
         $this->mixinsCount += $mixins->getMixinsCount();
         // var_dump($this->name . ": Mixins count now increased to " . $this->mixinsCount);
         // the mixin will include how many mixins its
         // baseclasses have defined ... we need go no further
         break;
     }
 }
Пример #6
0
 public static function destroy()
 {
     self::$mixins = array();
     self::$mixinAutoInc = 0;
 }
Пример #7
0
 protected function findObjsForMethod($methodName)
 {
     // we are specifically looking for all of the objects
     // that provide the same method
     $return = array();
     // check the mixins first
     $mixins = MF_Obj_MixinsManager::getMixinsFor($this->extensibleName);
     if ($mixins !== null) {
         // var_dump($mixins);
         $classes = $mixins->getClassnamesForMethod($methodName);
         if ($classes !== null) {
             foreach ($classes as $class) {
                 $return[] = $this->getMixinObject($class);
             }
         }
     }
     // now, what about our decorators too?
     if (isset($this->decorators['objs'])) {
         foreach ($this->decorators['objs'] as $decorator) {
             if (method_exists($decorator, $methodName)) {
                 $return[] = $decorator;
             }
         }
     }
     return $return;
 }