Skip to content

Commit

Permalink
feat(kobo): Display device name to test the param converter
Browse files Browse the repository at this point in the history
  • Loading branch information
ragusa87 committed Dec 9, 2024
1 parent 94dee84 commit cf2b080
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Controller/Kobo/Api/ApiEndpointController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Controller\Kobo\Api;

use App\Controller\Kobo\AbstractKoboController;
use App\Entity\KoboDevice;
use App\Kobo\Proxy\KoboStoreProxy;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
Expand All @@ -16,8 +17,8 @@ public function __construct(
}

#[Route('/', name: 'api_endpoint')]
public function index(): Response
public function index(KoboDevice $koboDevice): Response
{
return new Response('<html><body>Hello Kobo</body></html>');
return new Response(sprintf('<html><body>%s</body></html>', htmlentities('Hello Kobo "'.($koboDevice->getName() ?? $koboDevice->getId()).'"')));
}
}
21 changes: 21 additions & 0 deletions tests/Controller/Kobo/AbstractKoboControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\BrowserKit\AbstractBrowser;
use App\DataFixtures\BookFixture;
use App\Entity\Book;
use App\Entity\KoboDevice;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Profiler\Profile;

abstract class AbstractKoboControllerTest extends WebTestCase
{
Expand Down Expand Up @@ -119,4 +122,22 @@ protected function getMockClient(string $returnValue): ClientInterface
$handlerStack = HandlerStack::create($mock);
return new Client(['handler' => $handlerStack]);
}

/**
* Return the number of doctrine queries
* Make sure to call "$client->enableProfiler();" before making the request
*/
protected function getNumberOfQueries(KernelBrowser $client): int{
$profile = $client->getProfile();
if(!$profile instanceof Profile){
self::markTestSkipped('Profile is not available');
}

$dbCollector = $profile->getCollector('db');
if(!$dbCollector instanceof DoctrineDataCollector){
self::markTestSkipped('DoctrineDataCollector is not available');
}

return $dbCollector->getQueryCount();
}
}
38 changes: 38 additions & 0 deletions tests/Controller/Kobo/ApiEndpointControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Tests\Controller\Kobo;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;

class ApiEndpointControllerTest extends AbstractKoboControllerTest
{
public function testIndex(): void
{
$client = static::getClient();
if(!$client instanceof KernelBrowser){
self::markTestSkipped('Profiler is not available');
}

$this->injectFakeFileSystemManager();

$client->enableProfiler();
$client->request(Request::METHOD_GET, '/kobo/'.$this->accessKey."/");
self::assertResponseIsSuccessful();

// Setup does 2 requests:
// - Select kobo device by accessKey
// - Select Book by id
// -----------------------
// 4 requests are made for the endpoint
// - 1) Select KoboDevice by access key
// - 2) begin transaction
// - 3) Update user's last_login
// - 4) commit transaction
// We check that the number of queries is as expected
self::assertLessThanOrEqual(
2 + 4,
$this->getNumberOfQueries($client)
);
}
}

0 comments on commit cf2b080

Please sign in to comment.