Skip to content

Commit

Permalink
Setup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jul 4, 2023
1 parent c71d763 commit 6b7d478
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 2 deletions.
8 changes: 6 additions & 2 deletions app/Model/LoggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct(
public function write(string $message): void
{
FileSystem::createDir(dirname($this->file));
file_put_contents($this->file, Json::encode(['date' => time(), 'message' => $message]), FILE_APPEND);
file_put_contents($this->file, Json::encode(['date' => time(), 'message' => $message]) . "\n", FILE_APPEND);
}

/**
Expand All @@ -29,7 +29,11 @@ public function list(): array

$messages = [];
foreach ($lines as $line) {
$messages[] = Json::decode($line);
if (trim($line) === '') {
continue;
}

$messages[] = Json::decode(trim($line), true);
}

return $messages;
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
"App\\": "app"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests"
}
},
"prefer-stable": true,
"minimum-stability": "dev",
"config": {
Expand Down
1 change: 1 addition & 0 deletions ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<properties>
<property name="rootNamespaces" type="array">
<element key="app" value="App"/>
<element key="tests" value="Tests"/>
</property>
</properties>
</rule>
Expand Down
4 changes: 4 additions & 0 deletions tests/.coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# for php-coveralls
service_name: github-actions
coverage_clover: coverage.xml
json_path: coverage.json
10 changes: 10 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Folders - recursive
*.expected
*.actual

# Folders
/tmp

# Files
/*.log
/*.html
57 changes: 57 additions & 0 deletions tests/Cases/E2E/Container/EntrypointTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php declare(strict_types = 1);

namespace Tests\Cases\E2E\Container;

use App\Bootstrap;
use Contributte\Utils\FileSystem;
use Nette\Application\Application as WebApplication;
use Nette\DI\Container;
use Symfony\Component\Console\Application as ConsoleApplication;
use Tester\Assert;
use Tester\TestCase;
use Tests\Toolkit\Tests;

require_once __DIR__ . '/../../../bootstrap.php';

final class EntrypointTest extends TestCase
{

public function setUp(): void
{
parent::setUp();

if (!file_exists(Tests::ROOT_PATH . '/config/local.neon')) {
FileSystem::copy(
Tests::ROOT_PATH . '/config/local.neon.example',
Tests::ROOT_PATH . '/config/local.neon'
);
}
}

public function testWeb(): void
{
$container = Bootstrap::boot()->createContainer();
$container->getByType(WebApplication::class);

Assert::type(Container::class, $container);
}

public function testCli(): void
{
$container = Bootstrap::boot()->createContainer();
$container->getByType(ConsoleApplication::class);

Assert::type(Container::class, $container);
}

public function testTest(): void
{
$container = Bootstrap::boot()->createContainer();
$container->getByType(Container::class);

Assert::type(Container::class, $container);
}

}

(new EntrypointTest())->run();
36 changes: 36 additions & 0 deletions tests/Cases/E2E/Latte/LatteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

namespace Tests\Cases\E2E\Latte;

use App\Bootstrap;
use Contributte\Tester\Toolkit;
use Nette\Bridges\ApplicationLatte\Template;
use Nette\Bridges\ApplicationLatte\TemplateFactory;
use Nette\Utils\Finder;
use SplFileInfo;
use Tester\Assert;
use Tests\Toolkit\Tests;
use Throwable;

require_once __DIR__ . '/../../../bootstrap.php';

Toolkit::test(function (): void {
$container = Bootstrap::boot()->createContainer();

/** @var TemplateFactory $templateFactory */
$templateFactory = $container->getByType(TemplateFactory::class);
Assert::type(TemplateFactory::class, $templateFactory);

/** @var Template $template */
$template = $templateFactory->createTemplate();
$finder = Finder::findFiles('*.latte')->from(Tests::APP_PATH);

try {
/** @var SplFileInfo $file */
foreach ($finder as $file) {
$template->getLatte()->warmupCache($file->getRealPath());
}
} catch (Throwable $e) {
Assert::fail(sprintf('Template compilation failed ([%s] %s)', $e::class, $e->getMessage()));
}
});
19 changes: 19 additions & 0 deletions tests/Cases/Unit/Model/LoggerService.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1);

namespace Tests\Cases\Unit\Model;

use App\Model\LoggerService;
use Contributte\Tester\Environment;
use Contributte\Tester\Toolkit;
use Nette\Utils\Json;
use Tester\Assert;

require_once __DIR__ . '/../../../bootstrap.php';

Toolkit::test(function (): void {
$logfile = Environment::getTestDir() . '/log.txt';
$logger = new LoggerService($logfile);
$logger->write('test');

Assert::match('[{"date":%A%,"message":"test"}]', Json::encode($logger->list()));
});
11 changes: 11 additions & 0 deletions tests/Toolkit/Tests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types = 1);

namespace Tests\Toolkit;

final class Tests
{

public const ROOT_PATH = __DIR__ . '/../..';
public const APP_PATH = __DIR__ . '/../../app';

}
10 changes: 10 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types = 1);

use Contributte\Tester\Environment;

if (@!include __DIR__ . '/../vendor/autoload.php') {
echo 'Install Nette Tester using `composer update --dev`';
exit(1);
}

Environment::setup(__DIR__);

0 comments on commit 6b7d478

Please sign in to comment.