1. Go to this page and download the library: Download samuelgfeller/test-traits library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// Inserts the fixture with the first_name being "Bob" and the rest default values from the fixture.
// Returns the inserted row data with the auto-incremented id.
$bobUserRow = $this->insertFixture(UserFixture::class, ['first_name' => 'Bob']);
namespace App\Test\Fixture;
class UserFixture
{
// Database table name
public string $table = 'user';
// Database records
public array $records = [
// Only one record is relevant as the data is defined in the test function
[
// The id
'id' => 1,
'first_name' => 'John',
'last_name' => 'Doe',
],
];
}
namespace App\Test\TestCase;
use PHPUnit\Framework\TestCase;
use TestTraits\Trait\FixtureTestTrait;
final class FetchUsersTestAction extends TestCase
{
// ...
use FixtureTestTrait;
public function testAction(): void
{
// Insert the fixture with the default values
$defaultUserRow = $this->insertFixture(UserFixture::class);
// $defaultUserRow equals ['id' => 1, 'first_name' => 'John', 'last_name' => 'Doe']
// Insert the fixture with the given attributes
$bobUserRow = $this->insertFixture(UserFixture::class, ['first_name' => 'Bob', ]);
// $bobUserRow equals ['id' => 2, 'first_name' => 'Bob', 'last_name' => 'Doe']
// Insert 2 rows with the given attributes
$jackAndAliceRows = $this->insertFixture(
UserFixture::class,
['first_name' => 'Jack', 'last_name' => 'Brown'],
['first_name' => 'Alice']
);
// $jackAndAliceRows contains the two inserted rows:
// [
// ['id' => 3, 'first_name' => 'Jack', 'last_name' => 'Brown'],
// ['id' => 4, 'first_name' => 'Alice', 'last_name' => 'Doe']
// ]
// Multiple rows can also be inserted when passing one attribute argument that contains
// multiple arrays for each row
$benAndEveRows = $this->insertFixture(
UserFixture::class, [
['first_name' => 'Jack', 'last_name' => 'Brown'],
['first_name' => 'Alice']
]
);
// ...
}
}
namespace App\Test\TestCase;
use PHPUnit\Framework\TestCase;
use TestTraits\Trait\ContainerTestTrait;
use TestTraits\Trait\HttpTestTrait;
class GetUsersTestAction extends TestCase
{
use ContainerTestTrait;
use HttpTestTrait;
public function test(): void
{
$request = $this->createRequest('GET', '/api/users');
$response = $this->app->handle($request);
$this->assertSame(200, $response->getStatusCode());
}
}
namespace App\Test\TestCase;
use PHPUnit\Framework\TestCase;
use TestTraits\Trait\ContainerTestTrait;
use TestTraits\Trait\HttpTestTrait;
use TestTraits\Trait\RouteTestTrait;
final class GetUsersTestAction extends TestCase
{
use ContainerTestTrait;
use HttpTestTrait;
use RouteTestTrait;
public function test(): void
{
$request = $this->createRequest('GET', $this->urlFor('get-users'));
$response = $this->app->handle($request);
$this->assertSame(200, $response->getStatusCode());
}
}
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Mailer\EventListener\EnvelopeListener;
use Symfony\Component\Mailer\EventListener\MessageListener;
use Symfony\Component\Mailer\EventListener\MessageLoggerListener;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Transport\TransportInterface;
// ...
return [
// Mailer
MailerInterface::class => function (ContainerInterface $container) {
return new Mailer($container->get(TransportInterface::class));
},
// Mailer transport
TransportInterface::class => function (ContainerInterface $container) {
$settings = $container->get('settings')['smtp'];
// smtp://user:[email protected]:25
$dsn = sprintf(
'%s://%s:%s@%s:%s',
$settings['type'],
$settings['username'],
$settings['password'],
$settings['host'],
$settings['port']
);
$eventDispatcher = $container->get(EventDispatcherInterface::class);
return Transport::fromDsn($dsn, $eventDispatcher);
},
EventDispatcherInterface::class => function () {
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber(new MessageListener());
$eventDispatcher->addSubscriber(new EnvelopeListener());
$eventDispatcher->addSubscriber(new MessageLoggerListener());
return $eventDispatcher;
},
// ...
],
namespace App\Test\TestCase;
use PHPUnit\Framework\TestCase;
use TestTraits\Trait\ContainerTestTrait;
use TestTraits\Trait\MailerTestTrait;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
final class MailerExampleTest extends TestCase
{
use ContainerTestTrait;
use MailerTestTrait;
public function testMailer(): void
{
$mailer = $this->container->get(MailerInterface::class);
// Send email
$email = (new Email())
->from('[email protected]')
->to('[email protected]')
->subject('Time for Symfony Mailer!')
->text('Sending emails is fun again!')
->html('<p>My HTML content</p>');
$mailer->send($email);
$this->assertEmailCount(1);
$this->assertEmailTextBodyContains($this->getMailerMessage(), 'Sending emails is fun again!');
$this->assertEmailHtmlBodyContains($this->getMailerMessage(), '<p>My HTML content</p>');
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.