Skip to content

Commit c622fcb

Browse files
committed
Add tests
1 parent 4d9282e commit c622fcb

14 files changed

Lines changed: 769 additions & 5 deletions

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor/

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ composer require your-vendor/thomasnet-php
1010

1111
## Requirements
1212

13-
- PHP 8.1+
13+
- PHP 8.3+
1414
- Apify API token ([get one here](https://console.apify.com/account/integrations))
1515

1616
## Quick Start

‎composer.json‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
"type": "library",
55
"license": "MIT",
66
"require": {
7-
"php": "^8.1",
7+
"php": "^8.3",
88
"guzzlehttp/guzzle": "^7.0"
99
},
1010
"require-dev": {
11-
"phpunit/phpunit": "^10.0",
11+
"pestphp/pest": "^4.0",
1212
"friendsofphp/php-cs-fixer": "^3.0",
1313
"phpstan/phpstan": "^1.0"
1414
},
@@ -23,12 +23,15 @@
2323
}
2424
},
2525
"scripts": {
26-
"test": "phpunit",
26+
"test": "pest",
2727
"cs": "php-cs-fixer fix --dry-run --diff",
2828
"cs:fix": "php-cs-fixer fix",
2929
"analyse": "phpstan analyse"
3030
},
3131
"config": {
32-
"sort-packages": true
32+
"sort-packages": true,
33+
"allow-plugins": {
34+
"pestphp/pest-plugin": true
35+
}
3336
}
3437
}

‎phpunit.xml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true">
6+
<testsuites>
7+
<testsuite name="Tests">
8+
<directory>tests</directory>
9+
</testsuite>
10+
</testsuites>
11+
<source>
12+
<include>
13+
<directory>src</directory>
14+
</include>
15+
</source>
16+
</phpunit>

‎tests/AddressTest.php‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use ThomasNetScraper\DTO\Address;
6+
7+
it('creates address from complete data', function () {
8+
$data = [
9+
'address1' => '300 New Jersey Avenue, N.W.',
10+
'address2' => 'Suite 1000',
11+
'city' => 'Washington',
12+
'state' => 'DC',
13+
'stateName' => 'District of Columbia',
14+
'zip' => '20001',
15+
'country' => 'USA',
16+
'latitude' => 38.91076058,
17+
'longitude' => -77.0167462,
18+
];
19+
20+
$address = Address::fromArray($data);
21+
22+
expect($address->address1)->toBe('300 New Jersey Avenue, N.W.')
23+
->and($address->address2)->toBe('Suite 1000')
24+
->and($address->city)->toBe('Washington')
25+
->and($address->state)->toBe('DC')
26+
->and($address->stateName)->toBe('District of Columbia')
27+
->and($address->zip)->toBe('20001')
28+
->and($address->country)->toBe('USA')
29+
->and($address->latitude)->toBe(38.91076058)
30+
->and($address->longitude)->toBe(-77.0167462);
31+
});
32+
33+
it('handles missing optional fields', function () {
34+
$address = Address::fromArray([
35+
'city' => 'Austin',
36+
'state' => 'TX',
37+
]);
38+
39+
expect($address->city)->toBe('Austin')
40+
->and($address->state)->toBe('TX')
41+
->and($address->address1)->toBeNull()
42+
->and($address->address2)->toBeNull()
43+
->and($address->zip)->toBeNull()
44+
->and($address->latitude)->toBeNull()
45+
->and($address->longitude)->toBeNull();
46+
});
47+
48+
it('creates empty address from empty array', function () {
49+
$address = Address::fromArray([]);
50+
51+
expect($address->address1)->toBeNull()
52+
->and($address->city)->toBeNull()
53+
->and($address->state)->toBeNull()
54+
->and($address->country)->toBeNull();
55+
});
56+
57+
it('casts coordinates to float', function () {
58+
$address = Address::fromArray([
59+
'latitude' => '38.91076058',
60+
'longitude' => '-77.0167462',
61+
]);
62+
63+
expect($address->latitude)->toBeFloat()
64+
->and($address->longitude)->toBeFloat();
65+
});

