Skip to content

Commit a62d26e

Browse files
committed
Tests
1 parent 36f822f commit a62d26e

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

src/SparkLine.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ public function getTotal(): int
4141

4242
public function getPeriod(): Period
4343
{
44+
$start = $this->days->first()->day;
45+
$end = $this->days->last()->day;
46+
4447
return Period::make(
45-
new DateTimeImmutable("-{$this->maxItemAmount} days"),
46-
new DateTimeImmutable('now'),
48+
$start,
49+
$end,
4750
);
4851
}
4952

tests/SparkLineTest.php

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use DateTimeImmutable;
1010
use Illuminate\Support\Collection;
1111
use PHPUnit\Framework\TestCase;
12+
use Spatie\Period\Period;
1213

1314
final class SparkLineTest extends TestCase
1415
{
@@ -17,20 +18,72 @@ private function days(): Collection
1718
return collect([
1819
new SparkLineDay(
1920
count: 1,
20-
day: new DateTimeImmutable('-2 days')
21+
day: new DateTimeImmutable('2022-01-01')
2122
),
2223
new SparkLineDay(
2324
count: 2,
24-
day: new DateTimeImmutable('-1 day')
25+
day: new DateTimeImmutable('2022-01-02')
2526
),
2627
]);
2728
}
2829

2930
/** @test */
30-
public function it_creates_a_sparkline(): void
31+
public function test_create_sparkline(): void
3132
{
3233
$sparkLine = SparkLine::new($this->days())->make();
3334

3435
$this->assertStringContainsString('<svg', $sparkLine);
3536
}
37+
38+
/** @test */
39+
public function test_colors(): void
40+
{
41+
$sparkLine = SparkLine::new($this->days())
42+
->withColors('red', 'green', 'blue')
43+
->make();
44+
45+
$this->assertStringContainsString('<stop offset="0%" stop-color="red"></stop>', $sparkLine);
46+
$this->assertStringContainsString('<stop offset="33%" stop-color="green"></stop>', $sparkLine);
47+
$this->assertStringContainsString('<stop offset="66%" stop-color="blue"></stop>', $sparkLine);
48+
}
49+
50+
/** @test */
51+
public function test_stroke_width(): void
52+
{
53+
$sparkLine = SparkLine::new($this->days())
54+
->withStrokeWidth(50)
55+
->make();
56+
57+
$this->assertStringContainsString('stroke-width="50"', $sparkLine);
58+
}
59+
60+
/** @test */
61+
public function test_dimensions(): void
62+
{
63+
$sparkLine = SparkLine::new($this->days())
64+
->withDimensions(500, 501)
65+
->make();
66+
67+
$this->assertStringContainsString('width="500"', $sparkLine);
68+
$this->assertStringContainsString('height="501"', $sparkLine);
69+
}
70+
71+
/** @test */
72+
public function test_get_period(): void
73+
{
74+
$sparkLine = SparkLine::new($this->days());
75+
76+
$this->assertTrue(
77+
Period::fromString('[2022-01-01, 2022-01-02]')
78+
->equals($sparkLine->getPeriod()),
79+
);
80+
}
81+
82+
/** @test */
83+
public function test_get_total(): void
84+
{
85+
$sparkLine = SparkLine::new($this->days());
86+
87+
$this->assertEquals(3, $sparkLine->getTotal());
88+
}
3689
}

0 commit comments

Comments
 (0)