Skip to content

Commit 46176bc

Browse files
committed
Add PHP unit tests!
1 parent 5891c12 commit 46176bc

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Exclude unneeded files/folders for the distribution archive
2+
.gitignore export-ignore
3+
.gitattributes export-ignore
4+
/tests/ export-ignore
5+
phpunit.xml.dist export-ignore

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
/vendor
66
composer.lock
77

8+
# Tests
9+
phpunit.xml
10+
.phpunit.cache
11+
812
# IDE config folder
913
/.idea
1014

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,8 @@
2828
"gabordemooij/redbean": "^5.7",
2929
"vlucas/phpdotenv": "^5.5",
3030
"filp/whoops": "^2.15"
31+
},
32+
"require-dev": {
33+
"phpunit/phpunit": "^10.2"
3134
}
3235
}

phpunit.xml.dist

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/|version|/phpunit.xsd"
5+
bootstrap="vendor/autoload.php"
6+
colors="true"
7+
cacheDirectory=".phpunit.cache">
8+
<testsuites>
9+
<testsuite name="Unit Tests">
10+
<directory>tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
<source>
14+
<include>
15+
<directory suffix=".php">src</directory>
16+
</include>
17+
</source>
18+
</phpunit>

tests/Entity/UserTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PH7\ApiSimpleMenu\Tests\Entity;
6+
7+
use PH7\ApiSimpleMenu\Entity\User as UserEntity;
8+
use PHPUnit\Framework\TestCase;
9+
use Ramsey\Uuid\Uuid;
10+
11+
final class UserTest extends TestCase
12+
{
13+
private UserEntity $userEntity;
14+
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
$this->userEntity = new UserEntity();
20+
}
21+
22+
public function testSequentialId(): void
23+
{
24+
$sequentialId = 55;
25+
26+
$this->userEntity->setSequentialId($sequentialId);
27+
28+
$this->assertSame($sequentialId, $this->userEntity->getSequentialId());
29+
}
30+
31+
public function testUserUuid(): void
32+
{
33+
$uuid = Uuid::uuid4()->toString();
34+
35+
$this->userEntity->setUserUuid($uuid);
36+
37+
$this->assertSame($uuid, $this->userEntity->getUserUuid());
38+
}
39+
40+
public function testUnserialize(): void
41+
{
42+
$expectUserData = [
43+
'id' => 5,
44+
'user_uuid' => Uuid::uuid4()->toString(),
45+
'first_name' => 'Pierre',
46+
'last_name' => 'Soria',
47+
'email' => 'me@ph7.me',
48+
'phone' => '043983934',
49+
'password' => 'chicken',
50+
'created_date' => '2023-07-01 19:59:55'
51+
];
52+
53+
$this->userEntity->unserialize($expectUserData);
54+
55+
// assertions
56+
$this->assertSame($expectUserData['id'], $this->userEntity->getSequentialId());
57+
$this->assertSame($expectUserData['user_uuid'], $this->userEntity->getUserUuid());
58+
$this->assertSame($expectUserData['first_name'], $this->userEntity->getFirstName());
59+
$this->assertSame($expectUserData['last_name'], $this->userEntity->getLastName());
60+
$this->assertSame($expectUserData['email'], $this->userEntity->getEmail());
61+
$this->assertSame($expectUserData['phone'], $this->userEntity->getPhone());
62+
$this->assertSame($expectUserData['password'], $this->userEntity->getPassword());
63+
$this->assertSame($expectUserData['created_date'], $this->userEntity->getCreationDate());
64+
}
65+
}

0 commit comments

Comments
 (0)