diff --git a/src/Mailer.php b/src/Mailer.php index c71f25c..805d351 100644 --- a/src/Mailer.php +++ b/src/Mailer.php @@ -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[] */ diff --git a/src/Parameter.php b/src/Parameter.php index dc39aad..6cd97dc 100644 --- a/src/Parameter.php +++ b/src/Parameter.php @@ -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. * @@ -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, + }; + } } diff --git a/src/ParameterInterface.php b/src/ParameterInterface.php index 73d8847..8dea97d 100644 --- a/src/ParameterInterface.php +++ b/src/ParameterInterface.php @@ -4,6 +4,9 @@ namespace Yii\Service; +/** + * Provides a way to get application Parameter defined in `config/application-params.php`. + */ interface ParameterInterface { /** @@ -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; } diff --git a/src/Redirect.php b/src/Redirect.php index 18e99f5..6ebcd7d 100644 --- a/src/Redirect.php +++ b/src/Redirect.php @@ -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( diff --git a/tests/Mailer/ImmutabilityTest.php b/tests/Mailer/ImmutabilityTest.php index 2006966..1c918a3 100644 --- a/tests/Mailer/ImmutabilityTest.php +++ b/tests/Mailer/ImmutabilityTest.php @@ -13,8 +13,6 @@ final class ImmutabilityTest extends TestCase public function testImmutability(): void { - $this->createContainer(); - $mailer = $this->mailer; $this->assertNotSame($mailer, $mailer->attachments([])); diff --git a/tests/Mailer/Test.php b/tests/Mailer/Test.php index 459b412..b86bc7c 100644 --- a/tests/Mailer/Test.php +++ b/tests/Mailer/Test.php @@ -14,8 +14,6 @@ final class Test extends TestCase public function testMailer(): void { - $this->createContainer(); - $this->assertTrue( $this->mailer ->attachments(['@resources/attachment/test.txt']) @@ -49,8 +47,6 @@ public function testMailerFailed(): void public function testMailerWithEmptySignatureImage(): void { - $this->createContainer(); - $this->assertTrue( $this->mailer ->attachments(['@resources/attachment/test.txt']) @@ -66,8 +62,6 @@ public function testMailerWithEmptySignatureImage(): void public function testMailerWithTranslatorCategory(): void { - $this->createContainer(); - $this->assertTrue( $this->mailer ->attachments(['@resources/attachment/test.txt']) diff --git a/tests/Parameter/Test.php b/tests/Parameter/Test.php index 3297b95..69d7d15 100644 --- a/tests/Parameter/Test.php +++ b/tests/Parameter/Test.php @@ -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(); diff --git a/tests/Redirect/RedirectServiceTest.php b/tests/Redirect/Test.php similarity index 86% rename from tests/Redirect/RedirectServiceTest.php rename to tests/Redirect/Test.php index 2b076ee..fefe725 100644 --- a/tests/Redirect/RedirectServiceTest.php +++ b/tests/Redirect/Test.php @@ -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()); @@ -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()); @@ -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()); @@ -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()); diff --git a/tests/Support/TestTrait.php b/tests/Support/TestTrait.php index 1367ab5..3ae369c 100644 --- a/tests/Support/TestTrait.php +++ b/tests/Support/TestTrait.php @@ -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); diff --git a/tests/data/config/application-params.php b/tests/data/config/application-params.php index f4bf44e..8edc13b 100644 --- a/tests/data/config/application-params.php +++ b/tests/data/config/application-params.php @@ -8,5 +8,9 @@ 'aliases' => [ 'tests' => '@root/tests', ], + 'internal_array' => ['key' => 'value'], + 'internal_bool' => true, + 'internal_int' => 1, + 'internal_null' => null, ], ];