Skip to content

Commit

Permalink
✨ rest api attributes connector (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
julianzimmermann committed Oct 26, 2023
0 parents commit 71c9891
Show file tree
Hide file tree
Showing 16 changed files with 334 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: CI

on:
push:
branches: [ main ]

jobs:
continous-integration:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php-versions: [ "8.0", "8.1", "8.2" ]
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2 #https://github.com/shivammathur/setup-php
with:
php-version: ${{ matrix.php-versions }}
extensions: mbstring, dom, fileinfo, pgsql, json
coverage: xdebug #optional
- name: Get composer cache directory
id: composercache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache composer dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composercache.outputs.dir }}
# Use composer.json for key, if composer.lock is not committed.
# key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install dependencies and run ci tasks
run: make install ci
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.idea

.DS_Store

vendor
composer.lock
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2023-present FOND OF GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.PHONY: install
install:
composer install

.PHONY: phpcs
phpcs:
./vendor/bin/phpcs --standard=./vendor/spryker/code-sniffer/Spryker/ruleset.xml ./src/FondOfOryx/* ./tests/*

.PHONY: phpcbf
phpcbf:
./vendor/bin/phpcbf --standard=./vendor/spryker/code-sniffer/Spryker/ruleset.xml ./src/FondOfOryx/* ./tests/*

.PHONY: phpstan
phpstan:
./vendor/bin/phpstan --memory-limit=-1 analyse ./src/FondOfOryx ./tests

.PHONY: codeception
codeception:
./vendor/bin/codecept run --env standalone

.PHONY: ci
ci: phpcs codeception phpstan

.PHONY: clean
clean:
rm -Rf composer.lock
rm -Rf ./vendor
find ./tests/_output/ -not -name .gitignore -delete
rm -Rf src/Generated/*
rm -Rf src/Orm/*
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Carts Rest Api Attributes Connector
[![CI](https://github.com/fond-of-oryx/carts-rest-api-attributes-connector/actions/workflows/main.yml/badge.svg)](https://github.com/fond-of-oryx/carts-rest-api-attributes-connector/actions/workflows/main.yml)
[![license](https://img.shields.io/github/license/fond-of-oryx/carts-rest-api-attributes-connector.svg)](https://packagist.org/packages/fond-of-oryx/carts-rest-api-attributes-connector)

## Installation

```
composer require fond-of-oryx/carts-rest-api-attributes-connector
```
43 changes: 43 additions & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace: fond-of-oryx/carts-rest-api-attributes-connector

suites:
unit:
path: .

settings:
shuffle: true
lint: true

bootstrap: _bootstrap.php

paths:
tests: tests
output: tests/_output
support: tests/_support
data: tests/_data

coverage:
enabled: true
include:
- src/*.php

modules:
enabled:
- \FondOfCodeception\Module\Spryker
config:
\FondOfCodeception\Module\Spryker:
generate_transfer: false
generate_map_classes: false
generate_propel_classes: false
generate_ide_auto_completion: false

env:
standalone:
modules:
config:
\FondOfCodeception\Module\Spryker:
generate_transfer: true
generate_map_classes: true
generate_propel_classes: true
generate_ide_auto_completion: true
monorepo:
40 changes: 40 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "fond-of-oryx/carts-rest-api-attributes-connector",
"description": "Carts Rest Api Attributes Connector Package",
"license": "MIT",
"authors": [
{
"name": "Julian Hyatt",
"email": "dev@jzit.de"
}
],
"require": {
"php": ">=8.0",
"spryker/carts-rest-api-extension": "^1.0.0"
},
"require-dev": {
"fond-of-codeception/spryker": "^2.0.0",
"spryker/code-sniffer": "*",
"spryker-sdk/phpstan-spryker": "*"
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"psr-4": {
"FondOfOryx\\": "src/FondOfOryx/"
}
},
"autoload-dev": {
"psr-4": {
"FondOfOryx\\": "tests/FondOfOryx/",
"Generated\\": "src/Generated/",
"Orm\\Zed\\": "src/Orm/Zed/"
}
},
"config": {
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parameters:
level: 5
reportUnmatchedIgnoredErrors: false
scanDirectories:
- src/Orm/Zed
ignoreErrors:
- '#.* on an unknown class Generated\\(Client|Glue|Service|Yves|Zed)\\.*#'
- '#Dead catch - Exception is never thrown in the try block.#'
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace FondOfOryx\Glue\CartsRestApiAttributesConnector\Plugin;

use Generated\Shared\Transfer\ItemTransfer;
use Generated\Shared\Transfer\RestItemsAttributesTransfer;
use Spryker\Glue\CartsRestApiExtension\Dependency\Plugin\RestCartItemsAttributesMapperPluginInterface;

class ConcreteAttributesRestCartItemsAttributesMapperPlugin implements RestCartItemsAttributesMapperPluginInterface
{
/**
* @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
* @param \Generated\Shared\Transfer\RestItemsAttributesTransfer $restItemsAttributesTransfer
* @param string $localeName
*
* @return \Generated\Shared\Transfer\RestItemsAttributesTransfer
*/
public function mapItemTransferToRestItemsAttributesTransfer(
ItemTransfer $itemTransfer,
RestItemsAttributesTransfer $restItemsAttributesTransfer,
string $localeName
): RestItemsAttributesTransfer {
return $restItemsAttributesTransfer->setProductConcreteAttributes($this->convertArrayKeysToCamelCase($itemTransfer->getConcreteAttributes()));
}

