Skip to content

Commit ae93c93

Browse files
committed
Add tests for immutable Location
1 parent 6a77693 commit ae93c93

1 file changed

Lines changed: 147 additions & 0 deletions

File tree

tests/LocationTest.php

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace InteractionDesignFoundation\GeoIP\Tests;
6+
7+
use InteractionDesignFoundation\GeoIP\Location;
8+
use PHPUnit\Framework\Attributes\CoversClass;
9+
use PHPUnit\Framework\Attributes\Test;
10+
11+
#[CoversClass(Location::class)]
12+
class LocationTest extends TestCase
13+
{
14+
#[Test]
15+
public function with_attribute_returns_new_instance_and_original_is_unchanged(): void
16+
{
17+
$original = new Location([
18+
'ip' => '81.2.69.142',
19+
'iso_code' => 'US',
20+
]);
21+
22+
$modified = $original->withAttribute('iso_code', 'DE');
23+
24+
$this->assertNotSame($original, $modified);
25+
$this->assertSame('US', $original->iso_code);
26+
$this->assertSame('DE', $modified->iso_code);
27+
$this->assertSame('81.2.69.142', $modified->ip);
28+
}
29+
30+
#[Test]
31+
public function offset_set_throws_bad_method_call_exception(): void
32+
{
33+
$location = new Location(['ip' => '127.0.0.1']);
34+
35+
$this->expectException(\BadMethodCallException::class);
36+
37+
$location['ip'] = '10.0.0.1';
38+
}
39+
40+
#[Test]
41+
public function offset_unset_throws_bad_method_call_exception(): void
42+
{
43+
$location = new Location(['ip' => '127.0.0.1']);
44+
45+
$this->expectException(\BadMethodCallException::class);
46+
47+
unset($location['ip']);
48+
}
49+
50+
#[Test]
51+
public function constructor_sets_default_and_cached_to_false(): void
52+
{
53+
$location = new Location(['ip' => '127.0.0.1']);
54+
55+
$this->assertFalse($location->default);
56+
$this->assertFalse($location->cached);
57+
}
58+
59+
#[Test]
60+
public function constructor_preserves_provided_default_and_cached_values(): void
61+
{
62+
$location = new Location([
63+
'ip' => '127.0.0.1',
64+
'default' => true,
65+
'cached' => true,
66+
]);
67+
68+
$this->assertTrue($location->default);
69+
$this->assertTrue($location->cached);
70+
}
71+
72+
#[Test]
73+
public function reading_attributes_via_magic_get(): void
74+
{
75+
$location = new Location([
76+
'ip' => '81.2.69.142',
77+
'iso_code' => 'US',
78+
'country' => 'United States',
79+
'city' => 'New Haven',
80+
'state' => 'CT',
81+
'lat' => 41.31,
82+
'lon' => -72.92,
83+
]);
84+
85+
$this->assertSame('81.2.69.142', $location->ip);
86+
$this->assertSame('US', $location->iso_code);
87+
$this->assertSame('United States', $location->country);
88+
$this->assertSame('New Haven', $location->city);
89+
$this->assertSame('CT', $location->state);
90+
$this->assertSame(41.31, $location->lat);
91+
$this->assertSame(-72.92, $location->lon);
92+
}
93+
94+
#[Test]
95+
public function reading_attributes_via_array_access(): void
96+
{
97+
$location = new Location([
98+
'ip' => '81.2.69.142',
99+
'iso_code' => 'US',
100+
]);
101+
102+
$this->assertSame('81.2.69.142', $location['ip']);
103+
$this->assertSame('US', $location['iso_code']);
104+
}
105+
106+
#[Test]
107+
public function isset_returns_true_for_existing_attributes(): void
108+
{
109+
$location = new Location([
110+
'ip' => '81.2.69.142',
111+
'iso_code' => 'US',
112+
]);
113+
114+
$this->assertTrue(isset($location->ip));
115+
$this->assertTrue(isset($location->iso_code));
116+
$this->assertTrue(isset($location->default));
117+
$this->assertFalse(isset($location->nonexistent));
118+
}
119+
120+
#[Test]
121+
public function to_array_returns_all_attributes(): void
122+
{
123+
$attributes = [
124+
'ip' => '81.2.69.142',
125+
'iso_code' => 'US',
126+
'country' => 'United States',
127+
];
128+
129+
$location = new Location($attributes);
130+
$result = $location->toArray();
131+
132+
$this->assertSame('81.2.69.142', $result['ip']);
133+
$this->assertSame('US', $result['iso_code']);
134+
$this->assertSame('United States', $result['country']);
135+
$this->assertFalse($result['default']);
136+
$this->assertFalse($result['cached']);
137+
}
138+
139+
#[Test]
140+
public function same_returns_true_for_matching_ip(): void
141+
{
142+
$location = new Location(['ip' => '81.2.69.142']);
143+
144+
$this->assertTrue($location->same('81.2.69.142'));
145+
$this->assertFalse($location->same('10.0.0.1'));
146+
}
147+
}

0 commit comments

Comments
 (0)