Skip to content

Commit

Permalink
Merge pull request #21 from tomtomau/phpunit
Browse files Browse the repository at this point in the history
chore(deps): update to PHPUnit 8.3
  • Loading branch information
jrbasso authored Aug 8, 2019
2 parents a4de458 + 4b87bfc commit efec5ce
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 68 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"psr/log": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "4.8.*",
"phpunit/phpunit": "8.3.*",
"squizlabs/php_codesniffer": "3.4.*"
},
"autoload": {
Expand Down
4 changes: 1 addition & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="true"
strict="true"
colors="true">
<testsuites>
<testsuite>
<testsuite name="All tests">
<directory>./test</directory>
</testsuite>
</testsuites>
Expand Down
190 changes: 128 additions & 62 deletions test/AmplitudeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace Zumba\Amplitude\Test;

use \Zumba\Amplitude\Amplitude;
use \Zumba\Amplitude\Event;
use PHPUnit\Framework\TestCase;
use Zumba\Amplitude\Amplitude;
use Zumba\Amplitude\Event;

/**
* @group amplitude
*/
class AmplitudeTest extends \PHPUnit_Framework_TestCase
class AmplitudeTest extends TestCase
{
public function testGetInstance()
{
Expand Down Expand Up @@ -37,43 +38,54 @@ public function testInit()

public function testLogQueuedEvents()
{
$amplitude = $this->getMock('\Zumba\Amplitude\Amplitude', ['logEvent']);
$amplitude = $this->getMockBuilder(Amplitude::class)
->onlyMethods(['logEvent'])
->getMock()
;

$amplitude->expects($this->exactly(3))
->method('logEvent');
->method('logEvent')
;

$amplitude->queueEvent('Event 1')
->queueEvent('Event 2', ['customProp' => 'value'])
->queueEvent('Event 3');
->queueEvent('Event 3')
;

$this->assertTrue($amplitude->hasQueuedEvents(), 'Initialization check, should have queued events');

$amplitude->init('APIKEY', 'USER-ID')
->logQueuedEvents();
->logQueuedEvents()
;

$this->assertFalse($amplitude->hasQueuedEvents(), 'logQueuedEvents should reset the queue afterwards');
}

public function testLogQueuedEventsEmptyQueue()
{
$amplitude = $this->getMock('\Zumba\Amplitude\Amplitude', ['logEvent']);
$amplitude = $this->getMockBuilder(Amplitude::class)
->onlyMethods(['logEvent'])
->getMock()
;

$amplitude->expects($this->never())
->method('logEvent');
->method('logEvent')
;

$this->assertFalse($amplitude->hasQueuedEvents(), 'Initialization check, should not have queued events');

$result = $amplitude->init('APIKEY', 'USER-ID')
->logQueuedEvents();
->logQueuedEvents()
;

$this->assertSame($amplitude, $result, 'Should return itself');
}

public function testEvent()
{
$event = new Event();
$event = new Event();
$amplitude = new Amplitude();
$newEvent = $amplitude->event();
$newEvent = $amplitude->event();
$this->assertNotSame($newEvent, $event, 'Initialization check');
$amplitude->event($event);
$this->assertSame($event, $amplitude->event(), 'Event passed in should persist until it is used or reset');
Expand All @@ -90,17 +102,21 @@ public function testEvent()

public function testLogEvent()
{
$props = ['event property' => 'value'];
$userId = 'USERID';
$deviceId = 'DEVICEID';
$eventType = 'Event Type';
$props = ['event property' => 'value'];
$userId = 'USERID';
$deviceId = 'DEVICEID';
$eventType = 'Event Type';
$secondEventType = 'Second Event';

$amplitude = $this->getMock('\Zumba\Amplitude\Amplitude', ['sendEvent']);
$event = $amplitude->event();
$result = $amplitude->init('APIKEY', $userId)
$amplitude = $this->getMockBuilder(Amplitude::class)
->onlyMethods(['sendEvent'])
->getMock()
;
$event = $amplitude->event();
$result = $amplitude->init('APIKEY', $userId)
->setDeviceId($deviceId)
->logEvent($eventType, $props);
->logEvent($eventType, $props)
;

$eventData = $event->toArray();

Expand Down Expand Up @@ -145,17 +161,21 @@ public function testLogEvent()

public function testLogEventUserPropertiesMerged()
{
$props = ['event property' => 'value'];
$props2 = ['second prop' => 'second val'];
$userId = 'USERID';
$props = ['event property' => 'value'];
$props2 = ['second prop' => 'second val'];
$userId = 'USERID';
$eventType = 'Event Type';

$amplitude = $this->getMock('\Zumba\Amplitude\Amplitude', ['sendEvent']);
$event = $amplitude->event();
$amplitude = $this->getMockBuilder(Amplitude::class)
->onlyMethods(['sendEvent'])
->getMock()
;
$event = $amplitude->event();
$event->userProperties = $props;
$result = $amplitude->init('APIKEY', $userId)
$result = $amplitude->init('APIKEY', $userId)
->setUserProperties($props2)
->logEvent($eventType);
->logEvent($eventType)
;

$eventData = $event->toArray();

Expand All @@ -168,37 +188,49 @@ public function testLogEventUserPropertiesMerged()

public function testLogEventNoApiKey()
{
$amplitude = $this->getMock('\Zumba\Amplitude\Amplitude', ['sendEvent']);
$amplitude = $this->getMockBuilder(Amplitude::class)
->onlyMethods(['sendEvent'])
->getMock()
;

$amplitude->expects($this->never())
->method('sendEvent');
->method('sendEvent')
;

$this->setExpectedException('\LogicException', Amplitude::EXCEPTION_MSG_NO_API_KEY);
$this->expectException(\LogicException::class, Amplitude::EXCEPTION_MSG_NO_API_KEY);
$amplitude->logEvent();
}

public function testLogEventNoEventType()
{
$amplitude = $this->getMock('\Zumba\Amplitude\Amplitude', ['sendEvent']);
$amplitude = $this->getMockBuilder(Amplitude::class)
->onlyMethods(['sendEvent'])
->getMock()
;

$amplitude->expects($this->never())
->method('sendEvent');
->method('sendEvent')
;

$amplitude->init('APIKEY', 'USER');
$this->setExpectedException('\LogicException', Amplitude::EXCEPTION_MSG_NO_EVENT_TYPE);
$this->expectException(\LogicException::class, Amplitude::EXCEPTION_MSG_NO_EVENT_TYPE);
$amplitude->logEvent();
}

public function testLogEventEventInitializedEarly()
{
$amplitude = $this->getMock('\Zumba\Amplitude\Amplitude', ['sendEvent']);
$amplitude = $this->getMockBuilder(Amplitude::class)
->onlyMethods(['sendEvent'])
->getMock()
;

$amplitude->expects($this->once())
->method('sendEvent');
->method('sendEvent')
;

$event = $amplitude->event();
$event = $amplitude->event();
$event->eventType = 'Event Type';
$event->userId = 'USER';
$event->userId = 'USER';

$amplitude->init('APIKEY');
$amplitude->logEvent();
Expand All @@ -208,21 +240,30 @@ public function testLogEventEventInitializedEarly()

public function testLogEventNoUserNoDevice()
{
$amplitude = $this->getMock('\Zumba\Amplitude\Amplitude', ['sendEvent']);
$amplitude = $this->getMockBuilder(Amplitude::class)
->onlyMethods(['sendEvent'])
->getMock()
;

$amplitude->expects($this->never())
->method('sendEvent');
->method('sendEvent')
;

$amplitude->init('APIKEY');
$this->setExpectedException('\LogicException', Amplitude::EXCEPTION_MSG_NO_USER_OR_DEVICE);
$this->expectException(\LogicException::class, Amplitude::EXCEPTION_MSG_NO_USER_OR_DEVICE);
$amplitude->logEvent('Event');
}

public function testQueueEvent()
{
$amplitude = $this->getMock('\Zumba\Amplitude\Amplitude', ['logEvent']);
$amplitude = $this->getMockBuilder(Amplitude::class)
->onlyMethods(['logEvent'])
->getMock()
;

$amplitude->expects($this->never())
->method('logEvent');
->method('logEvent')
;

$event = $amplitude->event();
$amplitude->setUserId('USER');
Expand All @@ -246,13 +287,18 @@ public function testQueueEvent()

public function testQueueEventAlreadyInitRunImmediately()
{
$amplitude = $this->getMock('\Zumba\Amplitude\Amplitude', ['logEvent']);
$amplitude = $this->getMockBuilder(Amplitude::class)
->onlyMethods(['logEvent'])
->getMock()
;
$amplitude->expects($this->once())
->method('logEvent');
->method('logEvent')
;

$this->assertFalse($amplitude->hasQueuedEvents(), 'Initialization check, should not have queue starting out');
$amplitude->init('APIKEY', 'USER')
->queueEvent('Event');
->queueEvent('Event')
;
$this->assertFalse(
$amplitude->hasQueuedEvents(),
'Should have sent event right away since amplitude was already initialized'
Expand All @@ -261,13 +307,17 @@ public function testQueueEventAlreadyInitRunImmediately()

public function testQueueEventInitEarly()
{
$amplitude = $this->getMock('\Zumba\Amplitude\Amplitude', ['logEvent']);
$amplitude = $this->getMockBuilder(Amplitude::class)
->onlyMethods(['logEvent'])
->getMock()
;
$amplitude->expects($this->never())
->method('logEvent');
->method('logEvent')
;

$event = $amplitude->event();
$event = $amplitude->event();
$event->eventType = 'Event';
$result = $amplitude->queueEvent();
$result = $amplitude->queueEvent();
$this->assertTrue(
$amplitude->hasQueuedEvents(),
'Should have queued the event without throwing exception since event type set prior to being queued'
Expand All @@ -278,11 +328,15 @@ public function testQueueEventInitEarly()

public function testQueueEventNoEventType()
{
$amplitude = $this->getMock('\Zumba\Amplitude\Amplitude', ['logEvent']);
$amplitude = $this->getMockBuilder(Amplitude::class)
->onlyMethods(['logEvent'])
->getMock()
;
$amplitude->expects($this->never())
->method('logEvent');
->method('logEvent')
;

$this->setExpectedException('\LogicException', Amplitude::EXCEPTION_MSG_NO_EVENT_TYPE);
$this->expectException(\LogicException::class, Amplitude::EXCEPTION_MSG_NO_EVENT_TYPE);
$amplitude->queueEvent();
}

Expand All @@ -291,7 +345,8 @@ public function testResetUser()
$amplitude = new Amplitude();
$amplitude->setUserId('User')
->setDeviceId('device')
->setUserProperties(['user props']);
->setUserProperties(['user props'])
;
$this->assertNotEmpty($amplitude->getUserId(), 'Initialization check');
$this->assertNotEmpty($amplitude->getDeviceId(), 'Initialization check');
$this->assertNotEmpty($amplitude->getUserProperties(), 'Initialization check');
Expand All @@ -305,9 +360,14 @@ public function testResetUser()

public function testOptOut()
{
$amplitude = $this->getMock('\Zumba\Amplitude\Amplitude', ['sendEvent']);
$amplitude = $this->getMockBuilder(Amplitude::class)
->onlyMethods(['sendEvent'])
->getMock()
;

$amplitude->expects($this->never())
->method('sendEvent');
->method('sendEvent')
;
// Should not end up attempting to send any events no matter how they are logged, either through queue or
// directly
$amplitude->setOptOut(true);
Expand All @@ -316,24 +376,30 @@ public function testOptOut()

$amplitude->init('API', 'USER')
->setOptOut(true)
->logQueuedEvents();
->logQueuedEvents()
;

$amplitude->logEvent('Another Event')
->queueEvent('Another Queued Event');
->queueEvent('Another Queued Event')
;
$this->assertTrue($amplitude->getOptOut());
}

public function testSetUserProperties()
{
$userProps = ['dob' => 'tomorrow', 'gender' => 'f'];
$userProps = ['dob' => 'tomorrow',
'gender' => 'f',
];
$amplitude = new Amplitude();
$amplitude->setUserProperties($userProps);
$this->assertSame($userProps, $amplitude->getUserProperties());
$userProps2 = ['dob' => 'yesterday', 'name' => 'Baby'];
$expected = [
'dob' => 'yesterday',
$userProps2 = ['dob' => 'yesterday',
'name' => 'Baby',
];
$expected = [
'dob' => 'yesterday',
'gender' => 'f',
'name' => 'Baby',
'name' => 'Baby',
];
$amplitude->setUserProperties($userProps2);
$this->assertSame(
Expand Down
Loading

0 comments on commit efec5ce

Please sign in to comment.