Skip to content

Commit

Permalink
Split tests for different setup
Browse files Browse the repository at this point in the history
  • Loading branch information
giorgiopogliani committed Jun 16, 2022
1 parent 7b3e381 commit 4bdeef8
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 43 deletions.
2 changes: 2 additions & 0 deletions src/Node/ComponentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ protected function addTemplateArguments(Compiler $compiler)

if ($this->configuration->isUsingGlobalContext()) {
$compiler->write('$context,[');
} else {
$compiler->write('[');
}

$compiler->write("'slot' => new " . ComponentSlot::class . " (\$slot),\n")
Expand Down
44 changes: 1 addition & 43 deletions tests/ComponentTest.php → tests/ComponentsTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,8 @@

namespace Performing\TwigComponents\Tests;

use Performing\TwigComponents\Configuration;
use PHPUnit\Framework\TestCase;

class ComponentTest extends TestCase
Trait ComponentsTestTrait
{
protected $twig;

protected function setupTwig(): \Twig\Environment
{
$loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/templates');

$loader->addPath(__DIR__ . '/namespace-templates', 'ns');

$twig = new \Twig\Environment($loader, [
'cache' => false, //__DIR__ . '/../cache',
]);

Configuration::make($twig)
->setTemplatesPath('components')
->setTemplatesExtension('twig')
->useGlobalContext()
->useCustomTags()
->setup();

return $twig;
}

public function setUp(): void
{
$this->twig = $this->setupTwig();
}

/** @test */
public function render_simple_component()
{
Expand Down Expand Up @@ -153,18 +123,6 @@ public function render_namespaced_xtags_component()
HTML, $html);
}

/** @test */
public function share_global_context_inside_components()
{
$template = $this->twig->createTemplate(<<<HTML
{% set foo = 'bar' %}
<x-global_context></x-global_context>
HTML);
$html = $template->render();

$this->assertEquals('bar', $html);
}

/** @test */
public function test_class_merge_works_with_components_in_components()
{
Expand Down
50 changes: 50 additions & 0 deletions tests/GlobalContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Performing\TwigComponents\Tests;

use Performing\TwigComponents\Configuration;
use PHPUnit\Framework\TestCase;

class GlobalContextTest extends TestCase
{
use ComponentsTestTrait;

protected $twig;

protected function setupTwig(): \Twig\Environment
{
$loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/templates');

$loader->addPath(__DIR__ . '/namespace-templates', 'ns');

$twig = new \Twig\Environment($loader, [
'cache' => false, //__DIR__ . '/../cache',
]);

Configuration::make($twig)
->setTemplatesPath('components')
->setTemplatesExtension('twig')
->useGlobalContext()
->useCustomTags()
->setup();

return $twig;
}

public function setUp(): void
{
$this->twig = $this->setupTwig();
}

/** @test */
public function share_global_context_inside_components()
{
$template = $this->twig->createTemplate(<<<HTML
{% set foo = 'bar' %}
<x-global_context></x-global_context>
HTML);
$html = $template->render();

$this->assertEquals('bar', $html);
}
}
37 changes: 37 additions & 0 deletions tests/WithoutGlobalContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Performing\TwigComponents\Tests;

use Performing\TwigComponents\Configuration;
use PHPUnit\Framework\TestCase;

class WithoutGlobalContextTest extends TestCase
{
use ComponentsTestTrait;

protected $twig;

protected function setupTwig(): \Twig\Environment
{
$loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/templates');

$loader->addPath(__DIR__ . '/namespace-templates', 'ns');

$twig = new \Twig\Environment($loader, [
'cache' => false, //__DIR__ . '/../cache',
]);

Configuration::make($twig)
->setTemplatesPath('components')
->setTemplatesExtension('twig')
->useCustomTags()
->setup();

return $twig;
}

public function setUp(): void
{
$this->twig = $this->setupTwig();
}
}

0 comments on commit 4bdeef8

Please sign in to comment.