1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 
<?php
namespace Omeka\Test;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\QueryBuilder;
use Zend\ServiceManager\ServiceManager;

/**
 * Helper methods for building commonly used mock objects for testing.
 *
 * The idea is to first set the mock objects to variables and then set
 * their expectations according to the specific test case.
 */
class TestCase extends \PHPUnit_Framework_TestCase
{
    /**
     * Get a mock Zend\ServiceManager\ServiceManager (ServiceLocator) object.
     *
     * Pass mock service objects that should be accessible via the mocked
     * ServiceManager::get().
     *
     * @param array $services Where the key is the service name and the value
     * is the mock service object
     * @return ServiceManager
     */
    public function getServiceManager(array $services = [])
    {
        $serviceManager = $this->getMock('Zend\ServiceManager\ServiceManager');
        $serviceManager->expects($this->any())
            ->method('get')
            ->with($this->callback(function ($subject) use ($services) {
                return array_key_exists($subject, $services);
            }))
            ->will($this->returnCallback(function ($subject) use ($services) {
                return $services[$subject];
            }));
        return $serviceManager;
    }

    /**
     * Get a mock Doctrine\ORM\EntityManager object.
     *
     * @return EntityManager
     */
    public function getEntityManager()
    {
        $entityManager = $this->getMockBuilder('Doctrine\ORM\EntityManager')
            ->disableOriginalConstructor()
            ->getMock();
        return $entityManager;
    }

    public function getEntityRepository()
    {
        $entityRepostory = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
            ->disableOriginalConstructor()
            ->getMock();
        return $entityRepostory;
    }

    /**
     * Get a mock Doctrine\ORM\QueryBuilder object.
     *
     * @return QueryBuilder
     */
    public function getQueryBuilder()
    {
        $queryBuilder = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
            ->disableOriginalConstructor()
            ->getMock();
        return $queryBuilder;
    }

    /**
     * Get a mock Doctrine\ORM\Query object.
     *
     * Doctrine\ORM\Query cannot be mocked because it is declared "final".
     * Instead, mock stdClass and assign the requisite methods.
     *
     * @param array $methods
     * @return stdClass
     */
    public function getQuery(array $methods = [])
    {
        $query = $this->getMock('stdClass', $methods);
        return $query;
    }
}