Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Test enhancement #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.idea/
build/
vendor/
composer.lock
composer.lock
.phpunit.result.cache
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ after_script:
- if [ $TRAVIS_PHP_VERSION = '7.2' ]; then php vendor/bin/coveralls; fi

after_success:
- travis_retry php vendor/bin/coveralls -v
- travis_retry php vendor/bin/coveralls -v
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
"selami/stdlib": "^1.8"
},
"require-dev": {
"phpunit/phpunit": "^7.5",
"phpunit/phpunit": "^8.0",
"php-coveralls/php-coveralls": "^2.1",
"phpunit/phpcov": "^5.0",
"phpunit/phpcov": "^6.0",
"zendframework/zend-servicemanager":"~3.3",
"twig/twig": "^2.6",
"twig/extensions": "^1.5"
Expand All @@ -35,5 +35,10 @@
"Selami\\View\\Twig\\": "src/Twig/",
"Selami\\View\\Twig\\Functions\\": "src/Twig/Functions"
}
},
"autoload-dev": {
"psr-4": {
"TwigTests\\": "test/"
}
}
}
57 changes: 27 additions & 30 deletions test/TwigTest/TwigTest.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?php

namespace tests;
namespace TwigTests;

use Selami\View\Twig\Twig;
use InvalidArgumentException;
use BadMethodCallException;
use PHPUnit\Framework\TestCase;
use Twig\Loader\FilesystemLoader;
use Twig\Environment as TwigEnvironment;
use Twig\Error\RuntimeError;
use Zend\ServiceManager\ServiceManager;

