Skip to content

Commit e7680ae

Browse files
committed
Merge pull request #13 from rubenCodeforges/tests
Tests
2 parents 0144be7 + 32efde5 commit e7680ae

File tree

7 files changed

+243
-11
lines changed

7 files changed

+243
-11
lines changed

.travis.yml

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
language: php
2+
23
php:
3-
- 5.3
4-
- 5.4
5-
- 5.5
4+
- 5.3
5+
- 5.4
6+
- 5.5
7+
68
before_script:
7-
- composer self-update
8-
- composer install --dev --prefer-source
9+
- composer self-update
10+
- composer install --dev --prefer-source
11+
912
script: ./vendor/bin/phpunit -c lib/ --coverage-text
13+
1014
notifications:
11-
email:
12-
15+
email:
16+
17+

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#Smtp Validator Emails
2+
[![Build Status](https://travis-ci.org/rubenCodeforges/smtp-validator-email.svg)](https://travis-ci.org/rubenCodeforges/smtp-validator-email)
23

34
* Smtp Validator mail can validate your email to send smtp mail and check your mx.
45

lib/SmtpValidatorEmail/Email/Email.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,18 @@ class Email
2222
protected $email;
2323

2424
/**
25-
* @param $email
25+
* @param $email String
2626
*/
2727
public function __construct($email)
2828
{
29+
if(!is_string($email)){
30+
throw new \InvalidArgumentException('constructor expected string, got: '.gettype($email));
31+
}
32+
33+
if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
34+
throw new \InvalidArgumentException('String should be an email, got: '.$email);
35+
}
36+
2937
$this->setEmail($email);
3038
}
3139

lib/SmtpValidatorEmail/Email/EmailBag.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ public function add(array $emails)
122122
*/
123123
public function get($key, $default = null, $first = true)
124124
{
125+
if(!is_string($key) && !is_int($key)){
126+
throw new \InvalidArgumentException('$key expected to be string or integer, got: '.gettype($key));
127+
}
125128

126129
if (!array_key_exists($key, $this->emails)) {
127130
if (null === $default) {
@@ -141,13 +144,21 @@ public function get($key, $default = null, $first = true)
141144
/**
142145
* Sets a email values.
143146
*
144-
* @param string $key The key
147+
* @param string|int $key The key
145148
* @param string|array $values The value or an array of values
146149
* @param Boolean $replace Whether to replace the actual value or not (true by default)
147150
*
148151
*/
149152
public function set($key, $values, $replace = true)
150153
{
154+
if(!is_string($key) && !is_int($key)){
155+
throw new \InvalidArgumentException('$key expected to be string, got:'.gettype($key));
156+
}
157+
158+
if( !is_string($values) && !is_array($values) ){
159+
throw new \InvalidArgumentException('$key expected to be string or array, got: '.gettype($key));
160+
}
161+
151162
$values = array_values((array)$values);
152163

153164
if (true === $replace || !isset($this->emails[$key])) {
@@ -208,7 +219,7 @@ public function count()
208219
/**
209220
* Returns the number of emails.
210221
*
211-
* @return int The number of emails
222+
* @return object The ArrayIterator object of emails
212223
*/
213224
public function getIterator()
214225
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
3+
4+
namespace SmtpValidatorEmail\Tests\Email;
5+
6+
use SmtpValidatorEmail\Email\EmailBag;
7+
8+
class EmailBagTest extends \PHPUnit_Framework_TestCase
9+
{
10+
11+
public function testConstructorNoArrayGivenDropsError()
12+
{
13+
$this->setExpectedException(get_class(new \PHPUnit_Framework_Error("", 0, "", 1)));
14+
new EmailBag("notAnArrays");
15+
}
16+
17+
public function testToStringReturnsString()
18+
{
19+
$emails = array (
20+
21+
22+
23+
24+
25+
);
26+
27+
$bag = new EmailBag($emails);
28+
$this->assertTrue(is_string($bag->__toString()));
29+
}
30+
31+
public function testReplaceNoArrayGivenDropsError()
32+
{
33+
$this->setExpectedException(get_class(new \PHPUnit_Framework_Error("", 0, "", 1)));
34+
$bag = new EmailBag();
35+
$bag->replace("sdasd");
36+
}
37+
38+
public function testGetReturnsProperValue()
39+
{
40+
$emails = array (
41+
42+
43+
44+
45+
46+
);
47+
48+
$bag = new EmailBag($emails);
49+
$this->assertEquals('[email protected]', $bag->get(3));
50+
}
51+
52+
public function testGetInvalidArgumentKey()
53+
{
54+
$this->setExpectedException(
55+
'\InvalidArgumentException'
56+
);
57+
$bag = new EmailBag();
58+
$bag->get(new \stdClass());
59+
}
60+
61+
public function testGetWorksWithAssocArray()
62+
{
63+
$emails = array (
64+
'codeforges' => '[email protected]',
65+
'traceweb' => '[email protected]'
66+
);
67+
68+
$bag = new EmailBag($emails);
69+
$bag->get('codeforges');
70+
}
71+
72+
public function testSetInvalidArgumentKey()
73+
{
74+
$this->setExpectedException(
75+
'\InvalidArgumentException'
76+
);
77+
78+
$bag = new EmailBag();
79+
$bag->set(new \stdClass(), '[email protected]');
80+
}
81+
82+
public function testSetAcceptsAssocArray()
83+
{
84+
$emails = array (
85+
'codeforges' => '[email protected]',
86+
'traceweb' => '[email protected]'
87+
);
88+
89+
new EmailBag($emails);
90+
}
91+
92+
public function testHasReturnProperValue()
93+
{
94+
$emails = array (
95+
96+
97+
98+
99+
100+
);
101+
$bag = new EmailBag($emails);
102+
$this->assertTrue($bag->has(1));
103+
}
104+
105+
public function testHasWorksWithAssocArray()
106+
{
107+
$emails = array (
108+
'codeforges' => '[email protected]',
109+
'traceweb' => '[email protected]'
110+
);
111+
112+
$bag = new EmailBag($emails);
113+
$this->assertTrue($bag->has('codeforges'));
114+
}
115+
116+
public function testContainsReturnsPropperValue()
117+
{
118+
$emails = array (
119+
'codeforges' => '[email protected]',
120+
'traceweb' => '[email protected]'
121+
);
122+
123+
$bag = new EmailBag($emails);
124+
$this->assertTrue($bag->contains('codeforges', '[email protected]'));
125+
}
126+
127+
public function testRemove()
128+
{
129+
$emails = array (
130+
'codeforges' => '[email protected]',
131+
'traceweb' => '[email protected]'
132+
);
133+
134+
$bag = new EmailBag($emails);
135+
$beforeRemoveEmails = $bag->all();
136+
$bag->remove('codeforges');
137+
$this->assertNotEquals($beforeRemoveEmails, $bag->all());
138+
}
139+
140+
public function testReplace()
141+
{
142+
$emails = array (
143+
'codeforges' => '[email protected]',
144+
'traceweb' => '[email protected]'
145+
);
146+
147+
$replaceEmails = array (
148+
'traceweb' => '[email protected]',
149+
'codeforges' => '[email protected]'
150+
);
151+
152+
$bag = new EmailBag($emails);
153+
$oldEmails = $bag->all();
154+
$bag->replace($replaceEmails);
155+
$replaceEmails = $bag->all();
156+
157+
$this->assertNotTrue($oldEmails === $replaceEmails);
158+
}
159+
160+
public function testGetIteratorIsObject(){
161+
$emails = array (
162+
'codeforges' => '[email protected]',
163+
'traceweb' => '[email protected]'
164+
);
165+
$bag = new EmailBag($emails);
166+
167+
$this->assertTrue( is_object($bag->getIterator()) );
168+
}
169+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
4+
namespace SmtpValidatorEmail\Tests\Email;
5+
6+
use SmtpValidatorEmail\Email\Email;
7+
8+
class EmailTest extends \PHPUnit_Framework_TestCase {
9+
10+
11+
public function testConstructorExpectsString()
12+
{
13+
$this->setExpectedException(
14+
'\InvalidArgumentException',
15+
'/constructor expected string, got:/'
16+
);
17+
new Email(true);
18+
}
19+
20+
public function testConstructorStringIsEmailException(){
21+
$this->setExpectedException(
22+
'\InvalidArgumentException',
23+
'/String should be an email, got:/'
24+
);
25+
new Email('notAnEmail');
26+
}
27+
28+
public function testParseReturnNotEmptyUser(){
29+
$model = new Email('[email protected]');
30+
$results = $model->parse();
31+
$this->assertNotEmpty($results[0]);
32+
}
33+
34+
public function testParseReturnNotEmptyDomain(){
35+
$model = new Email('[email protected]');
36+
$results = $model->parse();
37+
$this->assertNotEmpty($results[1]);
38+
}
39+
}

lib/phpunit.xml.dist

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
<directory>./</directory>
1212
<exclude>
1313
<directory>../vendor/</directory>
14-
<directory>./SmtpValidatorEmail/Tests</directory>
1514
</exclude>
1615
</whitelist>
1716
</filter>

0 commit comments

Comments
 (0)