Skip to content

Commit

Permalink
Rework code. (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed May 25, 2023
1 parent b3b33da commit 8f043a7
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 17 deletions.
15 changes: 15 additions & 0 deletions src/Mailer.php
Expand Up @@ -16,6 +16,21 @@
use function basename;
use function mime_content_type;

/**
* Provides a way to send email messages.
*
* ```php
* $mailer
* ->attachments(['@resources/attachment/test.txt'])
* ->from('admin@example.com')
* ->layout(['html' => 'contact'])
* ->signatureImage('@resources/attachment/test.txt')
* ->signatureText('Signature')
* ->subject('Test subject')
* ->viewPath('@views')
* ->send('test@example.com', ['message' => 'Test body', 'username' => 'Test username']);
* ```
*/
final class Mailer
{
/** @psalm-var string[] */
Expand Down
22 changes: 22 additions & 0 deletions src/Parameter.php
Expand Up @@ -4,9 +4,14 @@

namespace Yii\Service;

use RuntimeException;
use Yiisoft\Aliases\Aliases;
use Yiisoft\Arrays\ArrayHelper;

use function is_array;
use function is_bool;
use function is_string;

/**
* Parameter provides a way to get application Parameter defined in config/parameters.php.
*
Expand Down Expand Up @@ -45,4 +50,21 @@ public function get(string $key, mixed $default = null): mixed

return $value;
}

public function getCastString(string $key, mixed $default = null): string
{
/** @psalm-var mixed $value */
$value = $this->get($key, $default);

if (is_array($value)) {
throw new RuntimeException(
'Unable to cast array to string. Please use `get()` method instead of `getCastString()`.'
);
}

return match (true) {
is_bool($value) => $value ? 'true' : 'false',
default => (string) $value,
};
}
}
11 changes: 11 additions & 0 deletions src/ParameterInterface.php
Expand Up @@ -4,6 +4,9 @@

namespace Yii\Service;

/**
* Provides a way to get application Parameter defined in `config/application-params.php`.
*/
interface ParameterInterface
{
/**
Expand All @@ -15,4 +18,12 @@ interface ParameterInterface
* @return mixed
*/
public function get(string $key, mixed $default = null): mixed;

/**
* Returns a parameter defined in params as string.
*
* @param string $key The key of the parameter to return.
* @param mixed $default The default value to return if the parameter does not exist.
*/
public function getCastString(string $key, mixed $default = null): string;
}
7 changes: 7 additions & 0 deletions src/Redirect.php
Expand Up @@ -9,6 +9,13 @@
use Psr\Http\Message\ResponseInterface;
use Yiisoft\Router\UrlGeneratorInterface;

/**
* Provides a way to redirect to a URL.
*
* ```php
* $response = $redirect->run('site/index');
* ```
*/
final class Redirect
{
public function __construct(
Expand Down
2 changes: 0 additions & 2 deletions tests/Mailer/ImmutabilityTest.php
Expand Up @@ -13,8 +13,6 @@ final class ImmutabilityTest extends TestCase

public function testImmutability(): void
{
$this->createContainer();

$mailer = $this->mailer;

$this->assertNotSame($mailer, $mailer->attachments([]));
Expand Down
6 changes: 0 additions & 6 deletions tests/Mailer/Test.php
Expand Up @@ -14,8 +14,6 @@ final class Test extends TestCase

public function testMailer(): void
{
$this->createContainer();

$this->assertTrue(
$this->mailer
->attachments(['@resources/attachment/test.txt'])
Expand Down Expand Up @@ -49,8 +47,6 @@ public function testMailerFailed(): void

public function testMailerWithEmptySignatureImage(): void
{
$this->createContainer();

$this->assertTrue(
$this->mailer
->attachments(['@resources/attachment/test.txt'])
Expand All @@ -66,8 +62,6 @@ public function testMailerWithEmptySignatureImage(): void

public function testMailerWithTranslatorCategory(): void
{
$this->createContainer();

$this->assertTrue(
$this->mailer
->attachments(['@resources/attachment/test.txt'])
Expand Down
27 changes: 27 additions & 0 deletions tests/Parameter/Test.php
Expand Up @@ -11,6 +11,33 @@ final class Test extends TestCase
{
use TestTrait;

public function testGetCastString(): void
{
$this->createContainer();

$this->assertSame('value', $this->parameter->getCastString('app.internal_array.key'));
$this->assertSame('1', $this->parameter->getCastString('app.internal_int'));
$this->assertSame('true', $this->parameter->getCastString('app.internal_bool'));
$this->assertSame('', $this->parameter->getCastString('app.internal_null'));
}

public function testGetCastStringException(): void
{
$this->createContainer();

$this->expectExceptionMessage(
'Unable to cast array to string. Please use `get()` method instead of `getCastString()',
);
$this->parameter->getCastString('app.internal_array');
}

public function testGetCastStringWithDefaultValue(): void
{
$this->createContainer();

$this->assertSame('default', $this->parameter->getCastString('app.noExist', 'default'));
}

public function testParameterExists(): void
{
$this->createContainer();
Expand Down
Expand Up @@ -7,14 +7,12 @@
use PHPUnit\Framework\TestCase;
use Yii\Service\Tests\Support\TestTrait;

final class RedirectServiceTest extends TestCase
final class Test extends TestCase
{
use TestTrait;

public function testRedirect(): void
{
$this->createContainer();

$redirect = $this->redirect->run('home');

$this->assertSame(302, $redirect->getStatusCode());
Expand All @@ -23,8 +21,6 @@ public function testRedirect(): void

public function testRedirectWithArguments(): void
{
$this->createContainer();

$redirect = $this->redirect->run('home', 302, ['id' => 1]);

$this->assertSame(302, $redirect->getStatusCode());
Expand All @@ -33,8 +29,6 @@ public function testRedirectWithArguments(): void

public function testRedirectWithCode200(): void
{
$this->createContainer();

$redirect = $this->redirect->run('home', 200);

$this->assertSame(200, $redirect->getStatusCode());
Expand All @@ -43,8 +37,6 @@ public function testRedirectWithCode200(): void

public function testRedirectWithQueryParams(): void
{
$this->createContainer();

$redirect = $this->redirect->run('home', 302, [], ['id' => 1]);

$this->assertSame(302, $redirect->getStatusCode());
Expand Down
5 changes: 5 additions & 0 deletions tests/Support/TestTrait.php
Expand Up @@ -26,6 +26,11 @@ trait TestTrait
private Redirect $redirect;
protected bool $writeToFiles = true;

public function setup(): void
{
$this->createContainer();
}

public function tearDown(): void
{
unset($this->aliases, $this->logger, $this->mailer, $this->parameter, $this->redirect);
Expand Down
4 changes: 4 additions & 0 deletions tests/data/config/application-params.php
Expand Up @@ -8,5 +8,9 @@
'aliases' => [
'tests' => '@root/tests',
],
'internal_array' => ['key' => 'value'],
'internal_bool' => true,
'internal_int' => 1,
'internal_null' => null,
],
];

0 comments on commit 8f043a7

Please sign in to comment.