class myTwigClass extends TestCase
class TwigTest extends TestCase
{
private $config = [
'runtime' => [
Expand All @@ -28,7 +27,7 @@ class myTwigClass extends TestCase
'title' => 'Twig::test',
]
],
'app_namespace' => 'TwigTest',
'app_namespace' => 'TwigTests',
'templates_path' => __DIR__ . '/templates',
'cache_dir' => '/tmp',
'title' => 'Twig::test',
Expand All @@ -41,7 +40,7 @@ class myTwigClass extends TestCase

private $view;

public function setUp()
protected function setUp(): void
{

$container = new ServiceManager();
Expand All @@ -63,18 +62,18 @@ public function setUp()
public function shouldRenderHtmlAndAddGlobalsSuccessfully()
{
$this->view->addGlobal('add_global', 'ok');
$result = $this->view->render('main.twig', ['app' => ['name' => 'myTwigClassTest']]);
$this->assertContains(
$result = $this->view->render('main.twig', ['app' => ['name' => TwigTest::class]]);
$this->assertStringContainsString(
'<span id="add_global">ok</span>',
$result,
'Twig didn\'t correctly render and add globals.'
);
$this->assertContains(
$this->assertStringContainsString(
'<span id="url_param">1</span>',
$result,
'Twig didn\'t correctly render and add globals.'
);
$this->assertContains('<title>Twig::test</title>', $result, "Twig didn't correctly render and add globals.");
$this->assertStringContainsString('<title>Twig::test</title>', $result, "Twig didn't correctly render and add globals.");
}


Expand All @@ -85,8 +84,8 @@ public function shouldRenderHtmlAndAddGlobalsSuccessfully()
public function shouldExtendForGetUrlSuccessfully()
{
$result = $this->view->render('get_url.twig', ['loaded_language' => 'tr_TR']);
$this->assertContains('<span>http://127.0.0.1/logout</span>', $result, "Twig didn't correctly get url.");
$this->assertContains('<span>http://127.0.0.1/tr_TR/about</span>', $result, "Twig didn't correctly get url.");
$this->assertStringContainsString('<span>http://127.0.0.1/logout</span>', $result, "Twig didn't correctly get url.");
$this->assertStringContainsString('<span>http://127.0.0.1/tr_TR/about</span>', $result, "Twig didn't correctly get url.");
}


Expand All @@ -96,8 +95,8 @@ public function shouldExtendForGetUrlSuccessfully()
public function shouldExtendForTranslatorSuccessfully()
{
$result = $this->view->render('translation.twig');
$this->assertContains('Merhaba Selami', $result, "Twig didn't correctly translate.");
$this->assertContains('Text missing in dictionary', $result, "Twig didn't correctly translate.");
$this->assertStringContainsString('Merhaba Selami', $result, "Twig didn't correctly translate.");
$this->assertStringContainsString('Text missing in dictionary', $result, "Twig didn't correctly translate.");
}

/**
Expand All @@ -106,8 +105,8 @@ public function shouldExtendForTranslatorSuccessfully()
public function shouldExtendForQueryParamsSuccessfully()
{
$result = $this->view->render('query_params.twig', ['parameters' => $this->config['runtime']['query_parameters']]);
$this->assertContains('<span>?param1=1&param2=2&param3=3</span>', $result, "Twig didn't correctly build http query.");
$this->assertContains('<span>?controller=login&param1=1&param2=2&param3=3</span>', $result, "Twig didn't correctly build http query.");
$this->assertStringContainsString('<span>?param1=1&param2=2&param3=3</span>', $result, "Twig didn't correctly build http query.");
$this->assertStringContainsString('<span>?controller=login&param1=1&param2=2&param3=3</span>', $result, "Twig didn't correctly build http query.");
}

/**
Expand All @@ -116,8 +115,8 @@ public function shouldExtendForQueryParamsSuccessfully()
public function shouldExtendForSiteUrlSuccessfully()
{
$result = $this->view->render('site_url.twig', ['parameters' => $this->config['runtime']['query_parameters']]);
$this->assertContains('<span id="single">http://127.0.0.1</span>', $result, "Twig didn't correctly return base_url.");
$this->assertContains('<span id="with_parameter">http://127.0.0.1/login</span>', $result, "Twig didn't correctly return base_url.");
$this->assertStringContainsString('<span id="single">http://127.0.0.1</span>', $result, "Twig didn't correctly return base_url.");
$this->assertStringContainsString('<span id="with_parameter">http://127.0.0.1/login</span>', $result, "Twig didn't correctly return base_url.");
}

/**
Expand All @@ -126,10 +125,10 @@ public function shouldExtendForSiteUrlSuccessfully()
public function shouldExtendForVarDumpSuccessfully()
{
$result = $this->view->render('var_dump.twig', ['parameters' => $this->config['runtime']['query_parameters']]);
$this->assertContains('array(3)', $result, "Twig didn't correctly dump variable.");
$this->assertContains('param1', $result, "Twig didn't correctly dump variable.");
$this->assertContains('param2', $result, "Twig didn't correctly dump variable.");
$this->assertContains('param3', $result, "Twig didn't correctly dump variable.");
$this->assertStringContainsString('array(3)', $result, "Twig didn't correctly dump variable.");
$this->assertStringContainsString('param1', $result, "Twig didn't correctly dump variable.");
$this->assertStringContainsString('param2', $result, "Twig didn't correctly dump variable.");
$this->assertStringContainsString('param3', $result, "Twig didn't correctly dump variable.");
}

/**
Expand All @@ -144,20 +143,18 @@ public function shouldExtendForPaginationSuccessfully()
];
$expected = '<ul class="pagination"><li class=""><a href="http://127.0.0.1/list?page_num=1" class="">1</a></li><li class=""><a href="http://127.0.0.1/list?page_num=2" class="">2</a></li><li class=""><a href="http://127.0.0.1/list?page_num=3" class="">3</a></li><li><a>...</a></li><li class=""><a href="http://127.0.0.1/list?page_num=7" class="">7</a></li><li class="active"><a href="http://127.0.0.1/list?page_num=8" class="active">8</a></li><li class=""><a href="http://127.0.0.1/list?page_num=9" class="">9</a></li><li><a>...</a></li><li class=""><a href="http://127.0.0.1/list?page_num=18" class="">18</a></li><li class=""><a href="http://127.0.0.1/list?page_num=19" class="">19</a></li><li class=""><a href="http://127.0.0.1/list?page_num=20" class="">20</a></li></ul>';
$result = $this->view->render('pagination.twig', $data);
$this->assertContains($expected, $result, "Twig didn't correctly return pagination.");
$this->assertStringContainsString($expected, $result, "Twig didn't correctly return pagination.");
}


/**
* @test
* @expectedException Twig_Error_Runtime
*/
public function shouldExtendForWidgetsSuccessfully()
{
$result = $this->view->render('widgets.twig', ['parameters' => $this->config['runtime']['query_parameters']]);
$this->assertContains('<span id="top">top</span>', $result, "Twig didn't correctly return widget.");
$this->assertContains('<span id="top1">top1</span>', $result, "Twig didn't correctly return widget.");
$this->assertContains('<span id="top2">top2</span>', $result, "Twig didn't correctly return widget.");
$this->expectException(RuntimeError::class);

$this->view->render('widgets.twig', ['parameters' => $this->config['runtime']['query_parameters']]);
}

/**
Expand All @@ -166,8 +163,8 @@ public function shouldExtendForWidgetsSuccessfully()
public function shouldRenderWidgetFromAnotherSourcesSuccessfully()
{
$this->view->addGlobal('add_global', '{{Widget_menu_top()}}');
$result = $this->view->render('main.twig', ['app' => ['name' => 'myTwigClassTest']]);
$result = $this->view->render('main.twig', ['app' => ['name' => TwigTest::class]]);

$this->assertContains('<span id="top">top</span></span', $result, "Twig didn't correctly render for widgets from anotherSources.");
$this->assertStringContainsString('<span id="top">top</span></span', $result, "Twig didn't correctly render for widgets from anotherSources.");
}
}
6 changes: 1 addition & 5 deletions test/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
<?php
namespace tests;

require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/common/widgets/Menu.php';
$loader = new \Composer\Autoload\ClassLoader();
$loader->add('test', dirname(__DIR__));
$loader->register();
2 changes: 1 addition & 1 deletion test/common/widgets/Menu.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace TwigTest\Widget;
namespace TwigTests\Widget;

class Menu
{
Expand Down