Skip to content

Commit ac709ff

Browse files
committed
ISSUE-345: tests
1 parent d199301 commit ac709ff

File tree

4 files changed

+290
-62
lines changed

4 files changed

+290
-62
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,3 @@
1616
.vagrant
1717
.phpdoc/
1818
.phpunit.result.cache
19-
/.env
20-
/.env.local

README.md

Lines changed: 27 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ phpList is an open source newsletter manager. This project is a rewrite of the
1616

1717
## About this package
1818

19-
This is the core module of the successor to phpList 3. It will have the
19+
This is the core module of the successor to phpList 3. It will have the
2020
following responsibilities:
2121

2222
* provide access to the DB via Doctrine models and repositories (and raw SQL
@@ -41,7 +41,7 @@ Since this package is only a service required to run a full installation of **ph
4141

4242
## Contributing to this package
4343

44-
Contributions to phpList repositories are highly welcomed! To get started please take a look at the [contribution guide](.github/CONTRIBUTING.md). It contains everything you would need to make your first contribution including how to run local style checks and run tests.
44+
Contributions to phpList repositories are highly welcomed! To get started please take a look at the [contribution guide](.github/CONTRIBUTING.md). It contains everything you would need to make your first contribution including how to run local style checks and run tests.
4545

4646
### Code of Conduct
4747

@@ -63,40 +63,7 @@ this code.
6363
The phpList application is configured so that the built-in PHP web server can
6464
run in development and testing mode, while Apache can run in production mode.
6565

66-
Please first set the database credentials in `config/parameters.yml` or in the `.env` file (see below).
67-
68-
## Environment Variables
69-
70-
phpList now supports loading environment variables from `.env` files. This allows you to configure your application without modifying any code or configuration files.
71-
72-
### Usage
73-
74-
1. Copy the `.env.example` file to `.env` in the project root:
75-
```bash
76-
cp .env.example .env
77-
```
78-
79-
2. Edit the `.env` file and customize the variables according to your environment:
80-
```
81-
# Application environment (dev, test, prod)
82-
APP_ENV=dev
83-
84-
# Debug mode (1 for enabled, 0 for disabled)
85-
APP_DEBUG=1
86-
87-
# Database configuration
88-
DATABASE_URL=mysql://db_user:db_password@localhost:3306/db_name
89-
90-
# Mailer configuration
91-
MAILER_DSN=smtp://localhost:25
92-
93-
# Secret key for security
94-
APP_SECRET=change_this_to_a_secret_value
95-
```
96-
97-
3. For local overrides, you can create a `.env.local` file which will be loaded after `.env`.
98-
99-
Note: The `.env` and `.env.local` files are excluded from version control to prevent sensitive information from being committed.
66+
Please first set the database credentials in `config/parameters.yml`.
10067

10168
### Development
10269

@@ -112,9 +79,9 @@ already in use, on the next free port after 8000).
11279

11380
You can stop the server with CTRL + C.
11481

115-
#### Development and Documentation
82+
#### Development and Documentation
11683

