Skip to main content
Re-tagged.
Link
Matt Stein
  • 4k
  • 3
  • 27
  • 57
deleted 14 characters in body; edited title
Source Link

Stub out the global craft() object for PHPUnit tests...

As best I can tell, the 'mockBuilder' function is exactly what I want, and based on other examples, should work.

HowAm I missing something? How can I stub out the global craft() object?

###under test: <?php

namespace Craft;


class JobsVariable
{

     // Grab all Jobss
     public function jobs()
      {
            return craft();
      }
 }

###test file: <?php

    namespace Craft;
    
    require_once '/home/stephen/Projects/EllationWebsite/www'www/craftcms/plugins/jobs/variables/JobsVariable.php';
    
    
    class JobsVariableTest extends \PHPUnit_Framework_TestCase
    {
    
    
    
        public function testJobs()
        {
    
    
            $craft = $this->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()

Stub out the global craft() object for PHPUnit tests...

As best I can tell, the 'mockBuilder' function is exactly what I want, and based on other examples, should work.

How can I stub out the global craft() object?

###under test: <?php

namespace Craft;


class JobsVariable
{

     // Grab all Jobss
     public function jobs()
      {
            return craft();
      }
 }

###test file: <?php

    namespace Craft;
    
    require_once '/home/stephen/Projects/EllationWebsite/www/craftcms/plugins/jobs/variables/JobsVariable.php';
    
    
    class JobsVariableTest extends \PHPUnit_Framework_TestCase
    {
    
    
    
        public function testJobs()
        {
    
    
            $craft = $this->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()

Stub out the global craft() object for PHPUnit tests

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: <?php

namespace Craft;


class JobsVariable
{

     // Grab all Jobss
     public function jobs()
      {
            return craft();
      }
 }

###test file: <?php

    namespace Craft;
    
    require_once 'www/craftcms/plugins/jobs/variables/JobsVariable.php';
    
    
    class JobsVariableTest extends \PHPUnit_Framework_TestCase
    {
    
    
    
        public function testJobs()
        {
    
    
            $craft = $this->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()

Source Link

Stub out the global craft() object for PHPUnit tests...

As best I can tell, the 'mockBuilder' function is exactly what I want, and based on other examples, should work.

How can I stub out the global craft() object?

###under test: <?php

namespace Craft;


class JobsVariable
{

     // Grab all Jobss
     public function jobs()
      {
            return craft();
      }
 }

###test file: <?php

    namespace Craft;
    
    require_once '/home/stephen/Projects/EllationWebsite/www/craftcms/plugins/jobs/variables/JobsVariable.php';
    
    
    class JobsVariableTest extends \PHPUnit_Framework_TestCase
    {
    
    
    
        public function testJobs()
        {
    
    
            $craft = $this->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()