Skip to content

Commit 8c6c50e

Browse files
committed
[php][user_email-01_base] Add NewsletterController
1 parent 30147f4 commit 8c6c50e

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\Controller;
6+
7+
use CodelyTv\Model\Newsletter;
8+
use InvalidArgumentException;
9+
10+
final class NewsletterController
11+
{
12+
public function post(string $emailAddress): Newsletter
13+
{
14+
if ('' === $emailAddress) {
15+
throw new InvalidArgumentException('The email address is empty');
16+
}
17+
18+
if (!filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
19+
throw new InvalidArgumentException("The email address <$emailAddress> is not valid");
20+
}
21+
22+
if (!(strpos($emailAddress, '@yahoo') || strpos($emailAddress, '@gmail') || strpos($emailAddress, '@outlook'))) {
23+
throw new InvalidArgumentException("The email address <$emailAddress> has not a common provider");
24+
}
25+
26+
return new Newsletter($emailAddress);
27+
}
28+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\Model;
6+
7+
final class Newsletter
8+
{
9+
private string $emailAddress;
10+
11+
public function __construct(string $emailAddress)
12+
{
13+
$this->emailAddress = $emailAddress;
14+
}
15+
16+
public function emailAddress(): string
17+
{
18+
return $this->emailAddress;
19+
}
20+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\Tests\Controller;
6+
7+
use CodelyTv\Controller\NewsletterController;
8+
use CodelyTv\Model\Newsletter;
9+
use InvalidArgumentException;
10+
use PHPUnit\Framework\TestCase;
11+
12+
final class NewsletterControllerTest extends TestCase
13+
{
14+
/** @test */
15+
public function it_should_create_a_valid_newsletter(): void
16+
{
17+
$controller = new NewsletterController();
18+
19+
$emailAddress = 'codely@gmail.com';
20+
21+
self::assertEquals(new Newsletter('codely@gmail.com'), $controller->post($emailAddress));
22+
}
23+
24+
/** @test */
25+
public function it_should_not_create_a_newsletter_with_an_empty_email(): void
26+
{
27+
$controller = new NewsletterController();
28+
29+
$emailAddress = '';
30+
31+
$this->expectExceptionObject(new InvalidArgumentException('The email address is empty'));
32+
33+
$controller->post($emailAddress);
34+
}
35+
36+
/** @test */
37+
public function it_should_not_create_a_newsletter_with_an_invalid_formatted_email(): void
38+
{
39+
$controller = new NewsletterController();
40+
41+
$emailAddress = 'codely[at]gmail.com';
42+
43+
$this->expectExceptionObject(new InvalidArgumentException("The email address <$emailAddress> is not valid"));
44+
45+
$controller->post($emailAddress);
46+
}
47+
48+
/** @test */
49+
public function it_should_not_create_a_newsletter_with_a_non_common_email_provider(): void
50+
{
51+
$controller = new NewsletterController();
52+
53+
$emailAddress = 'codely@clubdefansdejavi.io';
54+
55+
$this->expectExceptionObject(
56+
new InvalidArgumentException("The email address <$emailAddress> has not a common provider")
57+
);
58+
59+
$controller->post($emailAddress);
60+
}
61+
}

0 commit comments

Comments
 (0)