Recommend this page to a friend! |
![]() |
Info | Example | ![]() |
![]() |
![]() |
Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2024-07-20 (6 months ago) ![]() | Not yet rated by the users | Total: 16 | All time: 11,423 This week: 59![]() |
Version | License | PHP version | Categories | |||
testes-unitarios 1.0.0 | MIT/X Consortium ... | 8 | Language, Testing, PHP 8 |
Description | Author | |
This package can show how to test classes using PHPUnit. |
|
<?php |
Este projeto demonstra a criação e execução de testes unitários e de integração em PHP utilizando o PHPUnit. Ele inclui exemplos de como registrar e autenticar usuários, com testes unitários e de integração correspondentes para verificar a funcionalidade.
Teste unitário é uma técnica de teste de software onde pequenas partes do código (unidades) são testadas isoladamente para garantir que funcionem corretamente. Os testes unitários são fundamentais para identificar bugs no início do desenvolvimento e garantir que cada parte do código funcione como esperado.
Teste de integração verifica a interação entre diferentes módulos ou serviços do sistema, garantindo que eles funcionem juntos corretamente.
Siga os passos abaixo para configurar o projeto em sua máquina local.
Clone o repositório:
git clone https://github.com/seu-usuario/seu-repositorio.git
cd seu-repositorio
Instale as dependências do projeto:
composer install
Estrutura de Diretórios:
??? src/
? ??? User.php
? ??? UserService.php
??? tests/
? ??? UserTest.php
? ??? UserServiceTest.php
??? vendor/
??? composer.json
??? phpunit.xml
Para executar os testes unitários, use o comando:
./vendor/bin/phpunit tests/UserTest.php
estes de Integração Para executar os testes de integração, use o comando:
./vendor/bin/phpunit tests/UserServiceTest.php
namespace App\Testes;
class User {
private $users = [];
public function register($username, $password) {
if (isset($this->users[$username])) {
throw new \Exception("O nome de usuário já existe");
}
$this->users[$username] = password_hash($password, PASSWORD_DEFAULT);
return true;
}
public function login($username, $password) {
if (!isset($this->users[$username])) {
return false;
}
return password_verify($password, $this->users[$username]);
}
}
Testes Unitários para a Classe User Aqui estão alguns exemplos de testes unitários para a classe User:
namespace App\Testes;
use PHPUnit\Framework\TestCase;
class UserTest extends TestCase {
private $user;
protected function setUp(): void {
$this->user = new User();
}
public function testRegistrarUsuario() {
$result = $this->user->register("testuser", "password123");
$this->assertTrue($result, "Registro de usuário falhou");
fwrite(STDOUT, "Função testada: register - Registro de usuário com sucesso.\n");
}
public function testRegistrarUsuarioExistente() {
$this->user->register("testuser", "password123");
$this->expectException(\Exception::class);
$this->expectExceptionMessage("O nome de usuário já existe");
try {
$this->user->register("testuser", "password456");
} catch (\Exception $e) {
$this->assertEquals("O nome de usuário já existe", $e->getMessage());
fwrite(STDOUT, "Função testada: register - Exceção lançada para usuário existente.\n");
throw $e;
}
}
public function testLoginCorreto() {
$this->user->register("testuser", "password123");
$result = $this->user->login("testuser", "password123");
$this->assertTrue($result, "Login falhou com credenciais corretas");
fwrite(STDOUT, "Função testada: login - Login bem-sucedido com credenciais corretas.\n");
}
public function testLoginIncorreto() {
$this->user->register("testuser", "password123");
$result = $this->user->login("testuser", "wrongpassword");
$this->assertFalse($result, "Login bem-sucedido com credenciais incorretas");
fwrite(STDOUT, "Função testada: login - Falha no login com credenciais incorretas.\n");
}
public function testLoginUsuarioInexistente() {
$result = $this->user->login("nonexistentuser", "password123");
$this->assertFalse($result, "Login bem-sucedido com usuário inexistente");
fwrite(STDOUT, "Função testada: login - Falha no login com usuário inexistente.\n");
}
}
Sinta-se à vontade para contribuir com este projeto enviando pull requests. Para grandes mudanças, por favor, abra uma issue primeiro para discutir o que você gostaria de mudar.
Licença Este projeto está licenciado sob a licença MIT - veja o arquivo LICENSE para mais detalhes.
![]() |
File | Role | Description | ||
---|---|---|---|---|
![]() |
||||
![]() |
||||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Doc. | Documentation | ||
![]() ![]() |
Doc. | Documentation |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
![]() |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.