added phpunit and composer stuff

This commit is contained in:
Andreas 2019-10-07 10:55:12 +02:00
parent 3ad68e4078
commit ef2e25ef19
9 changed files with 1633 additions and 0 deletions

1
.phpunit.result.cache Normal file
View File

@ -0,0 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":508:{a:2:{s:7:"defects";a:4:{s:47:"TestUser::testCanBeCreatedFromValidEmailAddress";i:4;s:48:"TestEmail::testCanBeCreatedFromValidEmailAddress";i:4;s:53:"TestEmail::testCannotBeCreatedFromInvalidEmailAddress";i:4;s:32:"TestEmail::testCanBeUsedAsString";i:4;}s:5:"times";a:4:{s:48:"TestEmail::testCanBeCreatedFromValidEmailAddress";d:0.005;s:53:"TestEmail::testCannotBeCreatedFromInvalidEmailAddress";d:0.001;s:32:"TestEmail::testCanBeUsedAsString";d:0;s:47:"TestUser::testCanBeCreatedFromValidEmailAddress";d:0;}}}

10
composer.json Normal file
View File

@ -0,0 +1,10 @@
{
"autoload": {
"classmap": [
"dev_mvc/"
]
},
"require-dev": {
"phpunit/phpunit": "^8"
}
}

1535
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1 @@
C:30:"PHPUnit\Runner\TestResultCache":44:{a:2:{s:7:"defects";a:0:{}s:5:"times";a:0:{}}}

View File

@ -0,0 +1,28 @@
<?php
namespace model\forum;
class Email{
private $email;
private $valid;
function __construct($email){
$sanitized_email = filter_var($email, FILTER_SANITIZE_EMAIL);
if(filter_var($sanitized_email, FILTER_VALIDATE_EMAIL)){
$this->email = $sanitized_email;
$this->valid = true;
}
else{
$this->email = 'invalid';
$this->valid = false;
}
}
public function getEmail(){
return $this->email;
}
public function getValid(){
return $this->valid;
}
public function __toString(): string
{
return $this->email;
}
}

9
phpunit.xml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit bootstrap="vendor\autoload.php">
<testsuites>
<testsuite name="unit">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>

3
tests/unit/Test.php Normal file
View File

@ -0,0 +1,3 @@
<?php
include_once 'TestEmail.php';
include_once 'TestUser.php';

30
tests/unit/TestEmail.php Normal file
View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use model\forum\Email;
final class TestEmail extends TestCase
{
public function testCanBeCreatedFromValidEmailAddress(): void
{
$this->assertInstanceOf(
Email::class,
new Email('user@example.com')
);
}
public function testCannotBeCreatedFromInvalidEmailAddress(): void
{
$email = new Email('user');
$this->assertFalse($email->getValid());
$this->assertEquals($email->getEmail(), 'invalid');
}
public function testCanBeUsedAsString(): void
{
$this->assertEquals(
'user@example.com',
new Email('user@example.com')
);
}
}

16
tests/unit/TestUser.php Normal file
View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use model\forum\User;
final class TestUser extends TestCase
{
public function testCanBeCreatedFromValidEmailAddress(): void
{
$this->assertInstanceOf(
User::class,
new User(1, 'andreas', 'andreas@example.com', 'password', '10-04-2019 12:00:00', '10-04-2019 12:00:00', '94.212.253.51', -1, 1)
);
}
}