PHP code example of jsiefer / class-mocker

1. Go to this page and download the library: Download jsiefer/class-mocker library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jsiefer / class-mocker example snippets


/**
 * My awesome sample plugin
 */
class MyPlugin extends Example_Namespace_AbstractPlugin
{
    /**
     * Return full name
     *
     * @return string
     */
    public function getName()
    {
        if (!$this->_isLoaded) {
            throw new Exception("Not yet loaded");
        }
        $firstname = $this->getFirstname();
        $lastname = $this->getLastname();

        return $firstname . ' ' . $lastname;
    }
}


/**
 * Sample PHPUnit bootstrap.php
 */
sMocker();
//$classMocker->mock('Example\Namespace\*');
$classMocker->mock('Example_Namespace_*');
$classMocker->enable();

/**
 * Class MyPluginTest
 */
class MyPluginTest extends PHPUnit_Framework_TestCase
{
    /**
     * Test the getName method
     *
     * @return void
     * @test
     */
    public function testGetName()
    {
        /** @var MyPlugin|PHPUnit_Framework_MockObject_MockObject $myPlugin */
        $myPlugin = new MyPlugin();
        $myPlugin->_isLoaded = true;
        $myPlugin->expects($this->once())->method('getFirstname')->willReturn('John');
        $myPlugin->expects($this->once())->method('getLastname')->willReturn('Snow');

        $this->assertEquals(
            'John Snow',
            $myPlugin->getName(),
            'getName() Plugin did not return correct full name'
        );
    }

    /**
     * Should throw exception if object is not loaded
     *
     * @test
     * @expectedException Exception
     * @expectedExceptionMessage Not yet loaded
     */
    public function shouldFailGetNameIfNotLoaded()
    {
        /** @var MyPlugin|PHPUnit_Framework_MockObject_MockObject $myPlugin */
        $myPlugin = new MyPlugin();
        $myPlugin->_isLoaded = false;

        // should throw Exception
        $myPlugin->getName();
    }
}