Unit Testing Zend Controller and Offset some of the actions performed

I am writing some unit tests (PHPUnit 3.6) for my controllers and want to check that the correct actions are running, etc. It is quite simple. However, some of the controllers also perform certain actions using unwanted models, such as inserting records into the database.

I know that I need to mock them, but it is not clear how to do this. Taking the following controller example (cut out for clarity):

public function addAction()
{
    $data = $this->getRequest()->getPost();
    $model = $this->getModelFactory()->getCompetitionModel()->insert($data);    }

}

Note. All I want to do is verify that the correct controller and action has been sent, but do not want the record to actually be inserted. Similarly, I have equivalents for deletion, etc. I don’t want the records to actually be deleted.

What really needs to be taunted here? Competitive model, database adapter or factory model, or all three? How to do it? I tried (shortened again for short):

public function testAddActionIsDispatched()
{
    $this->request->setMethod('POST');
    $this->request->setPost(array($data…));

            $modelMock = $this->getMockBuilder('Competition_Adder')
                 ->disableOriginalConstructor()
                 ->getMock();                


            $factoryMock = $this->getMockBuilder('ModelFactory')
                    ->disableOriginalConstructor()
                    ->getMock(); 

        // Configure the stub.
            $factoryMock->expects($this->any())
                ->method('getCompetitionModel')
                ->will($this->returnValue($modelMock));        

            $modelMock->expects($this->once())
                    ->method('insert')
                    ->will($this->returnValue(true));

            $this->dispatch('/mymodule/add/');
            $this->assertController('test');
            $this->assertAction('add');  
            $this->assertResponseCode(200);
}

}

I realized that PHPUnit magically replaced any links to the originals with mocks, so that when the submission was called, fake bullying is used in their place. This is not happening. Can someone clarify how this is achieved?

+5
source share
2 answers

It looks like your layouts are set up correctly. I really didn’t know that you could return the mockery of ridicule until I saw your question and studied it.

, , getModelFactory() factory. .

, getModelFactory, , , mock factory.

, , . ZF , , , . Doctrine1.2, setUp() tearDown().

, factory. , , , . , 200 140 , .

, , . , . , sqlite , , . , . MySQL, , , , .

(, Doctrine. , , ):

public function setUp()
{
    $this->bootstrap = new Zend_Application(
        APPLICATION_ENV, APPLICATION_CONFIG);       
    parent::setUp();
    $bootstrap = $this->bootstrap->getBootstrap();

    $this->_conn = Doctrine_Manager::connection();
    $this->_conn->beginTransaction();
}

public function tearDown()
{
    $this->_conn->rollback();        
    $this->_conn->close();    
}
+3

, . . MySQL Zend_Db. , Zend_Db . , , .

Codeception Zend Framework Db.

0

All Articles