tests fixed
This commit is contained in:
		
							parent
							
								
									2138762fb2
								
							
						
					
					
						commit
						3719bbef53
					
				|  | @ -9,9 +9,6 @@ use Cake\Core\ContainerInterface; | |||
| use Cake\Core\PluginApplicationInterface; | ||||
| use Cake\Http\MiddlewareQueue; | ||||
| use Cake\Routing\RouteBuilder; | ||||
| use CakeProducts\Queue\Task\ProductCategories\CreateTask; | ||||
| use CakeProducts\Service\CatalogManagerServiceProvider; | ||||
| use CakeProducts\Service\ExternalCatalogManagerService; | ||||
| 
 | ||||
| /** | ||||
|  * Plugin for CakeProducts | ||||
|  |  | |||
|  | @ -47,6 +47,7 @@ class ExternalProductCatalogsController extends AppController | |||
|      */ | ||||
|     public function add() | ||||
|     { | ||||
|         Log::debug('inside add'); | ||||
|         $externalProductCatalog = $this->ExternalProductCatalogs->newEmptyEntity(); | ||||
|         if ($this->request->is('post')) { | ||||
|             $externalProductCatalog = $this->ExternalProductCatalogs->patchEntity($externalProductCatalog, $this->request->getData()); | ||||
|  |  | |||
|  | @ -1,29 +0,0 @@ | |||
| <?php | ||||
| 
 | ||||
| namespace CakeProducts\Service; | ||||
| 
 | ||||
| use Cake\Core\ContainerInterface; | ||||
| use Cake\Core\Plugin; | ||||
| use Cake\Core\ServiceConfig; | ||||
| use Cake\Core\ServiceProvider; | ||||
| use Cake\Log\Log; | ||||
| use Cake\ORM\Locator\LocatorAwareTrait; | ||||
| use CakeProducts\Service\InternalCatalogManagerService; | ||||
| 
 | ||||
| class CatalogManagerServiceProvider extends ServiceProvider | ||||
| { | ||||
|     protected array $provides = [ | ||||
|         InternalCatalogManagerService::class | ||||
|     ]; | ||||
| 
 | ||||
|     /** | ||||
|      * @param ContainerInterface $container | ||||
|      * | ||||
|      * @return void | ||||
|      */ | ||||
|     public function services(ContainerInterface $container): void | ||||
|     { | ||||
|         $container->add(InternalCatalogManagerService::class) | ||||
|             ->addArgument(new ExternalCatalogManagerService()); | ||||
|     } | ||||
| } | ||||
|  | @ -1,184 +0,0 @@ | |||
| <?php | ||||
| 
 | ||||
| namespace CakeProducts\Service; | ||||
| 
 | ||||
| use Cake\Cache\Cache; | ||||
| use Cake\Core\ServiceConfig; | ||||
| use Cake\Http\Client; | ||||
| use Cake\I18n\FrozenTime; | ||||
| use Cake\I18n\Time; | ||||
| use Cake\Log\Log; | ||||
| use Cake\ORM\Locator\LocatorAwareTrait; | ||||
| use CakeProducts\Model\Entity\ExternalProductCatalog; | ||||
| use CakeProducts\Model\Entity\ProductCategory; | ||||
| use CakeProducts\Model\Table\ProductCatalogsTable; | ||||
| 
 | ||||
| class ExternalCatalogManagerService | ||||
| { | ||||
|     use LocatorAwareTrait; | ||||
| 
 | ||||
|     /** | ||||
|      * @var ProductCatalogsTable | ||||
|      */ | ||||
|     protected \Cake\ORM\Table|ProductCatalogsTable $ProductCatalogs; | ||||
| 
 | ||||
|     /** | ||||
|      * @var ServiceConfig | ||||
|      */ | ||||
|     protected ServiceConfig $serviceConfig; | ||||
| 
 | ||||
|     /** | ||||
|      * @var Client | ||||
|      */ | ||||
|     protected Client $httpClient; | ||||
| 
 | ||||
|     /** | ||||
|      * | ||||
|      */ | ||||
|     public function __construct() | ||||
|     { | ||||
|         $this->ProductCatalogs = $this->fetchTable('CakeProducts.ProductCatalogs'); | ||||
|         $this->serviceConfig = new ServiceConfig(); | ||||
|         $this->httpClient = new Client([ | ||||
| //            'host' => $config['base_url'],
 | ||||
| //            'scheme' => 'https',
 | ||||
| //            'scheme' => 'http',
 | ||||
|         ]); | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     public function newCategoryCreated(ProductCategory $productCategory) | ||||
|     { | ||||
|         $results = []; | ||||
| 
 | ||||
|         $externalProductCatalogs = $this->_getExternalProductCatalogsForCatalogId($productCategory->product_catalog_id); | ||||
|         foreach ($externalProductCatalogs as $externalProductCatalog) { | ||||
|             $results[] = $this->_createNewCategoryForExternalProductCatalog($externalProductCatalog, $productCategory); | ||||
|         } | ||||
| 
 | ||||
|         return $results; | ||||
|     } | ||||
| 
 | ||||
|     protected function _createNewCategoryForExternalProductCatalog(ExternalProductCatalog $externalProductCatalog, ProductCategory $productCategory) | ||||
|     { | ||||
|         $url = $externalProductCatalog->api_url . '/product-categories'; | ||||
|         $response = $this->postToUrl($url, $productCategory->toArray()); | ||||
| 
 | ||||
|         Log::debug(print_r('$response->getJson()', true)); | ||||
|         Log::debug(print_r($response->getJson(), true)); | ||||
|         Log::debug(print_r('$response->getStatusCode()', true)); | ||||
|         Log::debug(print_r($response->getStatusCode(), true)); | ||||
| 
 | ||||
|         return $response->getStatusCode(); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @return mixed|null | ||||
|      */ | ||||
|     public function getJwtToken() | ||||
|     { | ||||
|         Log::debug('inside getJwtToken'); | ||||
|         if (Cache::read('product_catalog_api_token')) { | ||||
|             Log::debug('token was cached'); | ||||
| //            return Cache::read('product_catalog_api_token');
 | ||||
|         } else { | ||||
|             Log::debug('token was **NOT** cached'); | ||||
|         } | ||||
| 
 | ||||
|         $response = $this->httpClient->post('http://localhost:8766/api/v1/users/token', json_encode([ | ||||
|             'username' => 'test', | ||||
|             'password' => 'test', | ||||
|         ]), ['headers' => ['Accept' => 'application/json', 'Content-Type' => 'application/json']]); | ||||
| //        $this->httpClient->getConfig();
 | ||||
|         if ($response->isOk()) { | ||||
|             $json = $response->getJson(); | ||||
|             $token = array_key_exists('token', $json) ? $json['token'] : null; | ||||
|             Cache::write('product_catalog_api_token', $token); | ||||
|             Log::debug('$token'); | ||||
|             Log::debug($token); | ||||
| 
 | ||||
|             return $token; | ||||
|         } | ||||
|         Log::debug('$response->getStringBody()'); | ||||
|         Log::debug($response->getStringBody()); | ||||
|         Log::debug(print_r('$response->getStatusCode()', true)); | ||||
|         Log::debug(print_r($response->getStatusCode(), true)); | ||||
| 
 | ||||
|         return null; | ||||
|     } | ||||
| 
 | ||||
|     public function postToUrl(string $url, array $data, int $tries = 0) | ||||
|     { | ||||
| //        if (true || !Cache::read('product_catalog_api_token')) {
 | ||||
|         $token = $this->getJwtToken(); | ||||
| //        }
 | ||||
|         Log::debug('$token inside postToUrl' . $token); | ||||
| 
 | ||||
|         Log::debug('Cache::read(product_catalog_api_token)'); | ||||
|         Log::debug(Cache::read('product_catalog_api_token') ? Cache::read('product_catalog_api_token') : 'NULL'); | ||||
|         Log::debug('ATTEMPT # ' . $tries); | ||||
| 
 | ||||
|         $response = $this->httpClient->post($url, json_encode($data), [ | ||||
|             'headers' => [ | ||||
|                 'Accept' => 'application/json', | ||||
|                 'Content-Type' => 'application/json', | ||||
| //                'Authorization' => 'Bearer ' . base64_encode(Cache::read('product_catalog_api_token'))
 | ||||
|                 'Authorization' => 'Bearer ' . $token, | ||||
|             ] | ||||
|         ]); | ||||
| 
 | ||||
|         if (!$response->isOk()) { | ||||
|             $tries++; | ||||
|         } | ||||
|         if ($tries > 3) { | ||||
|             return $response; | ||||
|         } | ||||
|         if ($response->getStatusCode() == 401) { | ||||
|             $this->postToUrl($url, $data, $tries); | ||||
|         } | ||||
|         Log::debug('$response->getJson'); | ||||
|         Log::debug(print_r($response->getJson(), true)); | ||||
| 
 | ||||
|         return $response; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param string $url | ||||
|      * @param array $data | ||||
|      * @param int $tries | ||||
|      * | ||||
|      * @return mixed | ||||
|      */ | ||||
|     public function putToUrl(string $url, array $data, int $tries = 0) | ||||
|     { | ||||
|         if (!Cache::read('product_catalog_api_token')) { | ||||
|             $this->getJwtToken(); | ||||
|         } | ||||
| 
 | ||||
|         $response = $this->httpClient->put($url, json_encode($data), [ | ||||
|             'headers' => [ | ||||
|                 'Accept' => 'application/json', | ||||
|                 'Content-Type' => 'application/json', | ||||
|                 'Authorization' => 'Bearer ' . Cache::read('product_catalog_api_token') | ||||
|             ], | ||||
|         ]); | ||||
| 
 | ||||
|         if (!$response->isOk()) { | ||||
|             $tries++; | ||||
|         } | ||||
|         if ($tries > 3) { | ||||
|             return $response; | ||||
|         } | ||||
|         if ($response->getStatusCode() == 401) { | ||||
|             $this->getJwtToken(); | ||||
|             $this->putToUrl($url, $data, $tries); | ||||
|         } | ||||
| 
 | ||||
|         return $response; | ||||
|     } | ||||
| 
 | ||||
|     protected function _getExternalProductCatalogsForCatalogId(string $productCatalogId) | ||||
|     { | ||||
|         return $this->ProductCatalogs->ExternalProductCatalogs->find()->where(['product_catalog_id' => $productCatalogId])->toArray(); | ||||
|     } | ||||
| } | ||||
|  | @ -1,141 +0,0 @@ | |||
| <?php | ||||
| 
 | ||||
| namespace CakeProducts\Service; | ||||
| 
 | ||||
| use Cake\Core\ServiceConfig; | ||||
| use Cake\Datasource\EntityInterface; | ||||
| use Cake\I18n\FrozenTime; | ||||
| use Cake\I18n\Time; | ||||
| use Cake\Log\Log; | ||||
| use Cake\ORM\Locator\LocatorAwareTrait; | ||||
| use Cake\ORM\Table; | ||||
| use Cake\Utility\Text; | ||||
| use CakeProducts\Model\Entity\ExternalProductCatalog; | ||||
| use CakeProducts\Model\Entity\ProductCategory; | ||||
| use CakeProducts\Model\Table\ProductCatalogsTable; | ||||
| 
 | ||||
| class InternalCatalogManagerService | ||||
| { | ||||
|     use LocatorAwareTrait; | ||||
| 
 | ||||
|     /** | ||||
|      * @var ProductCatalogsTable | ||||
|      */ | ||||
|     protected Table|ProductCatalogsTable $ProductCatalogs; | ||||
| 
 | ||||
|     /** | ||||
|      * @var ServiceConfig | ||||
|      */ | ||||
|     protected ServiceConfig $serviceConfig; | ||||
| 
 | ||||
|     /** | ||||
|      * @var ExternalCatalogManagerService|null | ||||
|      */ | ||||
|     protected ?ExternalCatalogManagerService $externalCatalogManager; | ||||
| 
 | ||||
|     /** | ||||
|      * | ||||
|      */ | ||||
|     public function __construct(ExternalCatalogManagerService $externalCatalogManagerService) | ||||
|     { | ||||
|         $this->ProductCatalogs = $this->fetchTable('CakeProducts.ProductCatalogs'); | ||||
|         $this->serviceConfig = new ServiceConfig(); | ||||
| 
 | ||||
|         if ($this->serviceConfig->get('CakeProducts.internal.enabled') && $this->serviceConfig->get('CakeProducts.internal.syncExternally')) { | ||||
|             $this->externalCatalogManager = $externalCatalogManagerService; | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
|     public function getCatalog(string $id = null) | ||||
|     { | ||||
|         $contain = ['ProductCategories']; | ||||
|         if ($this->serviceConfig->get('CakeProducts.internal.syncExternally')) { | ||||
|             $contain[] = 'ExternalProductCatalogs'; | ||||
|         } | ||||
| 
 | ||||
|         return $this->ProductCatalogs->get($id, contain: $contain); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param ExternalProductCatalog $externalProductCatalog external product catalog entity | ||||
|      * @param array $data data to save | ||||
|      * | ||||
|      * @return array | ||||
|      */ | ||||
|     public function createNewExternalCatalog(ExternalProductCatalog $externalProductCatalog, array $data = []): array | ||||
|     { | ||||
|         $now = Time::now(); | ||||
|         $associated = []; | ||||
| 
 | ||||
|         Log::info('posted data - adding new ExternalProductCatalog'); | ||||
|         Log::info(print_r($data, true)); | ||||
| 
 | ||||
|         $saveOptions = [ | ||||
|             'associated' => $associated, | ||||
|         ]; | ||||
|         $externalProductCatalog = $this->ProductCatalogs->ExternalProductCatalogs->patchEntity($externalProductCatalog, $data, $saveOptions); | ||||
|         if ($externalProductCatalog->getErrors()) { | ||||
|             Log::debug(print_r('$externalProductCatalog->getErrors() next - failed to save from create new external product catalog', true)); | ||||
|             Log::debug(print_r($externalProductCatalog->getErrors(), true)); | ||||
|         } | ||||
|         $returnData = [ | ||||
|             'entity' => $externalProductCatalog, | ||||
|             'result' => $this->ProductCatalogs->ExternalProductCatalogs->save($externalProductCatalog, $saveOptions), | ||||
|             'apiResults' => [], | ||||
|         ]; | ||||
|         if ($returnData['result'] && $this->externalCatalogManager) { | ||||
| //            $returnData['apiResults'] = $this->externalCatalogManager->newCatalogCreated($returnData['result']);
 | ||||
|         } | ||||
| 
 | ||||
|         return $returnData; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param string|null $id | ||||
|      * | ||||
|      * @return \App\Model\Entity\ProductCategory|EntityInterface | ||||
|      */ | ||||
|     public function getCategory(string $id = null) | ||||
|     { | ||||
|         $contain = ['ProductCatalogs', 'ParentProductCategories', 'ChildProductCategories']; | ||||
| 
 | ||||
|         return $this->ProductCatalogs->ProductCategories->get($id, contain: $contain); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @param ProductCategory $productCategory product category entity | ||||
|      * @param array $data data to save | ||||
|      * | ||||
|      * @return array | ||||
|      */ | ||||
|     public function createNewCategory(ProductCategory $productCategory, array $data = []): array | ||||
|     { | ||||
|         $now = Time::now(); | ||||
|         $associated = []; | ||||
| 
 | ||||
|         Log::info('posted data - adding new ProductCategory'); | ||||
|         Log::info(print_r($data, true)); | ||||
| 
 | ||||
|         $saveOptions = [ | ||||
|             'associated' => $associated, | ||||
|         ]; | ||||
|         if (!array_key_exists('internal_id', $data) || !$data['internal_id']) { | ||||
|             $data['internal_id'] = Text::uuid(); | ||||
|         } | ||||
|         $productCategory = $this->ProductCatalogs->ProductCategories->patchEntity($productCategory, $data, $saveOptions); | ||||
|         if ($productCategory->getErrors()) { | ||||
|             Log::debug(print_r('$productCategory->getErrors() next - failed to save from create new product category', true)); | ||||
|             Log::debug(print_r($productCategory->getErrors(), true)); | ||||
|         } | ||||
|         $returnData = [ | ||||
|             'entity' => $productCategory, | ||||
|             'result' => $this->ProductCatalogs->ProductCategories->save($productCategory, $saveOptions), | ||||
|             'apiResults' => [], | ||||
|         ]; | ||||
|         if ($returnData['result'] && $this->externalCatalogManager) { | ||||
|             $returnData['apiResults'] = $this->externalCatalogManager->newCategoryCreated($returnData['result']); | ||||
|         } | ||||
| 
 | ||||
|         return $returnData; | ||||
|     } | ||||
| } | ||||
|  | @ -43,8 +43,9 @@ class ExternalProductCatalogsControllerTest extends BaseControllerTest | |||
|     protected function setUp(): void | ||||
|     { | ||||
|         parent::setUp(); | ||||
|         $this->enableCsrfToken(); | ||||
|         $this->enableSecurityToken(); | ||||
| //        $this->enableCsrfToken();
 | ||||
| //        $this->enableSecurityToken();
 | ||||
|         $this->disableErrorHandlerMiddleware(); | ||||
|         $this->ExternalProductCatalogs = $this->getTableLocator()->get('ExternalProductCatalogs'); | ||||
|     } | ||||
| 
 | ||||
|  |  | |||
|  | @ -42,8 +42,9 @@ class ProductCatalogsControllerTest extends BaseControllerTest | |||
|     protected function setUp(): void | ||||
|     { | ||||
|         parent::setUp(); | ||||
|         $this->enableCsrfToken(); | ||||
|         $this->enableSecurityToken(); | ||||
| //        $this->enableCsrfToken();
 | ||||
| //        $this->enableSecurityToken();
 | ||||
|         $this->disableErrorHandlerMiddleware(); | ||||
|         $config = $this->getTableLocator()->exists('ProductCatalogs') ? [] : ['className' => ProductCatalogsTable::class]; | ||||
|         $this->ProductCatalogs = $this->getTableLocator()->get('ProductCatalogs', $config); | ||||
|     } | ||||
|  |  | |||
|  | @ -42,8 +42,9 @@ class ProductCategoriesControllerTest extends BaseControllerTest | |||
|     protected function setUp(): void | ||||
|     { | ||||
|         parent::setUp(); | ||||
|         $this->enableCsrfToken(); | ||||
|         $this->enableSecurityToken(); | ||||
| //        $this->enableCsrfToken();
 | ||||
| //        $this->enableSecurityToken();
 | ||||
|         $this->disableErrorHandlerMiddleware(); | ||||
|         $this->ProductCategories = $this->getTableLocator()->get('ProductCategories'); | ||||
|     } | ||||
| 
 | ||||
|  |  | |||
|  | @ -40,8 +40,8 @@ class ProductCategoryAttributeOptionsControllerTest extends BaseControllerTest | |||
|     protected function setUp(): void | ||||
|     { | ||||
|         parent::setUp(); | ||||
|         $this->enableCsrfToken(); | ||||
|         $this->enableSecurityToken(); | ||||
| //        $this->enableCsrfToken();
 | ||||
| //        $this->enableSecurityToken();
 | ||||
|         $config = $this->getTableLocator()->exists('ProductCategoryAttributeOptions') ? [] : ['className' => ProductCategoryAttributeOptionsTable::class]; | ||||
|         $this->ProductCategoryAttributeOptions = $this->getTableLocator()->get('ProductCategoryAttributeOptions', $config); | ||||
|     } | ||||
|  |  | |||
|  | @ -42,8 +42,8 @@ class ProductCategoryAttributesControllerTest extends BaseControllerTest | |||
|     protected function setUp(): void | ||||
|     { | ||||
|         parent::setUp(); | ||||
|         $this->enableCsrfToken(); | ||||
|         $this->enableSecurityToken(); | ||||
| //        $this->enableCsrfToken();
 | ||||
| //        $this->enableSecurityToken();
 | ||||
|         $config = $this->getTableLocator()->exists('ProductCategoryAttributes') ? [] : ['className' => ProductCategoryAttributesTable::class]; | ||||
|         $this->ProductCategoryAttributes = $this->getTableLocator()->get('ProductCategoryAttributes', $config); | ||||
|     } | ||||
|  |  | |||
|  | @ -44,8 +44,8 @@ class ProductsControllerTest extends BaseControllerTest | |||
|     protected function setUp(): void | ||||
|     { | ||||
|         parent::setUp(); | ||||
|         $this->enableCsrfToken(); | ||||
|         $this->enableSecurityToken(); | ||||
| //        $this->enableCsrfToken();
 | ||||
| //        $this->enableSecurityToken();
 | ||||
|         $config = $this->getTableLocator()->exists('Products') ? [] : ['className' => ProductsTable::class]; | ||||
|         $this->Products = $this->getTableLocator()->get('Products', $config); | ||||
|     } | ||||
|  |  | |||
|  | @ -5,6 +5,7 @@ namespace TestApp; | |||
| use Cake\Http\BaseApplication; | ||||
| use Cake\Http\MiddlewareQueue; | ||||
| use Cake\Routing\Middleware\RoutingMiddleware; | ||||
| use Cake\Routing\Route\DashedRoute; | ||||
| use Cake\Routing\RouteBuilder; | ||||
| 
 | ||||
| class Application extends BaseApplication { | ||||
|  | @ -28,6 +29,7 @@ class Application extends BaseApplication { | |||
|     public function routes(RouteBuilder $routes): void | ||||
|     { | ||||
|         parent::routes($routes); // TODO: Change the autogenerated stub
 | ||||
|         $routes->setRouteClass(DashedRoute::class); | ||||
| 
 | ||||
|         $routes->plugin('CakeProducts', ['path' => '/cake-products'], function (RouteBuilder $pluginRoutes):void { | ||||
|            $pluginRoutes->fallbacks(); | ||||
|  |  | |||
|  | @ -5,4 +5,12 @@ namespace TestApp\Controller; | |||
| use Cake\Controller\Controller; | ||||
| 
 | ||||
| class AppController extends Controller { | ||||
|     /** | ||||
|      * @return void | ||||
|      */ | ||||
|     public function initialize(): void { | ||||
|         parent::initialize(); | ||||
| 
 | ||||
|         $this->loadComponent('Flash'); | ||||
|     } | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue