Мошенничество security.context при вызове считается необъектным

Я пытаюсь проверить мой сервис. Эта услуга вызывает другую услугу, как security.context, Когда AlbumHandler это вызов, издеваться над security.context шаг.

Ошибка:

 PHP Fatal error:  Call to a member function getToken() on a non-object

Код:

     public function setUp()
{
$this->container = $this->getMock('\Symfony\Component\DependencyInjection\ContainerInterface');
}public function testAdd()
{

// The user I want to return
$user = $this->getMock('\MyProject\Bundle\UserBundle\Entity\User');

// I create a Token for mock getUser()
$token = $this->getMock('\Symfony\Component\Security\Core\Authentication\Token');
$token->expects($this->once())
->method('getUser')
->will($this->returnValue($user));

// I mock the service. PHPUnit don't return an error here.
$service = $this->getMockBuilder('Symfony\Component\Security\Core\SecurityContextInterface')
->disableOriginalConstructor()
->getMock();
$service->expects($this->once())
->method('getToken')
->will($this->returnValue($token));

// I replace the real service by the mock
$this->container->set('security.context', $service);

// It fails at the constructor of this service.
$albumHandler = new AlbumHandler($entityManager, $this->container, 'MyProject\Bundle\AlbumBundle\Entity\Album');

$this->assertEquals($albumHandler, $albumHandler);

}

Следующий код является конструктором AlbumHandler

  public function       __construct(ObjectManager $om, Container $container, $entityClass)
{
$this->om = $om;
$this->entityClass = $entityClass;
$this->repository = $this->om->getRepository($this->entityClass);
$this->container = $container;

// fail here
$this->user = $this->container->get('security.context')->getToken()->getUser();
}

1

Решение

Вы должны издеваться над контейнером get call тоже. Попробуйте заменить это:

    // I replace the real service by the mock
$this->container->set('security.context', $service);

с

    $this->container
->expects($this->once())
->method('get')
->with('security.context')
->will($this->returnValue($service));

Надеюсь это поможет

РЕДАКТИРОВАТЬ:

Вы ошибаетесь Token Объект. Замена:

    // I create a Token for mock getUser()
$token = $this->getMock('\Symfony\Component\Security\Core\Authentication\Token');

С:

    // I create a Token for mock getUser()
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
5

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector