diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 26ec7d9..7a503e8 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -18,4 +18,4 @@ jobs: - name: Install dependencies run: composer i - name: Run linter - run: ./vendor/bin/php-cs-fixer fix --dry-run --diff --format=checkstyle | cs2pr + run: ./vendor/bin/php-cs-fixer fix --dry-run --diff --format=checkstyle --allow-risky=yes | cs2pr diff --git a/README.md b/README.md index f8a19f9..7356815 100644 --- a/README.md +++ b/README.md @@ -143,3 +143,8 @@ class TestCase extends PHPUnit The LiveIntent client inherits from Laravel's Http Client. Therefore, all the methods available to that client, will also be available here. For detailed documentation see [here](https://laravel.com/docs/8.x/http-client#testing). + +## Development + +For information about how to contribute to this project please see +[here](/docs/development.md). diff --git a/docs/development.md b/docs/development.md new file mode 100644 index 0000000..5fee8de --- /dev/null +++ b/docs/development.md @@ -0,0 +1,91 @@ +## Development + +To contribute to this package first you need to clone the repo and then copy the `.env` file. + +### Clone Repo +```php +git clone git@github.com:LiveIntent/sdk-php.git +``` + +After you clone the repo, then you need to copy the `.env.example` + +### Copy Env +```php +cp .env.example .env +``` + +After you copy the `.env.example` file then you can install the dependencies. + +### Install Dependencies +```php +composer install +``` + +If you get an error message to install PHP 8.0 then install via homebrew + +https://stitcher.io/blog/php-8-upgrade-mac + +Once the upgrade is successful, then you can try to install the dependencies. + +### Unit Tests + +To run the unit tests + +```php +composer test +``` + +The SDK uses Heimdall/Bifrost to authenticate so you need to create a Client and use that Client's Secret and Identifier. + +#### Create a Client in Bifrost + +First you need to get a token from Bifrost using the `oauth/token` endpoint. + +Grab the token and put it into the Authorize header in the Heimdall API. + +You can use the Heimdall API to create your client. + +```php +{ + "name": "testClient234", + "environment": "server", + "redirectUri": "https://qa-heimdall.liveintenteng.com/sign-in" +} +``` + +Take the response and copy the secret and identifier and put that into your `.env` + +```php +{ + "secret": "", + "id": , + "name": "", + "identifier": "", + "redirectUri": "https://qa-heimdall.liveintenteng.com/sign-in", + "created": "", + "updated": "" +} +``` + +#### Client Secret and Identifier + +In the `.env` file + +```php +CLIENT_ID= +CLIENT_SECRET= + +LI_BASE_URL=http://localhost:3000 // Pointing to Heimdall +``` + + +### Linting +The installed linter will auto-format your code to comply with our agreed php coding standard. + +To run the linter +```php +composer lint +``` + + + diff --git a/src/AdSlot.php b/src/AdSlot.php new file mode 100644 index 0000000..ddd36c4 --- /dev/null +++ b/src/AdSlot.php @@ -0,0 +1,8 @@ +isJson() - ? json_decode(collect($request->data())->flip()->first(), true) - : $request->data(); + // Initialize data when not json + $data = $request->data(); + + if ($request->isJson()) { + // Turn request data into an array + $temp = json_decode((string) collect($request->data()), true); + + $keys = array_keys($temp); + + // Grab the first array key and json_decode it + $data = json_decode(reset($keys), true); + } $excludedKeys = ['version', 'client_id', 'client_secret']; diff --git a/src/Services/NewsletterService.php b/src/Services/NewsletterService.php new file mode 100644 index 0000000..813d62a --- /dev/null +++ b/src/Services/NewsletterService.php @@ -0,0 +1,23 @@ + */ protected static $classMap = [ + 'adSlots' => AdSlotService::class, 'advertisers' => AdvertiserService::class, 'auth' => AuthService::class, 'campaigns' => CampaignService::class, 'insertionOrders' => InsertionOrderService::class, 'lineItems' => LineItemService::class, 'mediaGroups' => MediaGroupService::class, + 'newsletters' => NewsletterService::class, 'publishers' => PublisherService::class, 'users' => UserService::class, diff --git a/tests/Fixtures.php b/tests/Fixtures.php index 9f22f47..79ca1ff 100644 --- a/tests/Fixtures.php +++ b/tests/Fixtures.php @@ -4,6 +4,18 @@ abstract class Fixtures { + /** @return string */ + public static function adSlotHash() + { + return '60220c073cc811ec941d0ab243175da9'; + } + + /** @return int */ + public static function adSlotId() + { + return 834557; + } + /** @return string */ public static function campaignHash() { @@ -21,4 +33,22 @@ public static function lineItemHash() { return '00009758365a11e7943622000a974651'; } + + /** @return string */ + public static function newsletterHash() + { + return '03fb20f13cc811ec941d0ab243175da9'; + } + + /** @return int */ + public static function newsletterId() + { + return 36116; + } + + /** @return string */ + public static function publisherHash() + { + return 'b3e515b13cc711ec941d0ab243175da9'; + } } diff --git a/tests/Services/AdSlotServiceTest.php b/tests/Services/AdSlotServiceTest.php new file mode 100644 index 0000000..148a001 --- /dev/null +++ b/tests/Services/AdSlotServiceTest.php @@ -0,0 +1,113 @@ +service->find(Fixtures::adSlotId()); + $this->assertInstanceOf(AdSlot::class, $adSlot); + + $adSlot = $this->service->find(Fixtures::adSlotHash()); + $this->assertInstanceOf(AdSlot::class, $adSlot); + } + + public function testIsCreatableViaAttributesArray() + { + $adSlot = $this->service->create([ + 'name' => 'SDK Test', + 'newsletter' => Fixtures::newsletterHash(), + 'type' => 'image', + 'mediaType' => 'newsletter', + 'sizes' => [ + [ + 'width' => 500, + 'height' => 600, + 'floor' => 1.0, + 'deviceTypes' => [1,2,3], + ], + ], + 'adIndicatorId' => 1, + ]); + + $this->assertNotNull($adSlot->id); + $this->assertInstanceOf(AdSlot::class, $adSlot); + } + + public function testIsCreatableViaResourceInstance() + { + $adSlot = new AdSlot([ + 'name' => 'SDK Test', + 'newsletter' => Fixtures::newsletterHash(), + 'type' => 'image', + 'mediaType' => 'newsletter', + 'sizes' => [ + [ + 'width' => 500, + 'height' => 600, + 'floor' => 1.0, + 'deviceTypes' => [1,2,3], + ], + ], + 'adIndicatorId' => 1, + ]); + + $adSlot = $this->service->create($adSlot); + + $this->assertNotNull($adSlot->id); + $this->assertInstanceOf(AdSlot::class, $adSlot); + } + + public function testIsUpdateableViaAttributesArray() + { + $adSlot = $this->service->find(Fixtures::adSlotId()); + + $updatedName = 'SDK_TEST_UPDATE_NAME'; + + $adSlot = $this->service->update([ + 'id' => $adSlot->id, + 'version' => $adSlot->version, + 'name' => $updatedName, + 'sizes' => [ + [ + 'width' => 400, + 'height' => 500, + 'floor' => 1.0, + 'deviceTypes' => [1,2,3], + ], + ], + ]); + + $this->assertEquals($updatedName, $adSlot->name); + $this->assertInstanceOf(AdSlot::class, $adSlot); + } + + public function testIsUpdateableViaResourceInstance() + { + $adSlot = $this->service->find(Fixtures::adSlotId()); + $updatedName = 'SDK_TEST_UPDATE_NAME'; + + $adSlot->name = $updatedName; + $adSlot = $this->service->update($adSlot); + + $this->assertEquals($updatedName, $adSlot->name); + $this->assertInstanceOf(AdSlot::class, $adSlot); + } + + public function testThrowsWhenInvalidDataIsPassed() + { + $this->expectException(InvalidRequestException::class); + + $this->service->create([ + 'name' => 'SDK Test', + 'newsletter' => Fixtures::newsletterHash(), + ]); + } +} diff --git a/tests/Services/AuthServiceTest.php b/tests/Services/AuthServiceTest.php index adcaf67..f96b242 100644 --- a/tests/Services/AuthServiceTest.php +++ b/tests/Services/AuthServiceTest.php @@ -14,7 +14,7 @@ public function testGetCurrentUserWithAccessToken() $tokenService = new TokenService([ 'client_id' => env('CLIENT_ID'), 'client_secret' => env('CLIENT_SECRET'), - 'base_url' => env('LI_BASE_URL', 'http://localhost:33001'), + 'base_url' => env('LI_BASE_URL', 'http://localhost:3000'), ]); if (env('USE_SNAPSHOTS', true)) { diff --git a/tests/Services/NewsletterServiceTest.php b/tests/Services/NewsletterServiceTest.php new file mode 100644 index 0000000..bd2fcd1 --- /dev/null +++ b/tests/Services/NewsletterServiceTest.php @@ -0,0 +1,85 @@ +service->find(Fixtures::newsletterId()); + $this->assertInstanceOf(Newsletter::class, $newsletter); + + $newsletter = $this->service->find(Fixtures::newsletterHash()); + $this->assertInstanceOf(Newsletter::class, $newsletter); + } + + public function testIsCreatableViaAttributesArray() + { + $newsletter = $this->service->create([ + 'name' => 'SDK Test', + 'publisher' => Fixtures::publisherHash(), + 'category' => 1, + ]); + + $this->assertNotNull($newsletter->id); + $this->assertInstanceOf(Newsletter::class, $newsletter); + } + + public function testIsCreatableViaResourceInstance() + { + $newsletter = new Newsletter([ + 'name' => 'SDK Test', + 'publisher' => Fixtures::publisherHash(), + 'category' => 1, + ]); + + $newsletter = $this->service->create($newsletter); + + $this->assertNotNull($newsletter->id); + $this->assertInstanceOf(Newsletter::class, $newsletter); + } + + public function testIsUpdateableViaAttributesArray() + { + $newsletter = $this->service->find(Fixtures::newsletterId()); + + $updatedName = 'SDK_TEST_UPDATE_NAME'; + + $newsletter = $this->service->update([ + 'id' => $newsletter->id, + 'version' => $newsletter->version, + 'name' => $updatedName, + ]); + + $this->assertEquals($updatedName, $newsletter->name); + $this->assertInstanceOf(Newsletter::class, $newsletter); + } + + public function testIsUpdateableViaResourceInstance() + { + $newsletter = $this->service->find(Fixtures::newsletterId()); + $updatedName = 'SDK_TEST_UPDATE_NAME'; + + $newsletter->name = $updatedName; + $this->service->update($newsletter); + + $this->assertEquals($updatedName, $newsletter->name); + $this->assertInstanceOf(Newsletter::class, $newsletter); + } + + public function testThrowsWhenInvalidDataIsPassed() + { + $this->expectException(InvalidRequestException::class); + + $this->service->create([ + 'name' => 'SDK Test', + 'publisher' => Fixtures::publisherHash(), + ]); + } +} diff --git a/tests/Services/ServiceTestCase.php b/tests/Services/ServiceTestCase.php index b7ac81b..487b742 100644 --- a/tests/Services/ServiceTestCase.php +++ b/tests/Services/ServiceTestCase.php @@ -44,7 +44,7 @@ private function createClient() $client = new LiveIntentClient([ 'client_id' => env('CLIENT_ID'), 'client_secret' => env('CLIENT_SECRET'), - 'base_url' => env('LI_BASE_URL', 'http://localhost:33001'), + 'base_url' => env('LI_BASE_URL', 'http://localhost:3000'), // 'middleware' => [ // Middleware::log( // $log, diff --git a/tests/__snapshots__/snapshot b/tests/__snapshots__/snapshot index b34ad0b..063a48b 100644 Binary files a/tests/__snapshots__/snapshot and b/tests/__snapshots__/snapshot differ