Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Dec 12, 2023
1 parent bfabef3 commit 94324a2
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 226 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ jobs:
laravel: ^6.0
- php: 8.1
laravel: ^7.0
- php: 8.2
laravel: ^7.0
- php: 8.2
laravel: ^6.0

name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}

Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"laravel/framework":"~5.1|6.*|7.*|8.*|9.*|10.*"
},
"autoload-dev": {
"classmap": ["tests/TestCase.php"]
"psr-4": {
"Tests\\": "tests"
}
},
"require-dev": {
"orchestra/testbench": "~3.0|~4.0|~5.0|~6.0|~7.0|~8.0",
Expand Down
59 changes: 22 additions & 37 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,40 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
beStrictAboutTestsThatDoNotTestAnything="false"
stopOnError="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>

<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
</php>
<logging>
<log type="coverage-html" target="./log/codeCoverage" charset="UTF-8"
yui="true" highlight="true"
lowUpperBound="50" highLowerBound="80"/>
<log type="testdox-html" target="./log/testdox.html" />
</logging>
<filter>

<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src/Utils</directory>
</whitelist>

<blacklist>
<directory>./src/vendor</directory>
</blacklist>
</filter>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
</php>
</phpunit>
4 changes: 3 additions & 1 deletion src/Utils/WidgetJsonifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public function jsonResponse($widget, ...$args)
*/
private function makeWidgetObj(string $widget)
{
$widget = app()->getNamespace().'Widgets\\'.$widget;
if ($widget[0] !== '\\') {
$widget = app()->getNamespace().'Widgets\\'.$widget;
}

return app($widget);
}
Expand Down
30 changes: 25 additions & 5 deletions tests/SlotWidgetTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace Tests;

use Illuminate\Support\Facades\View;
use Imanghafoori\Widgets\Utils\WidgetRenderer;

require_once 'test_stubs.php';

class SlotWidgetTest extends TestCase
{
Expand Down Expand Up @@ -76,8 +77,8 @@ public function test_widget_with_a_slot()
ob_end_clean();

//act
$widget = new Widget4();
$widget->controller = 'Widget4Ctrl@meta';
$widget = new Widget4Slot();
$widget->controller = 'Tests\Widget764Ctrl@meta';
$renderer->renderWidget($widget, ['arg1' => 'a', 'arg2' => 'bb']);
}

Expand All @@ -103,8 +104,27 @@ public function test_widget_with_many_slots()
}

//act
$widget = new Widget4();
$widget->controller = 'Widget4Ctrl@meta';
$widget = new Widget4Slot();
$widget->controller = 'Tests\Widget764Ctrl@meta';
$renderer->renderWidget($widget, ['arg1' => 'a', 'arg2' => 'bb']);
}
}

class Widget4Slot
{
public $template = 'hello';
public $controller = 'Widget764Ctrl';
}

class Widget764Ctrl
{
public function data($arg1, $arg2)
{
return $arg1.$arg2;
}

public function meta($arg1, $arg2)
{
return $arg2.$arg1.$arg1;
}
}
4 changes: 3 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

abstract class TestCase extends Orchestra\Testbench\TestCase
namespace Tests;

class TestCase extends \Orchestra\Testbench\TestCase
{
protected function getPackageProviders($app)
{
Expand Down
87 changes: 83 additions & 4 deletions tests/WidgetCacheTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

require_once 'test_stubs.php';
namespace Tests;

use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Cache;


class WidgetCacheTest extends TestCase
{
Expand All @@ -18,7 +22,7 @@ public function test_the_view_and_controller_are_rendered_only_once_when_cache_i
\App::shouldReceive('call')->once()->andReturn('foo');

//act
$widget = new Widget1();
$widget = new Widget123();
$result1 = render_widget($widget);
$result2 = render_widget($widget);
$result3 = render_widget($widget);
Expand Down Expand Up @@ -67,7 +71,7 @@ public function test_caches_the_result_of_controller_method_and_views()
View::shouldReceive('exists')->once()->andReturn(true);

//act
$widget = new Widget1();
$widget = new Widget123();
$result1 = render_widget($widget);
$result2 = render_widget($widget);
$result3 = render_widget($widget);
Expand Down Expand Up @@ -196,7 +200,7 @@ public function test_the_view_and_controller_are_rendered_only_once_when_cache_i
\App::shouldReceive('call')->times(5)->andReturn('foo');

//act
$widget = new Widget1();
$widget = new Widget123();
$result1 = render_widget($widget);
$result2 = render_widget($widget);
$widget->cacheView = false;
Expand All @@ -210,3 +214,78 @@ public function test_the_view_and_controller_are_rendered_only_once_when_cache_i
$this->assertEquals($widget->cacheLifeTime->s, 1);
}
}

class ForeverWidget
{
public $cacheLifeTime = -1;
public $template = 'hello';

public function data()
{
}
}

class ForeverWidget2
{
public $cacheLifeTime = 'forever';
public $template = 'hello';

public function data()
{
}
}

class CustomCacheKeyWidget
{
public $cacheLifeTime = -1;
public $template = 'hello';

public function data()
{
}

public function cacheKey()
{
return 'abcde';
}
}

class TaggedWidget
{
public $template = 'hello';
public $cacheTags = ['t1', 't2'];

public function data()
{
}
}

class Widget123
{
public $template = 'hello';

public function data()
{
}
}

class Widget8
{
public $template = 'hello';

public $cacheView = false;

public function data()
{
}
}

class ZeroLifeTimeWidget
{
public $cacheLifeTime = 0;
public $template = 'hello';

public function data()
{
}
}
15 changes: 14 additions & 1 deletion tests/WidgetMinificationTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

require_once 'test_stubs.php';
namespace Tests;

use Illuminate\Support\Facades\View;


class WidgetMinificationTest extends TestCase
{
Expand Down Expand Up @@ -83,3 +86,13 @@ public function test_minifies_the_output_in_production_with_cache_turned_off()
$this->assertEquals($minified, $html);
}
}


class Widget7
{
public $template = 'hello';

public function data()
{
}
}
Loading

0 comments on commit 94324a2

Please sign in to comment.