public function haveFakeRepository($classname, $methods = [])
    {
        $em = $this->em;

        $metadata = $em->getMetadataFactory()->getMetadataFor($classname);
        $customRepositoryClassName = $metadata->customRepositoryClassName;

        if (!$customRepositoryClassName) {
            $customRepositoryClassName = '\Doctrine\ORM\EntityRepository';
        }

        $mock = Stub::make(
            $customRepositoryClassName,
            array_merge(
                [
                    '_entityName' => $metadata->name,
                    '_em' => $em,
                    '_class' => $metadata
                ],
                $methods
            )
        );

        $em->clear();
        $reflectedEm = new \ReflectionClass($em);


        if ($reflectedEm->hasProperty('repositories')) {
            //Support doctrine versions before 2.4.0

            $property = $reflectedEm->getProperty('repositories');
            $property->setAccessible(true);
            $property->setValue($em, array_merge($property->getValue($em), [$classname => $mock]));
        } elseif ($reflectedEm->hasProperty('repositoryFactory')) {
            //For doctrine 2.4.0+ versions

            $repositoryFactoryProperty = $reflectedEm->getProperty('repositoryFactory');
            $repositoryFactoryProperty->setAccessible(true);
            $repositoryFactory = $repositoryFactoryProperty->getValue($em);

            $reflectedRepositoryFactory = new \ReflectionClass($repositoryFactory);

            if ($reflectedRepositoryFactory->hasProperty('repositoryList')) {
                $repositoryListProperty = $reflectedRepositoryFactory->getProperty('repositoryList');
                $repositoryListProperty->setAccessible(true);

                $repositoryListProperty->setValue(
                    $repositoryFactory,
                    [$classname => $mock]
                );

                $repositoryFactoryProperty->setValue($em, $repositoryFactory);
            } else {
                $this->debugSection(
                    'Warning',
                    'Repository can\'t be mocked, the EventManager\'s repositoryFactory doesn\'t have "repositoryList" property'
                );
            }
        } else {
            $this->debugSection(
                'Warning',
                'Repository can\'t be mocked, the EventManager class doesn\'t have "repositoryFactory" or "repositories" property'
            );
        }
    }
public function haveFakeRepository($classname, $methods = [])
    {
        $em = $this->em;

        $metadata = $em->getMetadataFactory()->getMetadataFor($classname);
        $customRepositoryClassName = $metadata->customRepositoryClassName;

        if (!$customRepositoryClassName) {
            $customRepositoryClassName = '\Doctrine\ORM\EntityRepository';
        }

        $mock = Stub::make(
            $customRepositoryClassName,
            array_merge(
                [
                    '_entityName' => $metadata->name,
                    '_em' => $em,
                    '_class' => $metadata
                ],
                $methods
            )
        );

        $em->clear();
        $reflectedEm = new \ReflectionClass($em);


        if ($reflectedEm->hasProperty('repositories')) {
            //Support doctrine versions before 2.4.0

            $property = $reflectedEm->getProperty('repositories');
            $property->setAccessible(true);
            $property->setValue($em, array_merge($property->getValue($em), [$classname => $mock]));
        } elseif ($reflectedEm->hasProperty('repositoryFactory')) {
            //For doctrine 2.4.0+ versions

            $repositoryFactoryProperty = $reflectedEm->getProperty('repositoryFactory');
            $repositoryFactoryProperty->setAccessible(true);
            $repositoryFactory = $repositoryFactoryProperty->getValue($em);

            $reflectedRepositoryFactory = new \ReflectionClass($repositoryFactory);

            if ($reflectedRepositoryFactory->hasProperty('repositoryList')) {
                $repositoryListProperty = $reflectedRepositoryFactory->getProperty('repositoryList');
                $repositoryListProperty->setAccessible(true);

                $repositoryListProperty->setValue(
                    $repositoryFactory,
                    [$classname => $mock]
                );

                $repositoryFactoryProperty->setValue($em, $repositoryFactory);
            } else {
                $this->debugSection(
                    'Warning',
                    'Repository can\'t be mocked, the EventManager\'s repositoryFactory doesn\'t have "repositoryList" property'
                );
            }
        } else {
            $this->debugSection(
                'Warning',
                'Repository can\'t be mocked, the EventManager class doesn\'t have "repositoryFactory" or "repositories" property'
            );
        }
    }
public function haveFakeRepository ( $ classname , $ methods = [ ] ) { $ em = $ this -> em ; $ metadata = $ em -> getMetadataFactory ( ) -> getMetadataFor ( $ classname ) ; $ customRepositoryClassName = $ metadata -> customRepositoryClassName ; if ( ! $ customRepositoryClassName ) { $ customRepositoryClassName = '\Doctrine\ORM\EntityRepository' ; } $ mock = Stub :: make ( $ customRepositoryClassName , array_merge ( [ '_entityName' => $ metadata -> name , '_em' => $ em , '_class' => $ metadata ] , $ methods ) ) ; $ em -> clear ( ) ; $ reflectedEm = new \ ReflectionClass ( $ em ) ; if ( $ reflectedEm -> hasProperty ( 'repositories' ) ) { //Support doctrine versions before 2.4.0 $ property = $ reflectedEm -> getProperty ( 'repositories' ) ; $ property -> setAccessible ( true ) ; $ property -> setValue ( $ em , array_merge ( $ property -> getValue ( $ em ) , [ $ classname => $ mock ] ) ) ; } elseif ( $ reflectedEm -> hasProperty ( 'repositoryFactory' ) ) { //For doctrine 2.4.0+ versions $ repositoryFactoryProperty = $ reflectedEm -> getProperty ( 'repositoryFactory' ) ; $ repositoryFactoryProperty -> setAccessible ( true ) ; $ repositoryFactory = $ repositoryFactoryProperty -> getValue ( $ em ) ; $ reflectedRepositoryFactory = new \ ReflectionClass ( $ repositoryFactory ) ; if ( $ reflectedRepositoryFactory -> hasProperty ( 'repositoryList' ) ) { $ repositoryListProperty = $ reflectedRepositoryFactory -> getProperty ( 'repositoryList' ) ; $ repositoryListProperty -> setAccessible ( true ) ; $ repositoryListProperty -> setValue ( $ repositoryFactory , [ $ classname => $ mock ] ) ; $ repositoryFactoryProperty -> setValue ( $ em , $ repositoryFactory ) ; } else { $ this -> debugSection ( 'Warning' , 'Repository can\'t be mocked, the EventManager\'s repositoryFactory doesn\'t have "repositoryList" property' ) ; } } else { $ this -> debugSection ( 'Warning' , 'Repository can\'t be mocked, the EventManager class doesn\'t have "repositoryFactory" or "repositories" property' ) ; } }
Mocks the repository.

With this action you can redefine any method of any repository.
Please, note: this fake repositories will be accessible through entity manager till the end of test.

Example:

``` php
<?php

$I->haveFakeRepository('Entity\User', array('findByUsername' => function($username) {  return null; }));

```

This creates a stub class for Entity\User repository with redefined method findByUsername,
which will always return the NULL value.

@param $classname
@param array $methods
Mocks the repository .