yii2-bootstrap5/tests/ToastTest.php

191 lines
5.2 KiB
PHP
Raw Normal View History

<?php
namespace yiiunit\extensions\bootstrap5;
2021-08-16 10:09:09 +02:00
use PHPUnit\Framework\Constraint\IsType;
use Yii;
use yii\bootstrap5\Toast;
2021-08-16 10:09:09 +02:00
use yii\web\View;
/**
* @group bootstrap5
*/
class ToastTest extends TestCase
{
public function testBodyOptions()
{
Toast::$counter = 0;
$out = Toast::widget([
'bodyOptions' => ['class' => 'toast-body test', 'style' => ['text-align' => 'center']]
]);
$expected = <<<HTML
<div id="w0" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
2021-07-12 17:22:29 +02:00
<strong class="me-auto"></strong>
2021-08-18 15:41:26 +02:00
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body test" style="text-align: center;">
</div>
</div>
HTML;
$this->assertEqualsWithoutLE($expected, $out);
}
/**
*/
public function testContainerOptions()
{
Toast::$counter = 0;
ob_start();
Toast::begin([
'title' => 'Toast title',
'dateTime' => time() - 60
]);
echo 'Woohoo, you\'re reading this text in a toast!';
Toast::end();
$out = ob_get_clean();
$expected = <<<HTML
<div id="w0" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
2021-07-12 17:22:29 +02:00
<strong class="me-auto">Toast title</strong>
<small class="text-muted">a minute ago</small>
2021-08-18 15:41:26 +02:00
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Woohoo, you're reading this text in a toast!
</div>
</div>
HTML;
$this->assertEqualsWithoutLE($expected, $out);
}
public function testDateTimeOptions()
{
Toast::$counter = 0;
$out = Toast::widget([
'title' => 'Toast title',
'dateTime' => time() - 60,
'dateTimeOptions' => ['class' => ['toast-date-time'], 'style' => ['text-align' => 'right']]
]);
$expected = <<<HTML
<div id="w0" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
2021-07-12 17:22:29 +02:00
<strong class="me-auto">Toast title</strong>
<small class="toast-date-time text-muted" style="text-align: right;">a minute ago</small>
2021-08-18 15:41:26 +02:00
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
</div>
</div>
HTML;
$this->assertEqualsWithoutLE($expected, $out);
}
public function testTitleOptions()
{
Toast::$counter = 0;
$out = Toast::widget([
'title' => 'Toast title',
'titleOptions' => ['tag' => 'h5', 'style' => ['text-align' => 'left']]
]);
$expected = <<<HTML
<div id="w0" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
2021-07-12 17:22:29 +02:00
<h5 class="me-auto" style="text-align: left;">Toast title</h5>
2021-08-18 15:41:26 +02:00
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
</div>
</div>
HTML;
$this->assertEqualsWithoutLE($expected, $out);
}
2021-08-16 10:09:09 +02:00
/**
* @see https://github.com/yiisoft/yii2-bootstrap5/issues/5
*/
public function testWidgetInitialization()
{
Toast::$counter = 0;
ob_start();
$toast = Toast::begin([
'title' => 'Toast title',
'titleOptions' => ['tag' => 'h5', 'style' => ['text-align' => 'left']]
]);
echo 'test';
Toast::end();
$out = ob_get_clean();
2024-03-28 16:27:15 +08:00
$this->assertIsArray($toast->clientOptions);
2021-08-16 10:09:09 +02:00
$this->assertCount(0, $toast->clientOptions);
$js = Yii::$app->view->js[View::POS_READY];
2024-03-28 16:27:15 +08:00
// $this->assertInternalType(IsType::TYPE_ARRAY, $js);
$this->assertIsArray($js);
2021-08-16 10:09:09 +02:00
$options = array_shift($js);
$this->assertContainsWithoutLE("(new bootstrap.Toast('#w0', {}));", $options);
2021-08-16 10:09:09 +02:00
}
2022-03-10 23:43:09 +01:00
/**
* @see https://github.com/yiisoft/yii2-bootstrap5/issues/36
*/
public function testWidgetNoInitialization()
{
Toast::$counter = 0;
ob_start();
$toast = Toast::begin([
'title' => 'Toast title',
'clientOptions' => false,
'titleOptions' => ['tag' => 'h5', 'style' => ['text-align' => 'left']]
]);
echo 'test';
Toast::end();
$out = ob_get_clean();
$this->assertFalse($toast->clientOptions);
$this->assertArrayHasKey(View::POS_READY, Yii::$app->view->js);
2022-03-10 23:43:09 +01:00
}
public function testWidgetInitializationWithClientOptions()
2022-03-10 23:43:09 +01:00
{
Toast::$counter = 0;
ob_start();
$toast = Toast::begin([
'title' => 'Toast title',
'clientOptions' => ['delay' => 1000],
2022-03-10 23:43:09 +01:00
'titleOptions' => ['tag' => 'h5', 'style' => ['text-align' => 'left']]
]);
echo 'test';
Toast::end();
$out = ob_get_clean();
$this->assertArrayHasKey('delay', $toast->clientOptions);
$this->assertArrayHasKey(View::POS_READY, Yii::$app->view->js);
2022-03-10 23:43:09 +01:00
$js = Yii::$app->view->js[View::POS_READY];
2024-03-28 16:27:15 +08:00
// $this->assertInternalType(IsType::TYPE_ARRAY, $js);
$this->assertIsArray($js);
2022-03-10 23:43:09 +01:00
$options = array_shift($js);
$this->assertContainsWithoutLE("(new bootstrap.Toast('#w0', {\"delay\":1000}));", $options);
2022-03-10 23:43:09 +01:00
}
}