Skip to content

Commit

Permalink
reformat code, add whitespace, and rearrange methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Divyeshhhh committed May 25, 2024
1 parent 665437f commit 5261613
Showing 1 changed file with 91 additions and 79 deletions.
170 changes: 91 additions & 79 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,100 +17,113 @@ public function setUp(): void
{
$address = new Location("Royal Road", "Curepipe", 1);
$this->dummy_client = new Client(
"[email protected]", "john", "johhny", "abcd",
"13213431", $address);
"[email protected]",
"john",
"johhny",
"abcd",
"13213431",
$address
);

$success = $this->dummy_client->save();
if (!$success) {
throw new Exception('Unable to save client');
}
}

public function tearDown(): void
{
$this->dummy_client = null;
public function tearDown(): void
{
$this->dummy_client = null;

// Clear all data from client and user tables
self::query('DELETE FROM client; DELETE FROM user;');
}
}

public function testConstructor(): void
{
// check if fields were correctly set
self::assertEquals("[email protected]", $this->dummy_client->getEmail());
self::assertEquals("john", $this->dummy_client->getFirstName());
self::assertEquals("johhny", $this->dummy_client->getLastName());
self::assertEquals("13213431", $this->dummy_client->getPhoneNo());
self::assertEquals("Royal Road, Curepipe, Moka", $this->dummy_client->getAddress()->getFormattedAddress());
}
public function testConstructor(): void
{
// check if fields were correctly set
self::assertEquals("[email protected]", $this->dummy_client->getEmail());
self::assertEquals("john", $this->dummy_client->getFirstName());
self::assertEquals("johhny", $this->dummy_client->getLastName());
self::assertEquals("13213431", $this->dummy_client->getPhoneNo());
self::assertEquals("Royal Road, Curepipe, Moka", $this->dummy_client->getAddress()->getFormattedAddress());
}

public function testToArray(): void
{
$result = $this->dummy_client->toArray();

// check if all required keys are present
$this->assertArrayHasKey('user_id', $result);
$this->assertArrayHasKey('email', $result);
$this->assertArrayHasKey('first_name', $result);
$this->assertArrayHasKey('last_name', $result);
$this->assertArrayHasKey('phone_no', $result);
$this->assertArrayHasKey('district_id', $result);
$this->assertArrayHasKey('street', $result);
$this->assertArrayHasKey('city', $result);
$this->assertArrayHasKey('password', $result);

// check if actual values are correct
self::assertEquals("[email protected]", $result['email']);
self::assertEquals("john", $result['first_name']);
self::assertEquals("johhny", $result['last_name']);
self::assertEquals("13213431", $result['phone_no']);
self::assertEquals("Royal Road", $result['street']);
self::assertEquals("Curepipe", $result['city']);
self::assertEquals(1, $result['district_id']);
}
public function testToArray(): void
{
$result = $this->dummy_client->toArray();

// check if all required keys are present
$this->assertArrayHasKey('user_id', $result);
$this->assertArrayHasKey('email', $result);
$this->assertArrayHasKey('first_name', $result);
$this->assertArrayHasKey('last_name', $result);
$this->assertArrayHasKey('phone_no', $result);
$this->assertArrayHasKey('district_id', $result);
$this->assertArrayHasKey('street', $result);
$this->assertArrayHasKey('city', $result);
$this->assertArrayHasKey('password', $result);

// check if actual values are correct
self::assertEquals("[email protected]", $result['email']);
self::assertEquals("john", $result['first_name']);
self::assertEquals("johhny", $result['last_name']);
self::assertEquals("13213431", $result['phone_no']);
self::assertEquals("Royal Road", $result['street']);
self::assertEquals("Curepipe", $result['city']);
self::assertEquals(1, $result['district_id']);
}

public function testValidate(): void
{
$client = new Client(
"", "", "", "abcd",
"", new Location(), // pass an empty Location object for testing
);

// Test if existence checks work
self::assertEquals([
'email' => 'Invalid email format',
'first_name' => 'First name must be at least 3 characters long',
'last_name' => 'Last name must be at least 3 characters long',
'phone_no' => 'Phone number must be at least 7 characters long',
'district' => 'District does not exist'
], $client->validate());

// Test for range checks
$client = new Client(
"[email protected]", "Jo", "Doe", "1234567",
"123456", new Location(), // pass an empty Location object for testing
);

self::assertEquals([
'first_name' => 'First name must be at least 3 characters long',
'phone_no' => 'Phone number must be at least 7 characters long',
'district' => 'District does not exist'
], $client->validate());
}
public function testValidate(): void
{
$client = new Client(
"",
"",
"",
"abcd",
"",
new Location(), // pass an empty Location object for testing
);

// Test if existence checks work
self::assertEquals([
'email' => 'Invalid email format',
'first_name' => 'First name must be at least 3 characters long',
'last_name' => 'Last name must be at least 3 characters long',
'phone_no' => 'Phone number must be at least 7 characters long',
'district' => 'District does not exist'
], $client->validate());

// Test for range checks
$client = new Client(
"[email protected]",
"Jo",
"Doe",
"1234567",
"123456",
new Location(), // pass an empty Location object for testing
);

self::assertEquals([
'first_name' => 'First name must be at least 3 characters long',
'phone_no' => 'Phone number must be at least 7 characters long',
'district' => 'District does not exist'
], $client->validate());
}

public function testVerifyPassword(): void
{
// verify true password
self::assertTrue($this->dummy_client->verifyPassword("abcd"));
public function testVerifyPassword(): void
{
// verify true password
self::assertTrue($this->dummy_client->verifyPassword("abcd"));

// reject empty string
self::assertFalse($this->dummy_client->verifyPassword(""));
// reject empty string
self::assertFalse($this->dummy_client->verifyPassword(""));

// reject any other string
self::assertFalse($this->dummy_client->verifyPassword("abcde"));
self::assertFalse($this->dummy_client->verifyPassword("abcd "));
self::assertFalse($this->dummy_client->verifyPassword(" abcd"));
}
// reject any other string
self::assertFalse($this->dummy_client->verifyPassword("abcde"));
self::assertFalse($this->dummy_client->verifyPassword("abcd "));
self::assertFalse($this->dummy_client->verifyPassword(" abcd"));
}

/**
* @dataProvider getByIDProvider
Expand Down Expand Up @@ -216,5 +229,4 @@ public function testDeleteUser(): void
// Ensure the user does not exist anymore
self::assertNull($deletedClient);
}

}

0 comments on commit 5261613

Please sign in to comment.