samedi 28 décembre 2019

Is MOCKING really useful apart from isolating dependent component testing?

So i am developing a security library for Wordpress. Basically building on top of a wordpress core component called Wordpress nonces
Everything seems to be fine.
Come back to the best practices, i have written a unit testing suite along with the library.
Since my library extends the wordpress noces api, wordpress is clearly a dependency in my library.

To achieve isolation when testing my library i used PHPUnit's mockBuilder so i can instantiate my dependency class instead of actually instantiating it because it calls an external dependency(wordpress nonce api).
Below is my Test class,

<?php
namespace nonces;

/**
 * TestWpNonce
 * Test suite for wp-clean-nonces library
 *
 * Wp-clean-nonces is A clean OOP implementation of the Wordpress Nonce library.
 * Copyright (C) 2019  Salim Said.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * For the complete terms of the GNU General Public License, please see this URL:
 * http://www.gnu.org/licenses/gpl-2.0.html

 * @category  NA
 * @package   NA
 * @author    Salim Said <saliimsaid@gmail.com>
 * @copyright 2019 Salim Said
 * @license   GPL-2.0+ <http://www.gnu.org/licenses/gpl-2.0.html>
 * @link      ****
 */

if(file_exists('../vendor/autoload.php'))
require_once '../vendor/autoload.php';

use PHPUnit\Framework\TestCase;
class TestWpNonce extends TestCase
{
    /**
     * This test will create a nonce for a url and verify if a correct
     * url is returned
     *
     * The method asserts for the result returned by createTokenUrl()
     * against a predefined expected return value containing a nonce
     * protected string url
     *
     * @return void
     */
    public function testCreateTokenUrl()
    {
        $wpNonce = $this->getMockBuilder(WPNonce::class)
            ->getMock();

        $wpNonce->expects($this->once())
            ->method('createTokenUrl')
            ->with($this->equalTo("inpsyde.com?action=publish_postid=2"))
            ->will($this->returnValue('inpsyde.com?action=publish_postid=2&token=297d58f6ae'));

        $this->assertEquals(
            "inpsyde.com?action=publish_postid=2&token=297d58f6ae",
            $wpNonce->createTokenUrl("inpsyde.com?action=publish_postid=2")
        );
    }
}

Look at the testCreateTokenUrl() method; it creates a mock for the WPNonce class, and calls the class method createTokenUrl() . This is where things starts to get interesting.

I am defining the input parameter like so with($this->equalTo("inpsyde.com?action=publish_postid=2")) and also explicitly expecting a return value as in ->will($this->returnValue('inpsyde.com?action=publish_postid=2&token=297d58f6ae'));

The assertEquals() method will check if the returned value is what i set in the preceding code.

This code helps me test the createTokenUrl() in isolation, it doesn't require wordpress(my library's dependency in this case). I can successfully get passing tests without having wordpress installed. That's fine but really what is the whole point of mocking project dependencies if the mock is actually a dummy test that is hard coded with arguments and return types ?

Is there a point of mocking that i am missing or i don't understand ?

Aucun commentaire:

Enregistrer un commentaire