/**
* @param array<string, string> $array
*
* @return array<string, string>
*/
protected function convertArrayKeysToCamelCase(array $array): array
{
$newArray = [];
foreach ($array as $key => $value) {
$newArray[$this->stringToCamelCase($key)] = $value;
}

return $newArray;
}

/**
* @param string $string
*
* @return string
*/
protected function stringToCamelCase(string $string): string
{
return lcfirst(str_replace('_', '', ucwords($string, '_')));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<transfers xmlns="spryker:transfer-01"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="spryker:transfer-01 https://static.spryker.com/transfer-01.xsd">
<transfer name="RestItemsAttributes">
<property name="productConcreteAttributes" associative="true" type="array" singular="productConcreteAttribute"/>
</transfer>

<transfer name="Item">
<property name="concreteAttributes" type="array" singular="concreteAttribute"/>
</transfer>
</transfers>
Empty file added tests/FondOfOryx/.gitkeep
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace FondOfOryx\Glue\CartsRestApiAttributesConnector\Plugin;

use Codeception\Test\Unit;
use Generated\Shared\Transfer\ItemTransfer;
use Generated\Shared\Transfer\RestItemsAttributesTransfer;
use PHPUnit\Framework\MockObject\MockObject;

class ConcreteAttributesRestCartItemsAttributesMapperPluginTest extends Unit
{
/**
* @var \Generated\Shared\Transfer\ItemTransfer|\PHPUnit\Framework\MockObject\MockObject
*/
protected ItemTransfer|MockObject $itemTransferMock;

/**
* @var \Generated\Shared\Transfer\RestItemsAttributesTransfer|\PHPUnit\Framework\MockObject\MockObject
*/
protected RestItemsAttributesTransfer|MockObject $restItemsAttributesTransferMock;

/**
* @var \FondOfOryx\Glue\CartsRestApiAttributesConnector\Plugin\ConcreteAttributesRestCartItemsAttributesMapperPlugin
*/
protected $plugin;

/**
* @return void
*/
protected function _before(): void
{
parent::_before();

$this->itemTransferMock = $this->getMockBuilder(ItemTransfer::class)
->disableOriginalConstructor()
->getMock();

$this->restItemsAttributesTransferMock = $this->getMockBuilder(RestItemsAttributesTransfer::class)
->disableOriginalConstructor()
->getMock();

$this->plugin = new ConcreteAttributesRestCartItemsAttributesMapperPlugin();
}

/**
* @return void
*/
public function testMapItemTransferToRestItemsAttributesTransfer(): void
{
$attributes = [
'model' => 'test',
'abc_def' => 'ghi',
];

$attributesCheck = [
'model' => 'test',
'abcDef' => 'ghi',
];

$this->itemTransferMock
->expects(static::atLeastOnce())
->method('getConcreteAttributes')
->willReturn($attributes);

$this->restItemsAttributesTransferMock
->expects(static::once())
->method('setProductConcreteAttributes')
->with($attributesCheck)
->willReturnSelf();

$this->plugin->mapItemTransferToRestItemsAttributesTransfer($this->itemTransferMock, $this->restItemsAttributesTransferMock, '');
}
}
17 changes: 17 additions & 0 deletions tests/_bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

$pathToAutoloader = codecept_root_dir('vendor/autoload.php');

if (!file_exists($pathToAutoloader)) {
$pathToAutoloader = codecept_root_dir('../../vendor/autoload.php');
}

if (!file_exists($pathToAutoloader)) {
$pathToAutoloader = codecept_root_dir('../../autoload.php');
}

if (!file_exists($pathToAutoloader)) {
$pathToAutoloader = codecept_root_dir('../../../../autoload.php');
}

require_once $pathToAutoloader;
Empty file added tests/_data/.gitkeep
Empty file.
2 changes: 2 additions & 0 deletions tests/_output/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
Empty file added tests/_support/.gitkeep
Empty file.

0 comments on commit 71c9891

Please sign in to comment.