GithubHelp home page GithubHelp logo

subscribepro / doctrine-qbmocker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from michaelmoussa/doctrine-qbmocker

0.0 1.0 0.0 406 KB

A tool for easy mocking of Doctrine QueryBuilder objects to facilitate unit testing.

License: MIT License

PHP 100.00%

doctrine-qbmocker's Introduction

Build Status Coverage Status Latest Stable Version

Doctrine QueryBuilderMocker

What's this?

A tool for easy mocking of Doctrine QueryBuilder objects to facilitate unit testing.

Example

Suppose you have a collection of user accounts with documents looking like this:

{
    firstName: "John",
    lastName: "Doe",
    email: "[email protected]",
    country: "USA"
}

You have a User service class with a method that returns all names and e-mail address of users in a particular country, sorted by lastName, firstName:

public function getUsersByCountry($country)
{
    return $this->documentManager->createQueryBuilder('My\Document\User')
                                 ->select('firstName', 'lastName', 'email')
                                 ->field('country')->equals($country)
                                 ->sort('lastName', 'firstName')
                                 ->getQuery()
                                 ->execute();
}

Simple enough. Now how do we test it?

We could create a test database, add a few test records, then run the method to see if it retrieves the right data, but that's not ideal.

  1. Connecting to a database in a test is relatively slow. Too many such tests in a test suite can cause it to really drag.
  2. It only tests that you're getting the expected data back - not that your method is retrieving it correctly.
    • What if someone accidentally deletes the ->field('country')->equals($country)? Or changes it to ->gte($country)? Or removes the ->sort('lastName', 'firstName')?
    • Depending on what test records you created and the order in which you created them, you may still get the expected results back even though your method is not retrieving them the way you'd expect. Your test would still pass, but the code would be wrong.

The better way to test this is to mock the expected method calls and make sure the Query Builder is being used correctly.

But that ends up being pretty messy... take a look:

public function testCanGetSortedUsersByCountry()
{
    $mockQuery = $this->getMockBuilder('Doctrine\ODM\MongoDB\Query\Builder')
                      ->disableOriginalConstructor()
                      ->getMock();
    $mockQuery->expects($this->once())
              ->method('execute')
              ->will($this->returnValue('it works!'));
    
    $mockQueryBuilder = $this->getMockBuilder('Doctrine\ODM\MongoDB\Query\Builder')
                             ->disableOriginalConstructor()
                             ->getMock();
    $mockQueryBuilder->expects($this->at(0))
                     ->method('select')
                     ->with('firstName', 'lastName', 'email')
                     ->will($this->returnValue($mockQueryBuilder));
    $mockQueryBuilder->expects($this->at(1))
                     ->method('field')
                     ->with('country')
                     ->will($this->returnValue($mockQueryBuilder));
    $mockQueryBuilder->expects($this->at(2))
                     ->method('equals')
                     ->with('USA')
                     ->will($this->returnValue($mockQueryBuilder));
    $mockQueryBuilder->expects($this->at(3))
                     ->method('sort')
                     ->with('lastName', 'firstName')
                     ->will($this->returnValue($mockQueryBuilder));
    $mockQueryBuilder->expects($this->at(4))
                     ->method('getQuery')
                     ->will($this->returnValue($mockQuery));
    
    $mockDocumentManager = $this->getMockBuilder('Doctrine\ODM\MongoDB\DocumentManager')
                                ->disableOriginalConstructor()
                                ->getMock();
    $mockDocumentManager->expects($this->once())
                        ->method('createQueryBuilder')
                        ->with('My\Document\User')
                        ->will($this->returnValue($mockQueryBuilder));
    
    $service->setDocumentManager($mockDocumentManager);
    
    $this->assertSame('it works!', $this->service->getUsersByCountry('USA'));

Imagine a unit test class filled with dozens of those! Surely we can do better. How about this?

public function testCanGetSortedUsersByCountry()
{
    $qbm = new QueryBuilderMocker($this);
    $qbm->select('firstName', 'lastName', 'email')
        ->field('country')->equals($country)
        ->sort('lastName', 'firstName')
        ->getQuery()
        ->execute('it works!');
    $qb = $qbm->getQueryBuilderMock();
    
    $this->assertSame('it works!', $this->service->getUsersByCountry('USA'));
}

Short and concise! Notice that it looks almost identical to the calls being made in the actual service class: The only difference is that execute() accepts a parameter, which ends up becoming the value that the mocked call to execute() will return. Mocking a method's usage of the Query Builder (or vice-versa, for those of us who TDD) is as simple as copying-and-pasting.

Prefer MySQL? This library supports mocking Doctrine ORM query builders as well. Just use MMoussa\Doctrine\Test\ORM\QueryBuilderMocker instead of MMoussa\Doctrine\Test\ODM\MongoDB\QueryBuilderMocker!

Installation

The only supported method of installation is Composer: composer require --dev "michaelmoussa/doctrine-qbmocker"

Dependencies

  • Using this library for mocking the ORM query builder requires you to composer require --dev "doctrine/orm" as well.
  • Using this library for mocking the ODM query builder requires you to composer require --dev "doctrine/mongodb-odm" "jmikola/geojson" as well.

PHPUnit support

  • Added support for PHPUnit 6 in version 1.0
  • Older PHPUnit versions are supported in versions 0.x

Contributing

I am no longer actively maintaining this project, but I do review and merge Pull Requests, so contributions are welcome. There are several unsupported methods still that would be non-trivial to implement. Please feel free to take care of those and send a PR.

All contributions must conform to PSR2 and include 100% test coverage.

doctrine-qbmocker's People

Contributors

michaelmoussa avatar kingjerod avatar theorx avatar spdionis avatar hickeroar avatar spiffyjr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.