Skip to content

Commit

Permalink
Email Class for DataType and its tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorkmaz committed Jan 19, 2017
1 parent 38577cf commit 478b2d1
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/DataType/Email.php
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);

namespace Selami\Entity\DataType;

use Selami\Entity\Interfaces\DataTypeInterface;
use InvalidArgumentException;

class Email extends DataTypeAbstract implements DataTypeInterface
{
const DATA_FORMAT_ERROR = 'Assertion failed for value "%s" for "%s" : INVALID_MAIL_ADDRESS_FORMAT';

protected static $defaults = [
'default' => ''
];


/**
* Email constructor.
* @param string $key
* @param mixed $datum
* @param array $options
* @throws validArgumentException
*/
public function __construct(string $key, $datum, array $options = [])
{
$this->key = $key;
$this->datum = $datum;
$this->checkValidOptions($options);
$this->options = array_merge(self::$defaults, $options);
}
/**
* {@inheritdoc}
*/
public function assert()
{
if (filter_var($this->datum, FILTER_VALIDATE_EMAIL) === false) {
$this->errorMessageTemplate = self::DATA_FORMAT_ERROR;
$this->throwException();
}
return true;
}

/**
* {@inheritdoc}
*/
public function normalize()
{
try {
$this->assert();
return $this->datum;
} catch (InvalidArgumentException $e) {
return $this->options['default'];
}
}
}
49 changes: 49 additions & 0 deletions tests/DataType/EmailTest.php
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);

namespace tests;

use Selami\Test\Abstracts\DataType;

class MyEmailClass extends DataType
{

public function setUp()
{
$this->className = '\\Selami\\Entity\\DataType\\Email';
$this->validValue = 'mehmet@mkorkmaz.com';
$this->key = 'email';
$this->options = [];
}

public function trueProvider()
{
return [
['m@k.co'],
['mehmet.korkmaz@reformo.net'],
['mehmet+korkmaz@reformo.net']
];
}

public function exceptionProvider()
{
return [
['mehmet@korkmaz@reformo.net'], // no two @ char
['mehmet @mkorkmaz.com'], // no whitespace
['m@k.co,m@k.com'], // no comma char
['m@k.co;m@k.com'], // no colon char
['.m@k.co'], // no period at the first char
['m\k@k.co'], // backslash is invalid
['m+k.co'], // there must be at least one @ char
['email123456789012345678901234567890123456789012345678901234567890@k.co'] // max 64 char before @
];
}

public function defaultsProvider()
{
return [
['mehmet,@mkorkmaz.com', 'mehmet@mkorkmaz.com', ['default' => 'mehmet@mkorkmaz.com']],
['m@k.co;m@k.com', null, ['default' => null]]
];
}
}

0 comments on commit 478b2d1

Please sign in to comment.