Skip to content

Commit

Permalink
chore(tests): improve purchase event test
Browse files Browse the repository at this point in the history
  • Loading branch information
imdhemy committed Feb 11, 2023
1 parent 0072877 commit 6c3269f
Showing 1 changed file with 34 additions and 42 deletions.
76 changes: 34 additions & 42 deletions tests/Events/PurchaseEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,54 @@

namespace Imdhemy\Purchases\Tests\Events;

use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Response;
use Imdhemy\GooglePlay\ClientFactory;
use Imdhemy\GooglePlay\DeveloperNotifications\DeveloperNotification;
use Imdhemy\Purchases\Events\GooglePlay\SubscriptionRenewed;
use Imdhemy\Purchases\Contracts\ServerNotificationContract;
use Imdhemy\Purchases\Contracts\SubscriptionContract;
use Imdhemy\Purchases\Events\PurchaseEvent;
use Imdhemy\Purchases\ServerNotifications\GoogleServerNotification;
use Imdhemy\Purchases\Tests\TestCase;

class PurchaseEventTest extends TestCase
{
/**
* @var PurchaseEvent
*/
private $event;
/** @test */
public function get_server_notification(): void
{
$serverNotification = $this->mock(ServerNotificationContract::class);
$sut = $this->getMockForAbstractClass(PurchaseEvent::class, [$serverNotification]);

/**
* @var GoogleServerNotification
*/
private $googleServerNotification;
$this->assertSame($serverNotification, $sut->getServerNotification());
}

/**
* {@inheritDoc}
*/
protected function setUp(): void
/** @test */
public function get_subscription(): void
{
parent::setUp();
$data = 'eyJ2ZXJzaW9uIjoiMS4wIiwicGFja2FnZU5hbWUiOiJjb20udHdpZ2Fuby5mYXNoaW9uIiwiZXZlbnRUaW1lTWlsbGlzIjoiMTYwMzMwMDgwNzM2MSIsInN1YnNjcmlwdGlvbk5vdGlmaWNhdGlvbiI6eyJ2ZXJzaW9uIjoiMS4wIiwibm90aWZpY2F0aW9uVHlwZSI6NCwicHVyY2hhc2VUb2tlbiI6ImFuZWZjcG1jamZib2RqbGNqZWVhY2piaC5BTy1KMU95NkxWQ2lJSkJBWUY4WVJCZklsaGZiSjlWTUJUamUybHo1bk1vSUV1SEdpMmdLVHczQXlZWEN4enhueGxKbWNOb0NEZlo2VnhFR05EQ0lLS1ZuVXZqUFZRODBPZyIsInN1YnNjcmlwdGlvbklkIjoid2Vla19wcmVtaXVtIn19';
$developerNotification = DeveloperNotification::parse($data);
$this->googleServerNotification = new GoogleServerNotification($developerNotification);
$this->event = new SubscriptionRenewed($this->googleServerNotification);
$subscription = $this->mock(SubscriptionContract::class);
$serverNotification = $this->mock(ServerNotificationContract::class);
$serverNotification->shouldReceive('getSubscription')->andReturn($subscription);
$sut = $this->getMockForAbstractClass(PurchaseEvent::class, [$serverNotification]);

$this->assertSame($subscription, $sut->getSubscription());
}

/**
* @test
*/
public function test_it_can_get_server_notification()
/** @test */
public function get_subscription_id(): void
{
$this->assertSame(
$this->googleServerNotification,
$this->event->getServerNotification()
);
$subscription = $this->mock(SubscriptionContract::class);
$subscription->shouldReceive('getItemId')->andReturn('com.example.subscription');
$serverNotification = $this->mock(ServerNotificationContract::class);
$serverNotification->shouldReceive('getSubscription')->andReturn($subscription);
$sut = $this->getMockForAbstractClass(PurchaseEvent::class, [$serverNotification]);

$this->assertSame('com.example.subscription', $sut->getSubscriptionId());
}

/**
* @test
*
* @throws GuzzleException
*/
public function test_it_can_get_subscription()
/** @test */
public function get_subscription_identifier(): void
{
$client = ClientFactory::mock(new Response(200, [], '[]'));
$subscription = $this->mock(SubscriptionContract::class);
$subscription->shouldReceive('getUniqueIdentifier')->andReturn('com.example.subscription.123456');
$serverNotification = $this->mock(ServerNotificationContract::class);
$serverNotification->shouldReceive('getSubscription')->andReturn($subscription);
$sut = $this->getMockForAbstractClass(PurchaseEvent::class, [$serverNotification]);

$this->assertEquals(
$this->googleServerNotification->getSubscription($client),
$this->event->getSubscription($client)
);
$this->assertSame('com.example.subscription.123456', $sut->getSubscriptionIdentifier());
}
}

0 comments on commit 6c3269f

Please sign in to comment.