117-
We use `phpDocumentor` to automatically generate documentation for classes. To make this process efficient and easier, you are required to properly "document" your `classes`,`properties`, `methods` ... by annotating them with [docblocks](https://docs.phpdoc.org/latest/guide/guides/docblocks.html).
84+
We use `phpDocumentor` to automatically generate documentation for classes. To make this process efficient and easier, you are required to properly "document" your `classes`,`properties`, `methods` ... by annotating them with [docblocks](https://docs.phpdoc.org/latest/guide/guides/docblocks.html).
11885

11986
More about generatings docs in [PHPDOC.md](PHPDOC.md)
12087

@@ -156,12 +123,12 @@ listed in the `extra` section of the module's `composer.json` like this:
156123

157124
```json
158125
"extra": {
159-
"phplist/core": {
160-
"bundles": [
161-
"Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle",
162-
"PhpList\\Core\\EmptyStartPageBundle\\PhpListEmptyStartPageBundle"
163-
]
164-
}
126+
"phplist/core": {
127+
"bundles": [
128+
"Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle",
129+
"PhpList\\Core\\EmptyStartPageBundle\\PhpListEmptyStartPageBundle"
130+
]
131+
}
165132
}
166133
```
167134

@@ -176,32 +143,32 @@ the `extra` section of the module's `composer.json` like this:
176143

177144
```json
178145
"extra": {
179-
"phplist/core": {
180-
"routes": {
181-
"homepage": {
182-
"resource": "@PhpListEmptyStartPageBundle/Controller/",
183-
"type": "annotation"
184-
}
185-
}
146+
"phplist/core": {
147+
"routes": {
148+
"homepage": {
149+
"resource": "@PhpListEmptyStartPageBundle/Controller/",
150+
"type": "annotation"
151+
}
186152
}
153+
}
187154
}
188155
```
189156

190157
You can also provide system configuration for your module:
191158

192159
```json
193160
"extra": {
194-
"phplist/core": {
195-
"configuration": {
196-
"framework": {
197-
"templating": {
198-
"engines": [
199-
"twig"
200-
]
201-
}
202-
}
161+
"phplist/core": {
162+
"configuration": {
163+
"framework": {
164+
"templating": {
165+
"engines": [
166+
"twig"
167+
]
203168
}
169+
}
204170
}
171+
}
205172
}
206173
```
207174

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\Core\Tests\Unit\Domain\Messaging\Command;
6+
7+
use PhpList\Core\Domain\Messaging\Command\SendTestEmailCommand;
8+
use PhpList\Core\Domain\Messaging\Service\EmailService;
9+
use PHPUnit\Framework\MockObject\MockObject;
10+
use PHPUnit\Framework\TestCase;
11+
use Symfony\Component\Console\Application;
12+
use Symfony\Component\Console\Tester\CommandTester;
13+
use Symfony\Component\Mime\Email;
14+
15+
class SendTestEmailCommandTest extends TestCase
16+
{
17+
private EmailService&MockObject $emailService;
18+
private CommandTester $commandTester;
19+
20+
protected function setUp(): void
21+
{
22+
$this->emailService = $this->createMock(EmailService::class);
23+
$command = new SendTestEmailCommand($this->emailService);
24+
25+
$application = new Application();
26+
$application->add($command);
27+
28+
$this->commandTester = new CommandTester($command);
29+
}
30+
31+
public function testExecuteWithValidEmail(): void
32+
{
33+
$this->emailService->expects($this->once())
34+
->method('sendEmail')
35+
->with($this->callback(function (Email $email) {
36+
$this->assertEquals('Test Email from phpList', $email->getSubject());
37+
$this->assertStringContainsString('This is a test email', $email->getTextBody());
38+
$this->assertStringContainsString('<h1>Test</h1>', $email->getHtmlBody());
39+
40+
$toAddresses = $email->getTo();
41+
$this->assertCount(1, $toAddresses);
42+
$this->assertEquals('[email protected]', $toAddresses[0]->getAddress());
43+
44+
$fromAddresses = $email->getFrom();
45+
$this->assertCount(1, $fromAddresses);
46+
$this->assertEquals('[email protected]', $fromAddresses[0]->getAddress());
47+
$this->assertEquals('Admin Team', $fromAddresses[0]->getName());
48+
49+
return true;
50+
}));
51+
52+
$this->commandTester->execute([
53+
'recipient' => '[email protected]',
54+
]);
55+
56+
$output = $this->commandTester->getDisplay();
57+
$this->assertStringContainsString('Test email sent successfully', $output);
58+
59+
$this->assertEquals(0, $this->commandTester->getStatusCode());
60+
}
61+
62+
public function testExecuteWithoutRecipient(): void
63+
{
64+
$this->emailService->expects($this->never())
65+
->method('sendEmail');
66+
67+
$this->commandTester->execute([]);
68+
69+
$output = $this->commandTester->getDisplay();
70+
$this->assertStringContainsString('Recipient email address not provided', $output);
71+
72+
$this->assertEquals(1, $this->commandTester->getStatusCode());
73+
}
74+
75+
public function testExecuteWithInvalidEmail(): void
76+
{
77+
$this->emailService->expects($this->never())
78+
->method('sendEmail');
79+
80+
$this->commandTester->execute([
81+
'recipient' => 'invalid-email',
82+
]);
83+
84+
$output = $this->commandTester->getDisplay();
85+
$this->assertStringContainsString('Invalid email address', $output);
86+
87+
$this->assertEquals(1, $this->commandTester->getStatusCode());
88+
}
89+
90+
public function testExecuteWithEmailServiceException(): void
91+
{
92+
$this->emailService->expects($this->once())
93+
->method('sendEmail')
94+
->willThrowException(new \Exception('Test exception'));
95+
96+
$this->commandTester->execute([
97+
'recipient' => '[email protected]',
98+
]);
99+
100+
$output = $this->commandTester->getDisplay();
101+
$this->assertStringContainsString('Failed to send test email', $output);
102+
$this->assertStringContainsString('Test exception', $output);
103+
104+
$this->assertEquals(1, $this->commandTester->getStatusCode());
105+
}
106+
}

0 commit comments

Comments
 (0)