Skip to content

Commit

Permalink
Merge pull request #5 from justbetter/feature/null-coalescing-assignm…
Browse files Browse the repository at this point in the history
…ent-operator

Added null coalescing assignment operator for the base resource
  • Loading branch information
VincentBean authored Nov 25, 2022
2 parents 2eae734 + 1b25482 commit 59e7709
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/OData/BaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ abstract class BaseResource implements ArrayAccess, Arrayable

public function __construct(?string $connection = null, ?string $endpoint = null)
{
$this->connection = $connection ?? config('dynamics.connection');
$this->endpoint = $endpoint ?? config('dynamics.resources.'.static::class, Str::afterLast(static::class, '\\'));
$this->connection ??= $connection ?? config('dynamics.connection');
$this->endpoint ??= $endpoint ?? config('dynamics.resources.'.static::class, Str::afterLast(static::class, '\\'));
}

public static function new(?string $connection = null, ?string $endpoint = null): static
Expand Down
16 changes: 16 additions & 0 deletions tests/Fakes/OData/FakeResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace JustBetter\DynamicsClient\Tests\Fakes\OData;

use JustBetter\DynamicsClient\OData\BaseResource;

class FakeResource extends BaseResource
{
public string $connection = '::fake-connection::';

public string $endpoint = '::fake-endpoint::';

public array $primaryKey = [
'No',
];
}
34 changes: 34 additions & 0 deletions tests/OData/BaseResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace JustBetter\DynamicsClient\Tests\OData;

use JustBetter\DynamicsClient\OData\Pages\Customer;
use JustBetter\DynamicsClient\Tests\Fakes\OData\FakeResource;
use JustBetter\DynamicsClient\Tests\TestCase;

class BaseResourceTest extends TestCase
Expand All @@ -11,6 +12,10 @@ protected function setUp(): void
{
parent::setUp();

config()->set('dynamics.resources', [
Customer::class => '::customer-endpoint::',
]);

config()->set('dynamics.connections.::default::', [
'base_url' => '::base_url::',
'company' => '::company::',
Expand Down Expand Up @@ -44,6 +49,14 @@ public function it_can_get_the_default_connection(): void
$this->assertEquals('::default::', $page->connection);
}

/** @test */
public function it_can_get_the_default_endpoint(): void
{
$page = new Customer();

$this->assertEquals('::customer-endpoint::', $page->endpoint);
}

/** @test */
public function it_can_set_the_connection(): void
{
Expand All @@ -55,4 +68,25 @@ public function it_can_set_the_connection(): void

$this->assertEquals('::other-connection::', $page->connection);
}

/** @test */
public function it_can_set_the_endpoint(): void
{
$page = new Customer(null, '::other-endpoint::');

$this->assertEquals('::other-endpoint::', $page->endpoint);

$page = Customer::new(null, '::other-endpoint::');

$this->assertEquals('::other-endpoint::', $page->endpoint);
}

/** @test */
public function it_can_force_a_connection_and_endpoint(): void
{
$resource = FakeResource::new();

$this->assertEquals('::fake-connection::', $resource->connection);
$this->assertEquals('::fake-endpoint::', $resource->endpoint);
}
}

0 comments on commit 59e7709

Please sign in to comment.