0

i'm currently working on a php-framework to abstract and make it fun and easy to work with an external software ($ES) my company consults. My approach is the hexagonal design pattern, which works great so far. My only concern is mapping (and where to map) entities from $ES to our internal structure.

Example: $externalSoftwareProduct (kind of a god class which handles everything) is mapped to $internalFrameworkProduct (and many other classes to split responsibilities). This happens in Repositories. In every repository method, i collect those entities from $ES and do

new $internalFrameworkProduct(some arguments here coming from 
$externalSoftwareProduct)

foreach of my collected entities which then gets returned. In those repositories there are only generic methods, like getById, getAll, you name it.

Now we use this framework in a customer project and extend those base classes with domain specific extension, like CustomerNameProductRepository. There you find domain specific methods like getProductsCustomerAlwaysNeeds and so on. At the end of those methods, we map the $internalFrameworkProduct to $customerSpecificProduct which holds data for easier access, which is needed. A method in this specific repository looks like this.

public function getProductsCustomerAlwaysNeeds()
{
   $dataStuff = parent::getSomeStuff();
   /** @var internalFrameworkProduct[] $products **/
   $products = magic();

   foreach($products as $product)
   {
     $customerProducts[] = $this->getCustomerSpecificProduct($product->getId());
   }

   return $customerProducts;
}

public function getCustomerSpecificProductById(int $productId)
{
   $externalSoftwareProduct = new externalSoftwareProduct($productId)
   $customerSpecificProduct = new CustomerSpecificProduct(some arguments here coming from $externalSoftwareProduct)

   return $customerSpecificProduct;
}

Now this works fine so far. The only problem is in unit tests. We are using phpunit + Mockery. In order to mock those new created instances we have to use mock(overload:externalSoftwareProduct) and mock(overload: CustomerSpecificProduct) which is always a pain (especially if you try to test this with multiple instances, which is needed from time to time).

How would you approach this? There must be a better way to get those 3 Pieces connected (externalSoftwareProduct, internalFrameworkProduct and CustomerSpecificProduct (which extends internalFrameworkProduct)).

I was thinking about using a factory for the CustomerSpecificProduct in order to just mock the factory and let it spit out my Products. But i feel like im overengineering such a simple task.

1 Answers1

0

«.... mapping (and where to map) entities from $ES to our internal structure...»

In the adapter you use to access the external software.

choquero70
  • 3,332
  • 2
  • 24
  • 40