‎tests/CertificationTest.php‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use ThomasNetScraper\DTO\Certification;
6+
7+
it('creates certification from complete data', function () {
8+
$data = [
9+
'id' => 408554,
10+
'code' => '175',
11+
'title' => 'AS9100D',
12+
'type' => 'QUALITY',
13+
'tier' => 'DOWNLOAD',
14+
'group' => 'AS9100',
15+
'scope' => 'For the control products and systems (BT CPS) business unit...',
16+
'imageUrl' => 'https://cdn.thomasnet.com/certifications/large/40/408554.png',
17+
'thumbnailUrl' => 'https://cdn.thomasnet.com/certifications/thumbs/40/408554.png',
18+
'url' => 'https://www.downloads.siemens.com/download-center/...',
19+
'date' => '03/05/2018',
20+
'isActive' => false,
21+
];
22+
23+
$cert = Certification::fromArray($data);
24+
25+
expect($cert->id)->toBe(408554)
26+
->and($cert->code)->toBe('175')
27+
->and($cert->title)->toBe('AS9100D')
28+
->and($cert->type)->toBe('QUALITY')
29+
->and($cert->tier)->toBe('DOWNLOAD')
30+
->and($cert->group)->toBe('AS9100')
31+
->and($cert->scope)->toContain('control products')
32+
->and($cert->imageUrl)->toContain('408554.png')
33+
->and($cert->date)->toBe('03/05/2018')
34+
->and($cert->isActive)->toBeFalse();
35+
});
36+
37+
it('handles ISO 9001 certification', function () {
38+
$data = [
39+
'id' => 389564,
40+
'code' => '181',
41+
'title' => 'ISO 9001:2015',
42+
'type' => 'QUALITY',
43+
'group' => 'ISO 9001',
44+
'isActive' => true,
45+
];
46+
47+
$cert = Certification::fromArray($data);
48+
49+
expect($cert->title)->toBe('ISO 9001:2015')
50+
->and($cert->group)->toBe('ISO 9001')
51+
->and($cert->isActive)->toBeTrue();
52+
});
53+
54+
it('handles missing optional fields', function () {
55+
$data = [
56+
'id' => 123,
57+
'code' => 'TEST',
58+
'title' => 'Test Certification',
59+
'type' => 'DIVERSITY',
60+
];
61+
62+
$cert = Certification::fromArray($data);
63+
64+
expect($cert->id)->toBe(123)
65+
->and($cert->tier)->toBeNull()
66+
->and($cert->group)->toBeNull()
67+
->and($cert->scope)->toBeNull()
68+
->and($cert->imageUrl)->toBeNull()
69+
->and($cert->url)->toBeNull()
70+
->and($cert->date)->toBeNull()
71+
->and($cert->isActive)->toBeFalse();
72+
});
73+
74+
it('casts id to integer', function () {
75+
$data = [
76+
'id' => '408554',
77+
'code' => '175',
78+
'title' => 'AS9100D',
79+
'type' => 'QUALITY',
80+
];
81+
82+
$cert = Certification::fromArray($data);
83+
84+
expect($cert->id)->toBeInt()->toBe(408554);
85+
});

‎tests/CertificationTotalTest.php‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use ThomasNetScraper\DTO\CertificationTotal;
6+
7+
it('creates certification total from array', function () {
8+
$data = [
9+
'type' => 'QUALITY',
10+
'count' => 185,
11+
];
12+
13+
$total = CertificationTotal::fromArray($data);
14+
15+
expect($total->type)->toBe('QUALITY')
16+
->and($total->count)->toBe(185);
17+
});
18+
19+
it('handles registration type', function () {
20+
$data = [
21+
'type' => 'REGISTRATION',
22+
'count' => 47,
23+
];
24+
25+
$total = CertificationTotal::fromArray($data);
26+
27+
expect($total->type)->toBe('REGISTRATION')
28+
->and($total->count)->toBe(47);
29+
});
30+
31+
it('casts count to integer', function () {
32+
$data = [
33+
'type' => 'DIVERSITY',
34+
'count' => '12',
35+
];
36+
37+
$total = CertificationTotal::fromArray($data);
38+
39+
expect($total->count)->toBeInt()->toBe(12);
40+
});

