Ugly code unit testing with PHPUnit and Doctrine2 (an ORM) I am trying to unit test a small class that I consider part of the "business-logic" layer of my project.  It mostly just interact's with Doctrine's entity manager and a couple of entities with a many-to-many association.  I'm finding that unit testing it while mocking out its dependencies is just ludicrous.  Just the code to set up mocks is going to be at least twice as long as the class itself.  What am I doing wrong, or should I just not bother doing the mocks and let it interact with a real test database?

Here is the class I am trying to test:

class UserPermissions{

    protected $em;

    public function __construct($entityManager){
        $this-&gt;em = $entityManager;
    }

    /**
     * returns user's permissions for all products or specific product
     *
     **/
    public function getFromUUID($uuid, $productId=NULL){

        $user = $this-&gt;em-&gt;find('\MyProject\Entity\User',$uuid);
        $product = $productId ? $this-&gt;em-&gt;find('\MyProject\Entity\Product',$productId) : NULL;

        if ($user===null) {
            return null;
        } else {
            $userProducts = $user-&gt;getUserProducts();
            $result = [];

            if ($productId) {
                $criteria = Criteria::create()
                    -&gt;where(Criteria::expr()-&gt;eq("product",$product))
                    ;
                $userProducts = $userProducts-&gt;matching($criteria);
            }

            for ($i = 0; $i &lt; count($userProducts); $i++) {
                $productI = $userProducts[$i];
                array_push($result, array(
                    "product" =&gt; $productI-&gt;getProduct()-&gt;getId(),
                    "permissions" =&gt; $productI-&gt;getPermissions(),
                ));
            }

            return $result;
        }
    }
}


And here is what my test class is starting to look like:

class UserPermissionsTest extends PHPUnit_Framework_TestCase
{
    protected $testService;
    protected $mockProduct;
    protected $mockUser;
    protected $mockUserProduct;

    protected function setUp(){

        $em = $this-&gt;getMockBuilder('\Doctrine\ORM\EntityManager')
                   -&gt;disableOriginalConstructor()
                             -&gt;getMock();

        $emMap = array(
            array('\MyProject\Entity\User','a',$this-&gt;mockUser),
            array('\MyProject\Entity\Product','b',$this-&gt;mockProduct),
        );

        $em-&gt;expects($this-&gt;any())
           -&gt;method('find')
             -&gt;will($this-&gt;returnValueMap($emMap));


//      $userMockBuilder = $this-&gt;getMockBuilder('\MyProject\Entity\User')
//                              -&gt;disableOriginalConstructor()

        $user = $this-&gt;getMockBuilder('\MyProject\Entity\User')
                     -&gt;disableOriginalConstructor()
                                 -&gt;getMock();

        $user-&gt;expects($this-&gt;any())
             -&gt;method('getUserProducts')
                 -&gt;will($this-&gt;returnValue($this-&gt;mockUserProduct);

        $this-&gt;mockUser=$user;

        $userProduct = $this-&gt;getMockBuilder('\MyProject\Entity\UserProduct')
                       -&gt;disableOriginalConstructor()
                                   -&gt;getMock();

        $userProduct-&gt;expects($this-&gt;any())
                    -&gt;method('matching')
                        -&gt;will($this-&gt;returnValue($this-&gt;mockProduct);

        $this-&gt;mockUserProduct = $userProduct;

        $product =$this-&gt;getMockBuilder('\MyProject\Entity\Product')
                     -&gt;disableOriginalConstructor()
                                 -&gt;getMock();

        $product-&gt;expects($this-&gt;any())
                    -&gt;method('getProduct')
                        -&gt;will($this-&gt;returnValue($this-&gt;mockProduct);


.... and so on.  It's obviously not done yet.
I decided that there was too much database access code in my UserPermissions class and split it into two classes.  The first class is now much easier to mock and the second class is just a wrapper for a doctrine query, which is probably almost pointless to mock.

class UserPermissions{

    protected $dbalUserProducts;

    public function __construct($dbalUserProducts){
        $this-&gt;dbalUserProducts= $dbalUserProducts;
    }

    /**
     * returns user's permissions for all products or specific product
     **/
    public function get($userId, $productId=NULL){

        $userProducts = $this-&gt;dbalUserProducts-&gt;get($userId, $productId);

        $result = [];

        for ($i = 0; $i &lt; count($userProducts); $i++) {
            $up = $userProducts[$i];
            array_push($result, array(
                "product" =&gt; $up-&gt;getProduct()-&gt;getId(),
                "permissions" =&gt; $up-&gt;getPermissions(),
            ));
        }

        return $result;
    }
}




class UserProductQuery
{
    protected $em;

    public function __construct($entityManager)
    {
        $this-&gt;em = $entityManager;
    }

    public function get($userId=null, $productId=null){
        $qb = $this-&gt;em-&gt;createQueryBuilder();

        $qb-&gt;select('u')
           -&gt;from('\MyProject\Entity\UserProduct', 'u');

        if ($userId) {
            $qb-&gt;andWhere($qb-&gt;expr()-&gt;eq('u.user', ':userId'));
            $qb-&gt;setParameter('userId',$userId);
        }
        if ($productId) {
            $qb-&gt;andWhere($qb-&gt;expr()-&gt;eq('u.product', ':productId'));
            $qb-&gt;setParameter('productId',$productId);
        }

        return($qb-&gt;getQuery()-&gt;getResult());
    }
}