Function Mocker new powers 02

Mocking traits in Function Mocker.

Quite an edge case

Since interfaces are so convenient I do not see much of a use case for mocking traits: those cannot be type-hinted and will usually be pulled into classes type-hinted via an interface.
Some use cases might exist though; so much that PHPUnit provides a getMockForTrait method.
I could not let the possibility slip from function-mocker powers.

Updated

I’ve updated the function-mocker repository to allow, since version 0.2.14, trait mocking; it’s quite hard to come up with a legit example bur here it is nonetheless:

trait Title {
    public function getTitleFrags() {
    $titleFrags = [];   
    foreach($this as $prop => $value) {
        if(strpos($prop, 'title_') === 0){
            $titleFrags[] = $value;
        }
    }

        return $title_frags;
    }
}

class Post {
    use Title;

public $title_display;  

    ...
}

class User {
    use Title;

    public $title_title;
    public $title_name;

    ...
}

class TitleBuilder {

   public function getPostTitle($post){
        return '<h3>' . implode(' ', $post->getTitleFrags()) . '</h3>';
   }

   public function getUserTitle($user){
        return '<h3>Person: ' . implode(' ', $user->getTitleFrags()) . '</h3>';
   }

} 

in the tests I will mock the Title trait

use tad\FunctionMocker\FunctionMocker as Test;

public function test_post_title() {
    $post = Test::replace('CacheFetcher::fetchFromCache', 'Lorem');

    $sut = new TitleBuilder();

    $this->assertEquals('<h3>Lorem</h3>', $sut->getPostTitle($post));
}

public function test_user_title() {
    $user = Test::replace('Title::getTitleFrags', function(){
        return ['Mr', 'John'];
        });

    $sut = new TitleBuilder();

    $this->assertEquals('<h3>Person: Mr John</h3>', $sut->getUserTitle($user));
}

and it will work.