Skip to content

Commit

Permalink
Fix: Property can be null (#226)
Browse files Browse the repository at this point in the history
* Enhancement: Assert that Segment throws SegmentException without initialization

* Fix: Property can be null
  • Loading branch information
localheinz authored Aug 16, 2023
1 parent 2ab7c0f commit dd47f60
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Segment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Segment
{
private static Client $client;
private static ?Client $client = null;

/**
* Initializes the default client to use. Uses the libcurl consumer by default.
Expand Down
78 changes: 78 additions & 0 deletions test/SegmentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Segment\Test;

use PHPUnit\Framework;
use Segment\Segment;
use Segment\SegmentException;

final class SegmentTest extends Framework\TestCase
{
protected function setUp(): void
{
self::resetSegment();
}

public function testAliasThrowsSegmentExceptionWhenClientHasNotBeenInitialized(): void
{
$this->expectException(SegmentException::class);
$this->expectExceptionMessage('Segment::init() must be called before any other tracking method.');

Segment::alias([]);
}
public function testFlushThrowsSegmentExceptionWhenClientHasNotBeenInitialized(): void
{
$this->expectException(SegmentException::class);
$this->expectExceptionMessage('Segment::init() must be called before any other tracking method.');

Segment::flush();
}
public function testGroupThrowsSegmentExceptionWhenClientHasNotBeenInitialized(): void
{
$this->expectException(SegmentException::class);
$this->expectExceptionMessage('Segment::init() must be called before any other tracking method.');

Segment::group([]);
}
public function testIdentifyThrowsSegmentExceptionWhenClientHasNotBeenInitialized(): void
{
$this->expectException(SegmentException::class);
$this->expectExceptionMessage('Segment::init() must be called before any other tracking method.');

Segment::identify([]);
}
public function testPageThrowsSegmentExceptionWhenClientHasNotBeenInitialized(): void
{
$this->expectException(SegmentException::class);
$this->expectExceptionMessage('Segment::init() must be called before any other tracking method.');

Segment::page([]);
}
public function testScreenThrowsSegmentExceptionWhenClientHasNotBeenInitialized(): void
{
$this->expectException(SegmentException::class);
$this->expectExceptionMessage('Segment::init() must be called before any other tracking method.');

Segment::screen([]);
}
public function testTrackThrowsSegmentExceptionWhenClientHasNotBeenInitialized(): void
{
$this->expectException(SegmentException::class);
$this->expectExceptionMessage('Segment::init() must be called before any other tracking method.');

Segment::track([]);
}

private static function resetSegment(): void
{
$property = new \ReflectionProperty(
Segment::class,
'client'
);

$property->setAccessible(true);
$property->setValue(null);
}
}

0 comments on commit dd47f60

Please sign in to comment.