Proporciona características de ayuda para PHPUnit.
dd()
y d()
TestDouble
$this->getDouble()
$this->verifyInvoked()
$this->verifyInvokedOnce()
$this->verifyInvokedMultipleTimes()
$this->verifyNeverInvoked()
ReflectionHelper
$this->getPrivateProperty()
$this->setPrivateProperty()
$this->getPrivateMethodInvoker()
DebugHelper
Correr:
$ composer require --dev kenjis/phpunit-helper
TestDouble
Este rasgo proporciona métodos auxiliares para crear objetos simulados y verificar invocaciones.
Importe el rasgo KenjisPhpUnitHelperTestDouble
a su clase de prueba:
<?php
declare (strict_types= 1 );
namespace Foo Bar Test Unit ;
use Kenjis PhpUnitHelper TestDouble ;
use PHPUnit Framework ;
final class BazTest extends Framework TestCase
{
use TestDouble;
}
$this->getDouble()
parámetro | tipo | descripción |
---|---|---|
$classname | cadena | nombre de clase |
$params | formación | [nombre_método => valor_retorno] |
[[nombre_método => [valor_retorno1, valor_retorno2 [, ...]]] | ||
$constructor_params | falso/matriz | falso: deshabilitar constructor/matriz: parámetros del constructor |
returns
(objeto) objeto simulado PHPUnit.
Obtiene el objeto simulado PHPUnit.
$ email = $ this -> getMockBuilder (CI_Email::class)
-> disableOriginalConstructor ()
-> onlyMethods ([ ' send ' ])
-> getMock ();
$ email -> method ( ' send ' )
-> willReturn ( true );
Podrías escribir el código arriba como a continuación:
$ email = $ this -> getDouble (CI_Email::class, [ ' send ' => true ]);
Puede establecer Cierre como el valor de retorno de un método simulado.
$ ret = function () {
throw new RuntimeException ( ' Cannot send email! ' );
};
$ mock = $ this -> getDouble (CI_Email::class, [ ' send ' => $ ret ]);
También puede configurar el simulacro en sí como el valor de retorno de un método simulado usando $this->returnSelf()
.
$ mock = $ this -> getDouble (CI_Email::class, [
' to ' => $ this -> returnSelf (),
' subject ' => $ this -> returnSelf (),
' send ' => true ,
]);
Puedes crear simulacros con llamadas consecutivas.
$ mock = $ this -> getMockBuilder (CI_Email::class)
-> disableOriginalConstructor ()
-> onlyMethods ([ ' method ' ])
-> getMock ();
$ mock -> expects ( $ this -> any ())-> method ( ' method ' )
-> will ( $ this -> onConsecutiveCalls ( ' GET ' , ' POST ' , ' DELETE ' ));
Podrías escribir el código arriba como a continuación:
$ mock = $ this -> getDouble (
CI_Input::class,
[
[ ' method ' => [ ' GET ' , ' POST ' , ' DELETE ' ]],
]
);
$this->verifyInvoked()
parámetro | tipo | descripción |
---|---|---|
$mock | objeto | Objeto simulado PHPUnit |
$method | cadena | nombre del método |
$params | formación | argumentos |
Comprueba que un método fue invocado al menos una vez.
$ loader -> expects ( $ this -> atLeastOnce ())
-> method ( ' view ' )
-> with (
' shopConfirm ' , $ this -> anything (), true
);
Podrías escribir el código arriba como a continuación:
$ this -> verifyInvoked (
$ loader ,
' view ' ,
[
' shopConfirm ' , $ this -> anything (), true
]
);
$this->verifyInvokedOnce()
parámetro | tipo | descripción |
---|---|---|
$mock | objeto | Objeto simulado PHPUnit |
$method | cadena | nombre del método |
$params | formación | argumentos |
Verifica que el método se invocó solo una vez.
$ loader -> expects ( $ this -> once ())
-> method ( ' view ' )
-> with (
' shopConfirm ' , $ this -> anything (), true
);
Podrías escribir el código arriba como a continuación:
$ this -> verifyInvokedOnce (
$ loader ,
' view ' ,
[
' shopConfirm ' , $ this -> anything (), true
]
);
$this->verifyInvokedMultipleTimes()
parámetro | tipo | descripción |
---|---|---|
$mock | objeto | Objeto simulado PHPUnit |
$method | cadena | nombre del método |
$times | entero | veces |
$params | formación | argumentos |
Verifica que el método fue llamado exactamente $veces veces.
$ loader -> expects ( $ this -> exactly ( 2 ))
-> method ( ' view ' )
-> withConsecutive (
[ ' shopConfirm ' , $ this -> anything (), true ],
[ ' shopTmplCheckout ' , $ this -> anything ()]
);
Podrías escribir el código arriba como a continuación:
$ this -> verifyInvokedMultipleTimes (
$ loader ,
' view ' ,
2 ,
[
[ ' shopConfirm ' , $ this -> anything (), true ],
[ ' shopTmplCheckout ' , $ this -> anything ()]
]
);
$this->verifyNeverInvoked()
parámetro | tipo | descripción |
---|---|---|
$mock | objeto | Objeto simulado PHPUnit |
$method | cadena | nombre del método |
$params | formación | argumentos |
Verifica que no se llamó al método.
$ loader -> expects ( $ this -> never ())
-> method ( ' view ' )
-> with (
' shopConfirm ' , $ this -> anything (), true
);
Podrías escribir el código arriba como a continuación:
$ this -> verifyNeverInvoked (
$ loader ,
' view ' ,
[
' shopConfirm ' , $ this -> anything (), true
]
);
ReflectionHelper
Este rasgo proporciona métodos auxiliares para acceder a propiedades y métodos privados o protegidos.
Pero, en general, no se recomienda probar propiedades o métodos que no sean públicos, así que piénselo dos veces antes de utilizar los métodos de este rasgo.
Importe el rasgo KenjisPhpUnitHelperReflectionHelper
a su clase de prueba:
<?php
declare (strict_types= 1 );
namespace Foo Bar Test Unit ;
use Kenjis PhpUnitHelper ReflectionHelper ;
use PHPUnit Framework ;
final class BazTest extends Framework TestCase
{
use ReflectionHelper;
}
$this->getPrivateProperty()
parámetro | tipo | descripción |
---|---|---|
$obj | objeto/cadena | nombre de objeto/clase |
$property | cadena | nombre de propiedad |
returns
el valor de la propiedad (mixto).
Obtiene valor de propiedad privada o protegida.
$ obj = new SomeClass ();
$ private_propery = $ this -> getPrivateProperty (
$ obj ,
' privatePropery '
);
$this->setPrivateProperty()
parámetro | tipo | descripción |
---|---|---|
$obj | objeto/cadena | nombre de objeto/clase |
$property | cadena | nombre de propiedad |
$value | mezclado | valor |
Establece el valor de la propiedad privada o protegida.
$ obj = new SomeClass ();
$ this -> setPrivateProperty (
$ obj ,
' privatePropery ' ,
' new value '
);
$this->getPrivateMethodInvoker()
parámetro | tipo | descripción |
---|---|---|
$obj | objeto/cadena | nombre de objeto/clase |
$method | cadena | nombre del método |
returns
el invocador del método (cierre).
Obtiene un invocador de método privado o protegido.
$ obj = new SomeClass ();
$ method = $ this -> getPrivateMethodInvoker (
$ obj , ' privateMethod '
);
$ this -> assertEquals (
' return value of the privateMethod() method ' , $ method ()
);
DebugHelper
Este rasgo proporciona funciones auxiliares, dd()
y d()
para volcar variables.
Importe el rasgo KenjisPhpUnitHelperDebugHelper
a su clase de prueba:
<?php
declare (strict_types= 1 );
namespace Foo Bar Test Unit ;
use Kenjis PhpUnitHelper DebugHelper ;
use PHPUnit Framework ;
final class BazTest extends Framework TestCase
{
use DebugHelper;
}
Este paquete tiene la licencia MIT.
Por favor, eche un vistazo a LICENSE
.