<p>Whew...this seems to do the trick!
Hopefully this is an appropriate way to handle things.</p>

<p>I created a new Craft class file in the 'tests' dir.  There I can stub out any <code>\Craft\craft()</code> methods the class under test depends on.</p>

<h3>stub class \tests\Craft</h3>

<pre><code>&lt;?php

class Craft {

    public function __construct()
    {
    }

    public function jobs()
    {
        return 'xxx';
    }
    public function getJobs()
    {
        return 'xxx';
    }

    public function craft()
    {
        return $this;
    }
}
</code></pre>

<h3>in the test class</h3>

<pre><code>&lt;?php

namespace Craft;

require_once '/www/craftcms/plugins/jobs/variables/JobsVariable.php';
require_once '/www/craftcms/tests/Craft.php';


class JobsVariableTest extends \PHPUnit_Framework_TestCase
{

    public function testJobs()
    {
        $jobs = new \Craft\JobsVariable();
        $this-&gt;assertEquals('xxx', $jobs-&gt;jobs());

    }
}
function craft()
{
    return new \Craft;
}
</code></pre>

<h3>allows me to test this function...</h3>

<pre><code>public function jobs()
{
    return craft()-&gt;jobs();
}
</code></pre>

CC BY-SA 3.0
2015-06-06T21:06:28.037Z
2015-06-06T21:06:28.037Z
nullsteph
<p>...and because the actual test method to get mocked looks like this:
<code>craft()-&gt;jobs-&gt;getJobs()</code></p>

<p>I can mock this up by adding a class to my stubbed out \test\Craft class;</p>

<pre><code>&lt;?php

class Craft {

    public $jobs;

    public function __construct()
    {
        $this-&gt;jobs = new Jobs;
    }
}


class Jobs{

    public function __construct(){
        var_dump('new jobs');
    }


    public function getJobs(){
        return 'xxx';
    }
}
</code></pre>

<p>Now I can return dummy test data ('xxx'), instead of letting the code make  network requests for real data.</p>

CC BY-SA 3.0
2015-06-06T22:13:09.447Z
2015-06-06T22:13:09.447Z
nullsteph
<p>As best I can tell, the 'mockBuilder' function is exactly what I want, and based on other examples, should work.  </p>

<p>Am I missing something?
How can I stub out the global craft() object?</p>

<h3>under test:</h3>

<pre><code>&lt;?php

namespace Craft;


class JobsVariable
{

     // Grab all Jobss
     public function jobs()
      {
            return craft();
      }
 }
</code></pre>

<h3>test file:</h3>

<pre><code>    &lt;?php

    namespace Craft;

    require_once 'www/craftcms/plugins/jobs/variables/JobsVariable.php';


    class JobsVariableTest extends \PHPUnit_Framework_TestCase
    {



        public function testJobs()
        {


            $craft = $this-&gt;getMockBuilder('\\Craft')
                -&gt;disableOriginalConstructor()
                -&gt;setMethods(array('craft'))
                -&gt;getMock();


            $craft-&gt;method('craft')-&gt;willReturn('xxx');


            $jobs = new \Craft\JobsVariable();
            $this-&gt;assertEquals('xxx', $jobs-&gt;jobs());

        }
    }
</code></pre>

<h3>output:</h3>

<pre><code>PHP Fatal error:  Call to undefined function Craft\craft()
</code></pre>

CC BY-SA 3.0
2017-05-09T23:57:46.517Z
This is a rather extensive self-conversation @nullsteph! Would you kindly accept whichever of your answers you feel is best?
Matt Stein
<php><tests>
Stub out the global craft() object for PHPUnit tests
# Stub out the global craft() object for PHPUnit tests

(Asked by: username_0 on 2015-06-06)

As best I can tell, the 'mockBuilder' function is exactly what I want, and based on other examples, should work.  
Am I missing something?
How can I stub out the global craft() object?
under test:

test file:
    getMockBuilder('\\Craft')
                ->disableOriginalConstructor()
                ->setMethods(array('craft'))
                ->getMock();


            $craft->method('craft')->willReturn('xxx');


            $jobs = new \Craft\JobsVariable();
            $this->assertEquals('xxx', $jobs->jobs());

        }
    }

output:
PHP Fatal error:  Call to undefined function Craft\craft()

**Tags:** php, tests

> **Comments:**
> **username_1**: This is a rather extensive self-conversation @username_0! Would you kindly accept whichever of your answers you feel is best?  

---

## 2 answers

### Answer by username_0 on 2015-06-06. Score: 1

Whew...this seems to do the trick!
Hopefully this is an appropriate way to handle things.
I created a new Craft class file in the 'tests' dir.  There I can stub out any \Craft\craft() methods the class under test depends on.
stub class \tests\Craft

in the test class
assertEquals('xxx', $jobs->jobs());

    }
}
function craft()
{
    return new \Craft;
}

allows me to test this function...
public function jobs()
{
    return craft()->jobs();
}


---

### Answer by username_0 on 2015-06-06. Score: 1

...and because the actual test method to get mocked looks like this:
craft()->jobs->getJobs()
I can mock this up by adding a class to my stubbed out \test\Craft class;
jobs = new Jobs;
    }
}


class Jobs{

    public function __construct(){
        var_dump('new jobs');
    }


    public function getJobs(){
        return 'xxx';
    }
}

Now I can return dummy test data ('xxx'), instead of letting the code make  network requests for real data.


---

