Skip to content

Commit

Permalink
Merge pull request #3 from LemonMind/tests
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
pfilipkowskilemon authored Aug 10, 2022
2 parents 65d1bf6 + 0347df2 commit 88e9c0d
Show file tree
Hide file tree
Showing 8 changed files with 546 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"require-dev": {
"phpstan/phpstan": "^1.8",
"phpstan/phpstan-symfony": "^1.2",
"friendsofphp/php-cs-fixer": "^3.9"
"friendsofphp/php-cs-fixer": "^3.9",
"symfony/phpunit-bridge": "^6.1"
},
"autoload": {
"psr-4": {
Expand Down
10 changes: 10 additions & 0 deletions src/MessageBundle/Tests/ChatterControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace LemonMind\MessageBundle\Tests;

use PHPUnit\Framework\TestCase;

class ChatterControllerTest extends TestCase
{

}
103 changes: 103 additions & 0 deletions src/MessageBundle/Tests/Model/DiscordMessageModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

namespace LemonMind\MessageBundle\Tests;

use LemonMind\MessageBundle\Model\DiscordMessageModel;
use Pimcore\Bundle\EcommerceFrameworkBundle\Model\AbstractProduct;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Test\KernelTestCase;

class DiscordMessageModelTest extends KernelTestCase
{
/**
* @var AbstractObject
*/
private $testProduct;

protected function setUp(): void
{
$this->testProduct = new class() extends AbstractProduct {
public function getId(): int
{
return 1;
}

public function getName(): string
{
return 'name';
}

public function getPrice(): string
{
return '20';
}
};
}

public function testTitle(): void
{
$discordMessage = new DiscordMessageModel($this->testProduct, [], '');
$options = $discordMessage->create()->getOptions();

if (!is_null($options)) {
$options = $options->toArray();
} else {
throw new \Exception('options is null');
}

$this->assertEquals('Object id 1', $options['embeds'][0]['title']);
}

/**
* @test
* @dataProvider dataProvider
*/
public function testEmbedFields(array $fields, string $additionalInfo, string $expected): void
{
$discordMessage = new DiscordMessageModel($this->testProduct, $fields, $additionalInfo);

if (!$fields) {
$this->assertEquals($expected, $additionalInfo);

return;
}

$options = $discordMessage->create()->getOptions();

if (!is_null($options)) {
$options = $options->toArray();
} else {
throw new \Exception('options is null');
}

$actualFields = $options['embeds'][0]['fields'];
$actual = '';

foreach ($actualFields as $field) {
$actual .= $field['value'] . ';';
}

$actual = rtrim($actual, ';');

$this->assertEquals($expected, $actual);
}

public function dataProvider(): array
{
return [
[[], '', ''],
[[], 'lorem', 'lorem'],
[['name'], '', 'name'],
[['price'], '', '20'],
[['name'], 'lorem', 'name;lorem'],
[['name', 'price'], 'lorem', 'name;20;lorem'],
];
}

// protected function tearDown(): void
// {
// $this->testProduct = null;
// }
}
56 changes: 56 additions & 0 deletions src/MessageBundle/Tests/Model/EmailMessageModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace LemonMind\MessageBundle\Tests\Model;

use App\Model\Product\AbstractProduct;
use LemonMind\MessageBundle\Model\EmailMessageModel;
use Pimcore\Test\KernelTestCase;

class EmailMessageModelTest extends KernelTestCase
{
private AbstractProduct $testProduct;

protected function setUp(): void
{
$this->testProduct = new class() extends AbstractProduct {
public function getId(): int
{
return 1;
}

public function getName(): string
{
return 'name';
}

public function getPrice(): string
{
return '20';
}
};
}

/**
* @test
* @dataProvider dataProvider
*/
public function testCreate(array $fields, string $additionalInfo, string $expected): void
{
$emailMessage = new EmailMessageModel($this->testProduct, $fields, $additionalInfo);
$this->assertEquals($expected, $emailMessage->create());
}

public function dataProvider(): array
{
return [
[[], '', '<table></table>'],
[[], 'lorem', '<table></table><br>lorem'],
[['name'], '', '<table><tr><td>name</td><td>name</td></tr></table>'],
[['price'], '', '<table><tr><td>price</td><td>20</td></tr></table>'],
[['name', 'price'], '', '<table><tr><td>name</td><td>name</td></tr><tr><td>price</td><td>20</td></tr></table>'],
[['name', 'price'], 'lorem', '<table><tr><td>name</td><td>name</td></tr><tr><td>price</td><td>20</td></tr></table><br>lorem'],
];
}
}
130 changes: 130 additions & 0 deletions src/MessageBundle/Tests/Model/GoogleChatMessageModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

declare(strict_types=1);

namespace LemonMind\MessageBundle\Tests;

use LemonMind\MessageBundle\Model\GoogleChatMessageModel;
use Pimcore\Bundle\EcommerceFrameworkBundle\Model\AbstractProduct;
use Pimcore\Test\KernelTestCase;

class GoogleChatMessageModelTest extends KernelTestCase
{
/**
* @var AbstractProduct
*/
private $testProduct;

protected function setUp(): void
{
$this->testProduct = new class() extends AbstractProduct {
public function getId(): int
{
return 1;
}

public function getName(): string
{
return 'name';
}

public function getPrice(): string
{
return '20';
}
};
}

/**
* @test
*/
public function testHeader(): void
{
$googleMessage = new GoogleChatMessageModel($this->testProduct, [], '');
$options = $googleMessage->create()->getOptions();

if (!is_null($options)) {
$options = $options->toArray();
} else {
throw new \Exception('options is null');
}

$header = $options['cards'][0]['header'];
$this->assertEquals('Object id 1', $header['title']);
}

/**
* @test
* @dataProvider dataProvider
*/
public function testAdditionalInfoWidget(array $fields, string $additionalInfo, array $expected): void
{
$googleMessage = new GoogleChatMessageModel($this->testProduct, [], $additionalInfo);
$options = $googleMessage->create()->getOptions();

if (!is_null($options)) {
$options = $options->toArray();
} else {
throw new \Exception('options is null');
}

if ('' === $additionalInfo) {
$this->assertLessThan(2, count($options['cards'][0]['sections']));

return;
}

$additionalInfoWidget = $options['cards'][0]['sections'][1]['widgets'];

$expectedWidget = [
['textParagraph' => ['text' => 'Additional information']],
['textParagraph' => ['text' => $additionalInfo]],
];

$this->assertEquals($expectedWidget, $additionalInfoWidget);
}

/**
* @test
* @dataProvider dataProvider
*/
public function testDataWidget(array $fields, string $additionalInfo, array $expected): void
{
$googleMessage = new GoogleChatMessageModel($this->testProduct, $fields, $additionalInfo);
$options = $googleMessage->create()->getOptions();

if (!is_null($options)) {
$options = $options->toArray();
} else {
throw new \Exception('options is null');
}

$dataWidget = $options['cards'][0]['sections'][0]['widgets'];

if (!$fields) {
$this->assertEquals(0, count($dataWidget));
}

foreach ($fields as $field) {
$needle = [
'topLabel' => $field,
'content' => $expected[$field],
];

$r = array_search($needle, array_column($dataWidget, 'keyValue'), true);
$this->assertNotFalse($r);
}
}

public function dataProvider(): array
{
return [
[[], '', []],
[[], 'lorem', []],
[['name'], '', ['name' => 'name']],
[['price'], '', ['price' => '20']],
[['name'], 'lorem', ['name' => 'name']],
[['name', 'price'], 'lorem', ['name' => 'name', 'price' => '20']],
];
}
}
Loading

0 comments on commit 88e9c0d

Please sign in to comment.