Skip to content

Commit

Permalink
test: Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ckrack committed Feb 26, 2024
1 parent 30bafc5 commit 80301c9
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 19 deletions.
2 changes: 1 addition & 1 deletion templates/hydrometer/new_hydrometer.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<div class="example-wrapper">
<h1>New hydrometer added ✅</h1>

Your hydrometer token is: {{ hydrometer_id }}<br>
Your hydrometer token is: <code>{{ hydrometer_id }}</code><br>
<a href="{{ path('app_show_data', {hydrometerId: hydrometer_id}) }}">Show it's data</a>
</div>

Expand Down
93 changes: 93 additions & 0 deletions tests/BrewingFlowFunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace App\Tests;

use App\Entity\Hydrometer;
use App\Repository\HydrometerRepository;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Uid\Ulid;

class BrewingFlowFunctionalTest extends WebTestCase
{
private static KernelBrowser $client;

public static function setUpBeforeClass(): void
{
self::$client = static::createClient();
self::bootKernel();
}

protected function tearDown(): void
{
}

protected function getService($id): ?object

Check failure on line 26 in tests/BrewingFlowFunctionalTest.php

View workflow job for this annotation

GitHub Actions / test

Method App\Tests\BrewingFlowFunctionalTest::getService() has parameter $id with no type specified.
{
return self::getContainer()
->get($id);
}

public function testCanAddHydrometer(): Hydrometer
{
$crawler = self::$client->request('POST', '/new');

$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('h1', 'New hydrometer added ✅');
$this->assertSelectorTextContains('.example-wrapper', 'Your hydrometer token is: ');

$hydrometerId = Ulid::fromString($crawler->filter('.example-wrapper code')->text());

$hydrometerRepository = $this->getService(HydrometerRepository::class);
$hydrometer = $hydrometerRepository->find($hydrometerId);

Check failure on line 43 in tests/BrewingFlowFunctionalTest.php

View workflow job for this annotation

GitHub Actions / test

Cannot call method find() on object|null.
$this->assertInstanceOf(Hydrometer::class, $hydrometer);
$parameterBag = $this->getService(ParameterBagInterface::class);
$dataFilename = $parameterBag->get('kernel.project_dir').'/public/data/'.$hydrometer->getId().'.json';

Check failure on line 46 in tests/BrewingFlowFunctionalTest.php

View workflow job for this annotation

GitHub Actions / test

Cannot call method get() on object|null.
$this->assertFileExists($dataFilename);
$this->assertStringEqualsFile($dataFilename, '[]');

return $hydrometer;
}

/**
* @depends testCanAddHydrometer
*/
public function testShowsEmptyData(Hydrometer $hydrometer)

Check failure on line 56 in tests/BrewingFlowFunctionalTest.php

View workflow job for this annotation

GitHub Actions / test

Method App\Tests\BrewingFlowFunctionalTest::testShowsEmptyData() has no return type specified.
{
$crawler = self::$client->request('GET', '/show/'.$hydrometer->getId());
$this->assertSelectorTextContains('h1', 'Data for '.$hydrometer->getId());

return $hydrometer;
}

/**
* @depends testShowsEmptyData
*/
public function testCanPostData(Hydrometer $hydrometer)

Check failure on line 67 in tests/BrewingFlowFunctionalTest.php

View workflow job for this annotation

GitHub Actions / test

Method App\Tests\BrewingFlowFunctionalTest::testCanPostData() has no return type specified.
{
$data = [
'name' => 'Hydro-Test',
'ID' => '123456',
'angle' => 78.9,
'temperature' => 10.11,
'battery' => 5.43,
'gravity' => 12.34,
'token' => $hydrometer->getId(),
];
self::$client->request(
'POST',
'/data/'.$hydrometer->getId(),
content: json_encode($data)

Check failure on line 81 in tests/BrewingFlowFunctionalTest.php

View workflow job for this annotation

GitHub Actions / test

Parameter $content of method Symfony\Component\BrowserKit\AbstractBrowser::request() expects string|null, string|false given.
);
$this->assertResponseIsSuccessful();
$parameterBag = $this->getService(ParameterBagInterface::class);
$dataFilename = $parameterBag->get('kernel.project_dir').'/public/data/'.$hydrometer->getId().'.json';

Check failure on line 85 in tests/BrewingFlowFunctionalTest.php

View workflow job for this annotation

GitHub Actions / test

Cannot call method get() on object|null.
$jsonFromDataFile = file_get_contents($dataFilename);
$savedData = json_decode($jsonFromDataFile, true, 512, JSON_THROW_ON_ERROR);

Check failure on line 87 in tests/BrewingFlowFunctionalTest.php

View workflow job for this annotation

GitHub Actions / test

Parameter #1 $json of function json_decode expects string, string|false given.
unset($savedData[0]['time']);
$this->assertEquals($data, $savedData[0]);

return $hydrometer;
}
}
36 changes: 36 additions & 0 deletions tests/Command/AddDataCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Tests\Command;

use App\Command\AddDataCommand;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Uid\Ulid;

/**
* @covers \App\Command\AddDataCommand
*
* @uses \Symfony\Component\Uid\Ulid
*/
class AddDataCommandTest extends TestCase
{
private Ulid $ulid;
private AddDataCommand $command;

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

$this->ulid = new Ulid();
$this->command = new AddDataCommand($this->ulid, '{"foo":"bar"}');
}

public function testGetHydrometerId()

Check failure on line 27 in tests/Command/AddDataCommandTest.php

View workflow job for this annotation

GitHub Actions / test

Method App\Tests\Command\AddDataCommandTest::testGetHydrometerId() has no return type specified.
{
$this->assertSame($this->ulid, $this->command->getHydrometerId());
}

public function testGetPayload()

Check failure on line 32 in tests/Command/AddDataCommandTest.php

View workflow job for this annotation

GitHub Actions / test

Method App\Tests\Command\AddDataCommandTest::testGetPayload() has no return type specified.
{
$this->assertSame('{"foo":"bar"}', $this->command->getPayload());
}
}
22 changes: 22 additions & 0 deletions tests/Command/AddHydrometerCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Tests\Command;

use App\Command\AddHydrometerCommand;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Uid\Ulid;

/**
* @covers \App\Command\AddHydrometerCommand
*
* @uses \Symfony\Component\Uid\Ulid
*/
class AddHydrometerCommandTest extends TestCase
{
public function testGetId()
{
$ulid = new Ulid();
$command = new AddHydrometerCommand($ulid);
$this->assertSame($ulid, $command->getId());
}
}
26 changes: 26 additions & 0 deletions tests/Controller/IndexControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class IndexControllerTest extends WebTestCase
{
private static KernelBrowser $client;

public static function setUpBeforeClass(): void
{
self::$client = static::createClient();
self::bootKernel();
}

public function testShowsButtonToAddNewHydrometer(): void
{
self::$client->request('GET', '/');

$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('h1', 'Hello Homebrewer!');
$this->assertSelectorTextContains('button', 'Add new hydrometer');
}
}
18 changes: 0 additions & 18 deletions tests/IndexControllerTest.php

This file was deleted.

0 comments on commit 80301c9

Please sign in to comment.