jeudi 28 septembre 2017

How to test an method that has like parameter the mock name class

I have one method like this

public function prePersist(LifecycleEventArgs $event)
{
    $em = $event->getEntityManager();
    $entity = $event->getObject();
    $metadata = $em->getClassMetadata(get_class($entity));

}

I've tested getEntityManager and getObject methods, but it is time to test getClassMetadata method and each parameters, in this case, it only one !

get_class($entity)

the above line returns name class (random) :

Mock_ObjectManager_126b0394
Mock_ObjectManager_cc9f593f
Mock_ObjectManager_8e119a34

it never returns real name class... and I want check the first parameter set when getClassMetadata is called.

    $test = $this;
    $this->em->expects($this->at(0))
             ->method('getClassMetadata')
             ->with(
                $this->callback(function($arg) use ($test) {
                 $test->assertThat($arg, 
                  $this->logicalAnd(
                   $this->equalTo('ObjectManager')
                  )
                 );//assertThat
                return true;
              }) // callback                                    
             )
             ->willReturn($this->objectManager);

How test it ?

Aucun commentaire:

Enregistrer un commentaire