PHPUnit | função assertClassHasStaticAttribute()
A função assertClassHasStaticAttribute() é uma função embutida no PHPUnit e é usada para declarar uma classe com um atributo estático. Esta asserção retornará true no caso se a classe contiver o atributo fornecido como elemento estático senão retornará false e em caso de true o caso de teste declarado foi aprovado senão o caso de teste falhou.
Sintaxe :
assertClassHasStaticAttribute(string $attributeName, string $className, string $message = ''])
Parâmetros : esta função aceita três parâmetros, conforme mostrado na sintaxe acima. Os parâmetros são descritos abaixo:
- $attributeName : Este parâmetro representa o attributeName para ser o atributo da classe no array.
- $className : este parâmetro é um nome de classe para a qual a função assert irá verificar se contém atributo ou não.
- $mensagem : Este parâmetro leva um valor de string. Quando o caso de teste falhou, essa mensagem de string foi exibida como uma mensagem de erro.
Os programas abaixo ilustram a função assertClassHasStaticAttribute():
Programa 1 :
<?php
use PHPUnit\Framework\TestCase;
// test class
Class testClass{
public static $attribute = "test attribute";
}
class GeeksPhpunitTestCase extends TestCase
{
public function testNegativeTestcaseForClassHasStaticAttribute()
{
// assert function to test whether 'geeks' is a attribute of testclass
$this->assertClassHasStaticAttribute('geeks', "testClass", "testClass doesn't has geeks as static attribute") ;
}
}
?>
Saída:
PHPUnit 6.5.5 by Sebastian Bergmann and contributors. F 1 / 1 (100%) Time: 682 ms, Memory: 4.00MB There was 1 failure: 1) GeeksPhpunitTestCase::testNegativeTestcaseForClassHasStaticAttribute testClass doesn't has geeks as static attribute Failed asserting that class "testClass" has static attribute "geeks". /home/shivam/Documents/geeks/phpunit/abc.php:14 FAILURES! Tests: 1, Assertions: 1, Failures: 1.
Programa 2 :
<?php
use PHPUnit\Framework\TestCase;
// test class
Class testClass{
public static $attribute = "test attribute";
}
class GeeksPhpunitTestCase extends TestCase
{
public function testPositiveTestcaseForClassHasStaticAttribute()
{
// assert function to test whether 'attribute' is a attribute of testclass
$this->assertClassHasStaticAttribute('attribute', "testClass", "testClass doesn't has geeks as static attribute") ;
}
}
?>
Saída:
PHPUnit 6.5.5 by Sebastian Bergmann and contributors. . 1 / 1 (100%) Time: 21 ms, Memory: 4.00MB OK (1 test, 1 assertion)
Nota: Para rodar casos de teste com phpunit, siga os passos a partir daqui .
As postagens do blog Acervo Lima te ajudaram? Nos ajude a manter o blog no ar!
Faça uma doação para manter o blog funcionando.
70% das doações são no valor de R$ 5,00...
Diógenes Lima da Silva