Skip to content

Commit 98f21c1

Browse files
committed
PHPunit test cases added
1 parent a20fe1d commit 98f21c1

11 files changed

+97076
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/log

config.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php
22
define('CLIENT_ID', 'ju16a6m81mhid5ue1z3v2g0uh');
3-
define('ENABLE_LOG', true);
3+
define('ENABLE_LOG', false);
44
define('LOG_FILE_PATH', __DIR__.'/log/supermetricsClient2.log');

phpunit

+96,945
Large diffs are not rendered by default.

phpunit.xml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<phpunit bootstrap="bootstrap.php">
2+
<testsuites>
3+
<testsuite name="Supermetrics">
4+
<directory>tests</directory>
5+
</testsuite>
6+
</testsuites>
7+
</phpunit>

src/lib/Aggregator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private function getWeeklyStats(): array
6464
{
6565
$out = [];
6666
foreach ($this->weekly as $week => $val) {
67-
$out['weelly_stats'][] = [
67+
$out['weekly_stats'][] = [
6868
'week' => $week,
6969
'total_posts' => $val['total_posts'],
7070
];

src/lib/SupermetricsClient.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace supermetrics\lib;
44

55
use Exception;
6+
use InvalidArgumentException;
67
use supermetrics\exception\InvalidTokenException;
78

89
/**
@@ -38,10 +39,13 @@ class SupermetricsClient
3839
/**
3940
* Token registered with the API
4041
*/
41-
private $token = 1;
42+
private $token;
4243

4344
public function __construct(string $email, string $name, $clientId)
4445
{
46+
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
47+
throw new InvalidArgumentException($email . ' is not a valid email address');
48+
}
4549
$this->email = $email;
4650
$this->name = $name;
4751
$this->clientId = $clientId;

summary.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
$email = '[email protected]';
77
$name = 'Sampath';
8-
$numPages = 1;
8+
$numPages = 10;
99

1010
$supermetricsService = new SupermetricsService($email, $name, CLIENT_ID);
1111

summary_report.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"monthly_stats":[{"month":"2021-12","average_character_length_of_post":405.00666666666666,"longest_post":{"id":"post61cbc0e5c2138_36b110e9","from_name":"Rosann Eide","from_id":"user_9","message":"racism manufacture proposal deposit multimedia coincide instrument excitement prescription album accident margin tired option border grip retired chord recognize leaflet television definite depend serve revolution fine deserve provide detective network despise sunshine huge carry blade harmful hell era route horror memorandum reptile fixture nest process humanity federation wagon proposal definition thumb glory morning fill integrity album train danger reveal genuine veil grandmother mild excuse fabricate contract energy division funeral crystal spend effective sanctuary variant climate agreement expertise keep absorb crystal blade syndrome desert helmet establish midnight bathroom pray pray underline reptile pavement angel miscarriage hotdog route opposition carry","type":"status","created_time":"2021-12-09T08:06:22+00:00"},"average_number_of_posts_per_user":7.5},{"month":"2021-11","average_character_length_of_post":398.7843137254902,"longest_post":{"id":"post61cbc0e5c229b_d87b7b1f","from_name":"Regenia Boice","from_id":"user_13","message":"instal find sunshine publisher make midnight bend spontaneous adoption mastermind achievement district letter definition contract litigation design policeman conception intermediate pest bed cave control mug unaware infrastructure cash boat nuance cottage cottage experiment hell leak prescription spontaneous draw habitat aspect broken highway nuance day achievement mild courtship snow referee admit product feature nest underline project control dismissal short circuit coincidence beer glow missile abridge terminal morsel expertise miscarriage attention makeup biology qualified jurisdiction aspect plagiarize fabricate duty angel silver survey dressing habitat buy story biology fist acceptance abstract transmission night speech fuel stand vertical electron effort discourage Europe","type":"status","created_time":"2021-11-25T03:57:29+00:00"},"average_number_of_posts_per_user":7.65},{"month":"2021-10","average_character_length_of_post":407.82456140350877,"longest_post":{"id":"post61cbc0e5c27f3_3fc0612f","from_name":"Carly Alvarez","from_id":"user_6","message":"development appreciate export breakfast precede occasion future straw loose mess vertical carbon heal discourage fight experiment harmful qualified point romantic depression process chief lot circulation makeup graze loose keep bed toss script despise undertake far accountant parachute wood dimension district development climb falsify sunshine proud prevalence pioneer seem lake trench vessel appreciate cage hike pressure trick addition fill prosecute tendency danger federation graze reliance age photocopy size respectable evolution bow veil extraterrestrial pit steward extinct allocation dramatic reptile definite circulation revolution dirty morsel publisher borrow terms situation impulse difficult pavement fashionable duck throne aspect pour fixture village crossing celebration guideline","type":"status","created_time":"2021-10-04T05:36:32+00:00"},"average_number_of_posts_per_user":8.55},{"month":"2021-09","average_character_length_of_post":396.25308641975306,"longest_post":{"id":"post61cbc0e5c297c_32a0d3e9","from_name":"Regenia Boice","from_id":"user_13","message":"discrimination underline reckless food beautiful charm sister stress helmet visual bishop recommendation reduction keep hotdog sensation hole host midnight section fool product division introduce biology resource ankle interest thoughtful impulse referee protection dream elite terrify balance platform pick cigarette peasant appreciate confusion straw difficulty morsel empirical boat company ballet reduction invisible survivor series broken unfair output barrier sister glory scream reconcile berry terms relax monstrous wine thumb permission qualified transmission mold improve timetable bill visual suitcase correspondence dragon tidy referee abridge silver occasion survey shaft grimace air federation referee basket band reliable science mistreat aspect trend distant fling plain correspondence","type":"status","created_time":"2021-09-20T09:24:34+00:00"},"average_number_of_posts_per_user":8.1},{"month":"2021-08","average_character_length_of_post":374.3532934131737,"longest_post":{"id":"post61cbc0e5c2db2_ebb0b1b4","from_name":"Britany Heise","from_id":"user_4","message":"viable information positive debut dynamic noble tick fail timetable export complex inquiry debate AIDS vertical coincidence jurisdiction useful cruelty rehabilitation rape eyebrow greeting delicate rib sanctuary nationalist officer tap sunshine due dragon indoor contraction smell flat dilute thoughtful hike environmental thoughtful fountain reveal cigarette bury deficiency complex pot blade thumb level notice spell virtue spontaneous feature heaven science secretion animal admiration absorb transmission eyebrow loan deposit west calculation opposition lip computer diamond competition theft irony director recording hike abbey hour sunshine morning accountant water horseshoe diamond danger talkative race symbol addition turkey bill sip default fuel hate flow","type":"status","created_time":"2021-08-09T13:54:04+00:00"},"average_number_of_posts_per_user":8.35},{"month":"2021-07","average_character_length_of_post":393.99397590361446,"longest_post":{"id":"post61cbc0e5c312b_0bc175df","from_name":"Carson Smithson","from_id":"user_5","message":"mistreat check excitement superintendent lot pardon convince uncle knit still director relevance crash morsel drag nest lion return stereotype facade bake gutter clearance photocopy coincidence torture effective date consumption sniff instal formal skin outfit donor knit warrant stubborn night flat size suntan surround shake write competition correction merchant retired key hand pest water pursuit stunning marriage beautiful body romantic water belong sister bend think permission condition respectable reconcile snow confront lend brick peasant belief symptom admiration market stunning buy bake rage pain drag spontaneous dynamic painter civilian flavor agreement timetable decorative climb sanctuary reckless chaos master pillow delicate","type":"status","created_time":"2021-07-07T12:26:26+00:00"},"average_number_of_posts_per_user":8.3},{"month":"2021-06","average_character_length_of_post":413.3225806451613,"longest_post":{"id":"post61cbc0e5c31ce_bb855b37","from_name":"Lael Vassel","from_id":"user_0","message":"basket celebration printer section gravel smash ton imperial miscarriage hole borrow favor manufacture extension sensation glory test discrimination digital acceptance tense attention embark torture fly extension consumption aluminium Europe tell memorandum fashionable memorandum banana dominant due speech stake recruit rear rough spine swipe crosswalk cover grand reconcile lend crystal hallway core thin ban confusion still output exact shy noble tap lose glow notice mold empire digital conservative dimension story prevalence menu throne company vegetarian treasurer shy depend old symbol gravel pioneer generation age corn convention virgin meat delay suitcase charm suggest executive overcharge script lip credit card fame magazine","type":"status","created_time":"2021-06-30T17:24:20+00:00"},"average_number_of_posts_per_user":2.2142857142857144}]},{"weekly_stats":[{"week":"2021-52","total_posts":10},{"week":"2021-51","total_posts":38},{"week":"2021-50","total_posts":38},{"week":"2021-49","total_posts":37},{"week":"2021-48","total_posts":36},{"week":"2021-47","total_posts":36},{"week":"2021-46","total_posts":35},{"week":"2021-45","total_posts":35},{"week":"2021-44","total_posts":38},{"week":"2021-43","total_posts":39},{"week":"2021-42","total_posts":40},{"week":"2021-41","total_posts":39},{"week":"2021-40","total_posts":37},{"week":"2021-39","total_posts":38},{"week":"2021-38","total_posts":38},{"week":"2021-37","total_posts":38},{"week":"2021-36","total_posts":37},{"week":"2021-35","total_posts":38},{"week":"2021-34","total_posts":38},{"week":"2021-33","total_posts":39},{"week":"2021-32","total_posts":35},{"week":"2021-31","total_posts":38},{"week":"2021-30","total_posts":37},{"week":"2021-29","total_posts":37},{"week":"2021-28","total_posts":39},{"week":"2021-27","total_posts":37},{"week":"2021-26","total_posts":40},{"week":"2021-25","total_posts":13}]}]

tests/SupermetricsApiTest.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use \supermetrics\lib\SupermetricsApi;
5+
6+
class SupermetricsApiTest extends TestCase
7+
{
8+
public function testCanBeCreatedFromValidInput(): void
9+
{
10+
$this->assertInstanceOf(
11+
SupermetricsApi::class,
12+
new SupermetricsApi('[email protected]', 'Test Name', 123)
13+
);
14+
}
15+
16+
public function testCannotBeCreatedFromInvalidInput(): void
17+
{
18+
$this->expectException(InvalidArgumentException::class);
19+
new SupermetricsApi('userexample.com', 'Test Name', 123);
20+
}
21+
22+
public function testInvalidFetchPostRequest(): void
23+
{
24+
$sma = new SupermetricsApi('[email protected]', 'Test Name', 'xxx');
25+
$this->expectException(Exception::class);
26+
$data = $sma->fetchPost(1);
27+
}
28+
29+
public function testValidFetchPostRequest(): void
30+
{
31+
$sma = new SupermetricsApi('[email protected]', 'Test Name', 'ju16a6m81mhid5ue1z3v2g0uh');
32+
$rsp = $sma->fetchPost(1);
33+
$this->assertIsObject($rsp);
34+
$this->assertObjectHasAttribute('data', $rsp);
35+
$this->assertIsArray($rsp->data->posts);
36+
$this->assertCount(100, $rsp->data->posts);
37+
}
38+
}

tests/SupermetricsClientTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use \supermetrics\lib\SupermetricsClient;
5+
6+
class SupermetricsClientTest extends TestCase
7+
{
8+
public function testCanBeCreatedFromValidInput(): void
9+
{
10+
$this->assertInstanceOf(
11+
SupermetricsClient::class,
12+
new SupermetricsClient('[email protected]', 'Test Name', 123)
13+
);
14+
}
15+
16+
public function testCannotBeCreatedFromInvalidInput(): void
17+
{
18+
$this->expectException(InvalidArgumentException::class);
19+
new SupermetricsClient('userexample.com', 'Test Name', 123);
20+
}
21+
22+
}

tests/supermetricsServiceTest.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use \supermetrics\services\SupermetricsService;
5+
6+
class SupermetricsServiceTest extends TestCase
7+
{
8+
public function testCanBeCreatedFromValidInput(): void
9+
{
10+
$this->assertInstanceOf(
11+
SupermetricsService::class,
12+
new SupermetricsService('[email protected]', 'Test Name', 123)
13+
);
14+
}
15+
16+
public function testCannotBeCreatedFromInvalidInput(): void
17+
{
18+
$this->expectException(InvalidArgumentException::class);
19+
new SupermetricsService('userexample.com', 'Test Name', 123);
20+
}
21+
22+
public function testInvalidFetchPostRequest(): void
23+
{
24+
$sma = new SupermetricsService('[email protected]', 'Test Name', 'xxx');
25+
$this->expectException(Exception::class);
26+
$data = $sma->getWeeklyMonthlySummary(1);
27+
}
28+
29+
public function testValidGetWeeklyMonthlySummary(): void
30+
{
31+
$sma = new SupermetricsService('[email protected]', 'Test Name', 'ju16a6m81mhid5ue1z3v2g0uh');
32+
$rsp = $sma->getWeeklyMonthlySummary(2);
33+
$this->assertIsArray($rsp);
34+
$this->assertCount(2, $rsp);
35+
$this->assertArrayHasKey('monthly_stats', $rsp[0]);
36+
$this->assertCount(2, $rsp[0]['monthly_stats']);
37+
$this->assertArrayHasKey('weekly_stats', $rsp[1]);
38+
$this->assertIsArray($rsp[0]['monthly_stats']);
39+
$this->assertIsArray($rsp[1]['weekly_stats']);
40+
$this->assertArrayHasKey('month', $rsp[0]['monthly_stats'][0]);
41+
$this->assertArrayHasKey('average_character_length_of_post', $rsp[0]['monthly_stats'][0]);
42+
$this->assertArrayHasKey('longest_post', $rsp[0]['monthly_stats'][0]);
43+
$this->assertArrayHasKey('average_number_of_posts_per_user', $rsp[0]['monthly_stats'][0]);
44+
$this->assertArrayHasKey('week', $rsp[1]['weekly_stats'][0]);
45+
$this->assertArrayHasKey('total_posts', $rsp[1]['weekly_stats'][0]);
46+
$cmonth = date('Y-m');
47+
$cweek = date('Y-W');
48+
$this->assertEquals($cmonth, $rsp[0]['monthly_stats'][0]['month']);
49+
$this->assertEquals($cweek, $rsp[1]['weekly_stats'][0]['week']);
50+
$this->assertIsFloat($rsp[0]['monthly_stats'][0]['average_character_length_of_post']);
51+
$this->assertIsFloat($rsp[0]['monthly_stats'][0]['average_number_of_posts_per_user']);
52+
$this->assertIsInt($rsp[1]['weekly_stats'][0]['total_posts']);
53+
}
54+
}

0 commit comments

Comments
 (0)