En otros artículos, he ido explicando como escribir test unitarios con PHPUnit, y he pensado que os podría ser útil disponer de la lista de todos los assert disponibles (o casi todos, si queréis ver el resto, podéis ir a https://phpunit.readthedocs.io/es/latest/assertions.html)
Los mas frecuentes
- assertEmpty(mixed $actual[, string $message = »])
- Reporta un error identificado por el $message si $actual no está vacío.
- assertNotEmpty() es el inverso
- assertEquals(mixed $expected, mixed $actual[, string $message = »])
- Reporta un error identificado por el $message si las dos variables $expected y $actual no son iguales.
- assertNotEquals() es el inverso
- Si la comparación es numérica, puede tener un argumento mas, para indicar la precisión
- assertFalse(bool $condition[, string $message = »])
- Reporta un error identificado por el $message si $condition es true.
- assertNotFalse() es el inverso
- assertCount($expectedCount, $haystack[, string $message = »])
- Reporta un error identificado por el $message si el número de elementos en $haystack no es $expectedCount.
- assertNotCount() es el inverso
- assertGreaterThan(mixed $expected, mixed $actual[, string $message = »])
- Reporta un error identificado por el $message si el valor de $actual no es mayor que el valor de $expected.
- assertGreaterThanOrEqual(mixed $expected, mixed $actual[, string $message = »])
- Reporta un error identificado por el $message si el valor de $actual no es mayor o igual al valor de $expected.
- assertInstanceOf($expected, $actual[, $message = »])
- Reporta un error identificado por el $message si $actual no es una instancia de $expected.
- assertNotInstanceOf() es el inverso
- assertLessThan(mixed $expected, mixed $actual[, string $message = »])
- Reporta un error identificado por el $message si el valor de $actual no es menor que el valor de $expected.
- assertLessThanOrEqual(mixed $expected, mixed $actual[, string $message = »])
- Reporta un error identificado por el $message si el valor de $actual no es menor o igual que el valor de $expected.
- assertNull(mixed $variable[, string $message = »])
- Reporta un error identificado por el $message si $variable no es null.
- assertNotNull() es el inverso
- assertSame(mixed $expected, mixed $actual[, string $message = »])
- Reporta un error identificado por el $message si las dos variables $expected y $actual no tienen el mismo tipo y valor.
- assertNotSame() es el inverso
- assertStringEndsWith(string $suffix, string $string[, string $message = »])
- Reporta un error identificado por el $message si $string no termina en $suffix.
- assertStringEndsNotWith() es el inverso
- assertStringStartsWith(string $prefix, string $string[, string $message = »])
- Reporta un error identificado por el $message si el $string no comienza con el $prefix.
- assertStringStartsNotWith() es el inverso
- assertTrue(bool $condition[, string $message = »])
- Reporta un error identificado por el $message si $condition es false.
- assertNotTrue() es el inverso
Para el sistema de ficheros
- assertDirectoryExists(string $directory[, string $message = »])
- Reporta un error identificado por el $message si la carpeta especificada por $directory no existe.
- assertDirectoryNotExists() es el inverso
- assertDirectoryIsReadable(string $directory[, string $message = »])
- Reporta un error identificado por el $message si la carpeta especificada en $directory no es una carpeta o no es legible.
- assertDirectoryNotIsReadable() es el inverso
- assertDirectoryIsWritable(string $directory[, string $message = »])
- Reporta un error identificado por el $message si la carpeta especificada en $directory no es una carpeta o no se puede escribir en ella.
- assertDirectoryNotIsWritable() es el inverso
- assertFileEquals(string $expected, string $actual[, string $message = »])
- Reporta un error identificado por el $message si el archivo especificado en $expected no tiene el mismo contenido que el archivo especificado en $actual.
- assertFileNotEquals() es el inverso
- assertFileExists(string $filename[, string $message = »])
- Reporta un error identificado por el $message si the archivo especificado en $filename no existe
- assertFileNotExists() es el inverso
- assertFileIsReadable(string $filename[, string $message = »])
- Reporta un error identificado por el $message si el archivo especificado en $filename no es un archivo o no es legible.
- assertFileNotIsReadable()
- assertFileIsWritable(string $filename[, string $message = »])
- Reporta un error identificado por el $message si el archivo especificado en $filename no es un archivo o no se puede escribir en él.
- assertFileNotIsWritable() es el inverso
- assertIsReadable(string $filename[, string $message = »])
- Reporta un error identificado por el $message si el archivo o carpeta especificada en $filename no se puede leer.
- assertNotIsReadable() es el inverso
- assertIsWritable(string $filename[, string $message = »])
- Reporta un error identificado por el $message si el archivo o carpeta especificada en $filename no se puede escribir.
- assertNotIsWritable() es el inverso
- assertJsonFileEqualsJsonFile(mixed $expectedFile, mixed $actualFile[, string $message = »])
- Reporta un error identificado por el $message si el valor de $actualFile no coincide con el valor de $expectedFile.
- assertJsonStringEqualsJsonFile(mixed $expectedFile, mixed $actualJson[, string $message = »])
- Reporta un error identificado por el $message si el valor de $actualJson no coincide con el valor de $expectedFile.
- assertStringEqualsFile(string $expectedFile, string $actualString[, string $message = »])
- Reporta un error identificado por el $message si el archivo especificado en $expectedFile no tiene a $actualString como su contenido.
- assertStringNotEqualsFile() es el inverso
- assertXmlFileEqualsXmlFile(string $expectedFile, string $actualFile[, string $message = »])
- Reporta un error identificado por el $message si the XML document in $actualFile no es igual al documento XML en $expectedFile.
- assertXmlFileNotEqualsXmlFile() es el inverso
- assertXmlStringEqualsXmlFile(string $expectedFile, string $actualXml[, string $message = »])
- Reporta un error identificado por el $message si the XML document in $actualXml is not equal to the XML document in $expectedFile.
- assertXmlStringNotEqualsXmlFile() es el inverso
Otros asserts de PHPUnit
- assertArrayHasKey(mixed $key, array $array[, string $message = »])
- Reporta un error identificado por el
$message
si el$array
no tiene la llave$key
. - assertArrayNotHasKey() Reporta error si lo tiene
- Reporta un error identificado por el
- assertClassHasAttribute(string $attributeName, string $className[, string $message = »])
- Reporta un error identificado por el $message si $className::attributeName no existe.
- assertClassNotHasAttribute() es el inverso
- assertArraySubset(array $subset, array $array[, bool $strict = false, string $message = »])
- Reporta un error identificado por el $message si el $array no contiene el $subset
- assertClassHasStaticAttribute(string $attributeName, string $className[, string $message = »])
- Reporta un error identificado por el $message si $className::attributeName no existe.
- assertContains(mixed $needle, iterable $haystack[, string $message = »])
- Reporta un error identificado por el $message si $needle no es un elemento de $haystack.
- assertNotContains() es el inverso
- assertAttributeContains() y assertAttributeNotContains() son envoltorios convenientes que usan un atributo public, protected o private de una clase u objeto como el lugar donde buscar, haystack.
- assertContainsOnly(string $type, iterable $haystack[, boolean $isNativeType = null, string $message = »]
- Reporta un error identificado por el $message si $haystack no contiene solamente variables del tipo $type.
- assertAttributeContainsOnly() y assertAttributeNotContainsOnly() son envoltorios convenientes que usan un atributo public, protected o private de una clases u objeto como el lugar donde buscar, haystack.
- assertContainsOnlyInstancesOf(string $classname, Traversable|array $haystack[, string $message = »])
- Reporta un error identificado por el $message si $haystack no contiene solamente instancias de la clase $classname
- assertEqualXMLStructure(DOMElement $expectedElement, DOMElement $actualElement[, boolean $checkAttributes = false, string $message = »])
- Reporta un error identificado por el $message si la estructura del DOMElement en $actualElement no es igual a la estructura XML del DOMElement en $expectedElement.
- assertIsArray($actual[, $message = »])
- Reporta un error identificado por el $message si $actual no es del tipo array.
- assertIsNotArray() es el inverso
- assertIsBool($actual[, $message = »])
- Reporta un error identificado por el $message si $actual no es del tipo booleano.
- assertIsNotBool() es el inverso
- assertIsCallable($actual[, $message = »])
- Reporta un error identificado por el $message si $actual no es del tipo «callable».
- assertIsNotCallable() es el inverso
- assertIsFloat($actual[, $message = »])
- Reporta un error identificado por el $message si $actual no es del tipo float
- assertIsNotFloat() es el inverso.
- assertIsInt($actual[, $message = »])
- Reporta un error identificado por el $message si $actual no es del tipo int
- assertIsNotInt() es el inverso.
- assertIsIterable($actual[, $message = »])
- Reporta un error identificado por el $message si $actual no es del tipo iterable
- assertIsNotIterable() es el inverso.
- assertIsNumeric($actual[, $message = »])
- Reporta un error identificado por el $message si $actual no es numérico
- assertIsNotNumeric() es el inverso
- assertIsObject($actual[, $message = »])
- Reporta un error identificado por el $message si $actual no es del tipo object
- assertIsNotObject() es el inverso
- assertIsString($actual[, $message = »])
- Reporta un error identificado por el $message si $actual no es del tipo string
- assertIsNotString() es el inverso
- assertJsonStringEqualsJsonString(mixed $expectedJson, mixed $actualJson[, string $message = »])
- Reporta un error identificado por el $message si el valor de $actualJson no coincide con el valor de $expectedJson.
- assertNan(mixed $variable[, string $message = »])
- Reporta un error identificado por el $message si $variable no es NAN.
- assertObjectHasAttribute(string $attributeName, object $object[, string $message = »])
- Reporta un error identificado por el $message si $object->attributeName no existe.
- assertObjectNotHasAttribute() es el inverso
- assertRegExp(string $pattern, string $string[, string $message = »])
- Reporta un error identificado por el $message si $string no coincide con la expresión regular $pattern.
- assertNotRegExp() es el inverso
- assertStringMatchesFormat(string $format, string $string[, string $message = »])
- Reporta un error identificado por el $message si el $string no coincide con el formato dado en $format.
- assertStringNotMatchesFormat() es el inverso
- %e: Representa el separador de carpetas, por ejemplo / en GNU/Linux.
%s: Uno o más de cualquier carácter (o espacio en blanco) excepto el carácter de fin de línea.
%S: Cero o más de cualquier carácter (o espacio en blanco) excepto el carácter de fin de línea.
%a: Uno o más de cualquier carácter (o espacio en blanco) incluyendo el carácter de fin de línea.
%A: Cero o más de cualquier carácter (o espacio en blanco) incluyendo el carácter de fin de línea.
%w: Cero o más caracteres de espacio en blanco.
%i: Un valor entero con signo, por ejemplo +3142, -3142.
%d: Un valor entero sin signo, por ejemplo 123456.
%x: Uno o más caracteres hexadecimales. Esto es, caracteres en el rango de 0-9, a-f y A-F.
%f: Un número de punto flotante, por ejemplo: 3.142, -3.142, 3.142E-10 o 3.142e+10.
%c: Un solo carácter de cualquier tipo.
%%: Un carácter literal de porcentaje: %.
- assertXmlStringEqualsXmlString(string $expectedXml, string $actualXml[, string $message = »])
- Reporta un error identificado por el $message si the XML document in $actualXml is not equal to the XML document in $expectedXml.
- assertXmlStringNotEqualsXmlString() es el inverso