Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

228
Visualizações
Mocking trait methods with mockery

I have a trait:

trait A {
    function foo() {
        ...
    }
}

and a class that uses the trait like this:

class B {
    use A {
        foo as traitFoo;
    }

    function foo() {
        $intermediate = $this->traitFoo();
        ...
    }
}

I want to test the class' foo() method and want to mock (with Mockery) the behavior of the trait's foo() method. I tried using a partial and mocking traitFoo() like:

$mock = Mockery::mock(new B());
$mock->shouldReceive('traitFoo')->andReturn($intermediate);

But it doesn't work.

Is it possible to do this? Is there an alternative way? I want to test B::foo() isolating it from the trait's foo() implementation.

Thanks in advance.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The proxy mock you are using proxies calls from outside the mock, so internal calls within the class $this->... cannot be mocked.

If you have no final methods, you still can use normal partial mock or a passive partial mock, which extends the mocked class, and don't have such limitations:

$mock = Mockery::mock(B::class)->makePartial();
$mock->shouldReceive('traitFoo')->andReturn($intermediate);
$mock->foo();

UPDATE:

The full example with non-mocked trait functions:

use Mockery as m;

trait A
{
    function foo()
    {
        return 'a';
    }

    function bar()
    {
        return 'd';
    }
}

class B
{
    use A {
        foo as traitFoo;
        bar as traitBar;
    }

    function foo()
    {
        $intermediate = $this->traitFoo();
        return "b$intermediate" . $this->traitBar();
    }
}


class BTest extends \PHPUnit_Framework_TestCase
{

    public function testMe()
    {
        $mock = m::mock(B::class)->makePartial();
        $mock->shouldReceive('traitFoo')->andReturn('c');
        $this->assertEquals('bcd', $mock->foo());
    }
}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda