Skip to content

Commit

Permalink
Enhancement: Adds StoryblokCollectorTest (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
silasjoisten authored Sep 28, 2024
1 parent 69af105 commit 0f6474d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/DataCollector/StoryblokCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function lateCollect(): void
*/
public function getTraces(): array
{
return $this->data['traces'];
return $this->data['traces'] ?? [];
}

public function getRequestCount(): int
Expand Down
93 changes: 93 additions & 0 deletions tests/Unit/DataCollector/StoryblokCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

/**
* This file is part of sensiolabs-de/storyblok-bundle.
*
* (c) SensioLabs Deutschland <info@sensiolabs.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SensioLabs\Storyblok\Bundle\Tests\Unit\DataCollector;

use PHPUnit\Framework\TestCase;
use SensioLabs\Storyblok\Bundle\DataCollector\StoryblokCollector;
use SensioLabs\Storyblok\Bundle\Tests\Util\FakerTrait;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\JsonMockResponse;
use Symfony\Component\HttpClient\TraceableHttpClient;

final class StoryblokCollectorTest extends TestCase
{
use FakerTrait;

/**
* @test
*/
public function defaults(): void
{
$client = new TraceableHttpClient(new MockHttpClient());
$collector = new StoryblokCollector($client);

self::assertEmpty($collector->getTraces());
self::assertSame(0, $collector->getRequestCount());
self::assertSame(0, $collector->getErrorCount());
}

/**
* @test
*/
public function getTemplate(): void
{
self::assertSame('@Storyblok/data_collector.html.twig', StoryblokCollector::getTemplate());
}

/**
* @test
*/
public function lateCollect(): void
{
$client = new TraceableHttpClient(new MockHttpClient([
new JsonMockResponse(['hello' => 'there'], ['http_code' => 200]),
]));

$client->request('GET', 'https://example.com');

$collector = new StoryblokCollector($client);

$collector->lateCollect();

self::assertCount(1, $collector->getTraces());
self::assertSame(1, $collector->getRequestCount());
self::assertSame(0, $collector->getErrorCount());
}

/**
* @test
*/
public function reset(): void
{
$client = new TraceableHttpClient(new MockHttpClient([
new JsonMockResponse(['hello' => 'there'], ['http_code' => 200]),
]));

$client->request('GET', 'https://example.com');

$collector = new StoryblokCollector($client);

$collector->lateCollect();

self::assertCount(1, $collector->getTraces());
self::assertSame(1, $collector->getRequestCount());
self::assertSame(0, $collector->getErrorCount());

$collector->reset();

self::assertEmpty($collector->getTraces());
self::assertSame(0, $collector->getRequestCount());
self::assertSame(0, $collector->getErrorCount());
}
}

0 comments on commit 0f6474d

Please sign in to comment.