‎tests/EnumTest.php‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use ThomasNetScraper\SearchMode;
6+
use ThomasNetScraper\Area;
7+
8+
it('has correct search mode values', function () {
9+
expect(SearchMode::All->value)->toBe('all')
10+
->and(SearchMode::Name->value)->toBe('name');
11+
});
12+
13+
it('has correct area codes for major regions', function () {
14+
expect(Area::All->value)->toBe('NA')
15+
->and(Area::TexasNorth->value)->toBe('NT')
16+
->and(Area::TexasSouth->value)->toBe('GT')
17+
->and(Area::CaliforniaNorth->value)->toBe('CN')
18+
->and(Area::CaliforniaSouth->value)->toBe('CS')
19+
->and(Area::NewYorkMetro->value)->toBe('DN')
20+
->and(Area::NewYorkUpstate->value)->toBe('UN')
21+
->and(Area::Michigan->value)->toBe('MI')
22+
->and(Area::Illinois->value)->toBe('IL');
23+
});
24+
25+
it('has correct area codes for Ohio and Pennsylvania regions', function () {
26+
expect(Area::OhioNorth->value)->toBe('NO')
27+
->and(Area::OhioSouth->value)->toBe('SO')
28+
->and(Area::PennsylvaniaEast->value)->toBe('EP')
29+
->and(Area::PennsylvaniaWest->value)->toBe('WP');
30+
});
31+
32+
it('has correct area codes for Canadian provinces', function () {
33+
expect(Area::Ontario->value)->toBe('ON')
34+
->and(Area::Quebec->value)->toBe('QC')
35+
->and(Area::Alberta->value)->toBe('AB')
36+
->and(Area::BritishColumbia->value)->toBe('BC');
37+
});
38+
39+
it('can create search mode from string value', function () {
40+
expect(SearchMode::from('all'))->toBe(SearchMode::All)
41+
->and(SearchMode::from('name'))->toBe(SearchMode::Name);
42+
});
43+
44+
it('can create area from string value', function () {
45+
expect(Area::from('MI'))->toBe(Area::Michigan)
46+
->and(Area::from('NT'))->toBe(Area::TexasNorth)
47+
->and(Area::from('ON'))->toBe(Area::Ontario);
48+
});

‎tests/HeadingTest.php‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use ThomasNetScraper\DTO\Heading;
6+
7+
it('creates heading from complete data', function () {
8+
$data = [
9+
'headingId' => '171801',
10+
'name' => 'Accelerators: Linear',
11+
'familyName' => 'Accelerators',
12+
'description' => 'Worldwide manufacturer of digital linear accelerators for radiation therapy.',
13+
'url' => 'https://example.com/accelerators',
14+
];
15+
16+
$heading = Heading::fromArray($data);
17+
18+
expect($heading->headingId)->toBe('171801')
19+
->and($heading->name)->toBe('Accelerators: Linear')
20+
->and($heading->familyName)->toBe('Accelerators')
21+
->and($heading->description)->toContain('linear accelerators')
22+
->and($heading->url)->toBe('https://example.com/accelerators');
23+
});
24+
25+
it('handles missing optional fields', function () {
26+
$data = [
27+
'headingId' => '39774245',
28+
'name' => 'Access Control Systems',
29+
];
30+
31+
$heading = Heading::fromArray($data);
32+
33+
expect($heading->headingId)->toBe('39774245')
34+
->and($heading->name)->toBe('Access Control Systems')
35+
->and($heading->familyName)->toBeNull()
36+
->and($heading->description)->toBeNull()
37+
->and($heading->url)->toBeNull();
38+
});
39+
40+
it('casts headingId to string', function () {
41+
$data = [
42+
'headingId' => 171801,
43+
'name' => 'Test Heading',
44+
];
45+
46+
$heading = Heading::fromArray($data);
47+
48+
expect($heading->headingId)->toBeString()->toBe('171801');
49+
});

0 commit comments

Comments
 (0)