r/PHPhelp • • Jan 16 '22

Need help with basic php unit testing

Hi all,

Let's start of with the fact that I have very minimal experience in backend (PHP) development but I do enjoy learing about back-end as a front-end developer :). So I am currently at the stage where I want to learn some testing with php unit tests.

The problem Im having with this is that I do not know what exactly I should test inside my project.

I currently have a login, which includes the register part of the user. inside my class User, I created my public function as such:

<?php

public function register($data) {

$this->db->query('INSERT INTO users (username, email, account, password) VALUES(:username, :email, :account, :password)');

//Bind values

$this->db->bind(':username', $data['username']);

$this->db->bind(':email', $data['email']);

$this->db->bind(':account', $data['account']);

$this->db->bind(':password', $data['password']);

//Execute function

if ($this->db->execute()) {

return true;

} else {

return false;

}

}

?>

What should I test with php unit test and how should I go about this? Like does someone have an example of how to best test the functionality? I just want to do basic testing but id like to do it the right way.

Hope I explained it well.

Thanks for reading all :)

9 Upvotes

11 comments sorted by

View all comments

3

u/[deleted] Jan 16 '22 edited Jan 16 '22
<?php
class User
{
    private PDO $db;
    
    public function __construct(PDO $db)
    {
        $this->db = $db;
    }
    
    public function register($data) {
        $this->db->query('
           INSERT INTO users 
               (username, email, account, password) 
           VALUES
               (:username, :email, :account, :password)
        ');

        $this->db->bind(':username', $data['username']);
        $this->db->bind(':email', $data['email']);
        $this->db->bind(':account', $data['account']);
        $this->db->bind(':password', $data['password']);

        return $this->db->execute();
    }
}

<?php
class UserTest extends TestCase
{
    public function testRegister()
    {
        $data = [
            'username' => 'username',
            'email' => 'email',
            'account' => 'account',
            'password' => 'password'
        ];

        $stmt = $this->createMock(PDOStatement::class);
        $stmt->expects($this->exactly(count($data)))
             ->method('bind')
             ->with($this->anything());
        $stmt->method('execute')
             ->willReturn(true);
        
        $subject = new User($pdo);

        $this->assertTrue($subject->register($data));
    }

1

u/Chemicalclash Jan 16 '22

Cool thanks a lot. So I can basically test if something is true like this line $this->assertTrue($subject->register($data));

3

u/[deleted] Jan 16 '22

Tests usually have 3 parts:

  1. The subject, which is the User class in this case.

  2. The expectations, we mock PDO and expect some methods be called some number of times and return specific things.

  3. The assertion, we assert our subject's method call returns true for the test to pass.