Skip to content

Commit

Permalink
Fix for some errors in the prepareData method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Dambacher committed Jul 15, 2016
1 parent 85b766a commit 2720b81
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 95 deletions.
10 changes: 7 additions & 3 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('coffee_bike_collmex');

// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
$rootNote
->children()
->scalarNode('user')->end()
->scalarNode('password')->end()
->scalarNode('customer_id')->end()
->end()
;

return $treeBuilder;
}
Expand Down
10 changes: 9 additions & 1 deletion Entity/CollmexObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ public function setData($aTemplate)

public function getData()
{
return $this;
$data = array();

foreach ($this as $key => $value) {
if ($key != 'extraInfo' && $key != 'id') {
$data[$key] = $value;
}
}

return $data;
}

public function setField($key, $value)
Expand Down
6 changes: 3 additions & 3 deletions Entity/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

class Request
{
private $data;
private $data = array();

public function __construct($data)
{
$this->data = $data;
Expand All @@ -26,7 +26,7 @@ public function setData($data)
{
$this->data = $data;
}

public function getData()
{
return $this->data;
Expand Down
11 changes: 7 additions & 4 deletions Entity/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ public function addObject($aData)
case 'PRDGRP':
$object = new ProductGroup();
break;
case 'NEW_OBJECT_ID';
break;
default:
die('Entity not mapped in CollmexBundle!');

}

$object->setData($aData);
}

$this->objects[] = $object;
if (isset($object)) {
$object->setData($aData);
$this->objects[] = $object;
}
}

public function getObjects()
Expand Down
35 changes: 24 additions & 11 deletions Services/CollmexManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function send(Request $request)
"https://www.collmex.de/cgi-bin/cgi.exe?" . $this->credentials['customerId'] . ",0,data_exchange"
);
cURL_setopt($curl, CURLOPT_POST, 1);
cURL_setopt($curl, CURLOPT_POSTFIELDS, $this->prepareData($request));
cURL_setopt($curl, CURLOPT_POSTFIELDS, $this->prepareData($request->getData()));
cURL_setopt($curl, CURLOPT_HTTPHEADER, Array("Content-Type: text/csv"));
cURL_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
cURL_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
Expand Down Expand Up @@ -95,22 +95,25 @@ public function prepareData($data)
{
$strCSV = "LOGIN;" . $this->credentials['user'] . ";" . $this->credentials['password'] . "\n";

// TODO: Remove duplicated code!
if (is_array($data)) {
foreach ($data as $obj) {
foreach ($obj->getData() as $field) {
if ($this->containsOnlyObjects($data)) {
foreach ($data as $obj) {
foreach ($obj->getData() as $field) {
$strCSV .= $field . ";";
}
$strCSV .= "\n";
}
$strCSV = substr($strCSV, 0, -2); // Delete \n from CSV
} else {
foreach ($data as $field) {
$strCSV .= $field . ";";
}
$strCSV .= "\n";
}
} else {
foreach ($data->getData() as $field) {
$strCSV .= $field . ";";
}
$strCSV .= "\n";
}

$strCSV = substr($strCSV, 0, -2); // Delete \n from CSV
}

return utf8_decode($strCSV);
}
Expand Down Expand Up @@ -185,7 +188,7 @@ public function getInvoice($invoiceNo = null, $customerNo = null, $from = null,

return $response->getObjects()[0];
}

public function getInvoices($customerNo = null, $from = null, $to = null, $onlyIssued = 0, $onlyModified = 0, $onlyCreatedWithThisAPI = 0, $companyId = 1)
{
$request = new Request([
Expand All @@ -208,7 +211,17 @@ public function getInvoices($customerNo = null, $from = null, $to = null, $onlyI
return $response->getObjects();
}


private function containsOnlyObjects($data) {

foreach ($data as $element) {
if (is_object($element)) {
return true;
}
return false;
}
}




}
139 changes: 66 additions & 73 deletions Tests/CollmexManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace CoffeeBike\CollmexBundle\Tests\Services;

use CoffeeBike\CollmexBundle\Models\Invoice;
use CoffeeBike\CollmexBundle\Models\Product;
use CoffeeBike\CollmexBundle\Models\ProductGroup;
use CoffeeBike\CollmexBundle\Models\Request;
use CoffeeBike\CollmexBundle\Models\Response;
use CoffeeBike\CollmexBundle\Entity\Invoice;
use CoffeeBike\CollmexBundle\Entity\Product;
use CoffeeBike\CollmexBundle\Entity\ProductGroup;
use CoffeeBike\CollmexBundle\Entity\Request;
use CoffeeBike\CollmexBundle\Entity\Response;
use CoffeeBike\CollmexBundle\Services\CollmexManager;

class CollmexManagerTest extends \PHPUnit_Framework_TestCase
Expand All @@ -15,7 +15,7 @@ class CollmexManagerTest extends \PHPUnit_Framework_TestCase

public function setUp()
{
$this->cm = new CollmexManager('USER', 'PASSWORD', 'CUSTOMERID');
$this->cm = new CollmexManager('USER', 'PASSWORD', '137944');
}

public function testPrepareData()
Expand All @@ -26,7 +26,7 @@ public function testPrepareData()
'1000',
]);

$data = $this->cm->prepareData($request);
$data = $this->cm->prepareData($request->getData());

$this->assertContains('PRODUCT_GET;1;1000', $data);
}
Expand Down Expand Up @@ -73,7 +73,7 @@ public function testGetProduct()
$product = $this->cm->getProduct(1000);

$this->assertTrue($product instanceof Product);
$this->assertEquals('Coffee-Bike', $product->getField('product_name'));
$this->assertEquals('Test', $product->getField('product_name'));
}

public function testGetProductGroup()
Expand Down Expand Up @@ -107,79 +107,72 @@ public function testCollmexObject()
$this->assertEquals('Test', $group->getField('name'));
}

/*
public function testGetInvoice()
public function testCreateInvoice()
{
$testData = array(
'set_type' => 'INVOICE_GET',
'invoice_id' => null,
'client_id' => null,
'customer_id' => 10623,
'invoice_date_begin' => null,
'invoice_date_end' => null,
'only_sent' => null,
'format' => null,
'only_changed' => null,
'system_name' => null,
'system_generated' => null,
'no_stationery' => null,
);
$invoice = $this->cm->getInvoice($testData);
print_r($invoice);
$invoice = new Invoice();
$invoice->setField('invoice_id', '-1');
$invoice->setField('position', '1');
$invoice->setField('client_id', '1');
$invoice->setField('invoice_type', '0');
$invoice->setField('customer_id', '9999');
$invoice->setField('invoice_text', 'Text');
$invoice->setField('language', '0');
$invoice->setField('system_name', 'CoffeeBikeCollmexBundle');
$invoice->setField('status', '10');
$invoice->setField('position_type', '0');
$invoice->setField('product_id', '1000');
$invoice->setField('quantity', '1');
$invoice->setField('foreign_tax', '0');

$response = $this->cm->send(new Request($invoice));

$this->assertEquals('S', $response->getMessages()[0]->getStatus());
$this->assertEquals('Datenübertragung erfolgreich. Es wurden 1 Datensätze verarbeitet.', $response->getMessages()[0]->getText());
}

public function testProduct()
public function testCreateInvoices()
{
$product = $this->cm->getProducts();
$invoice[0] = new Invoice();
$invoice[0]->setField('invoice_id', '-1');
$invoice[0]->setField('position', '1');
$invoice[0]->setField('client_id', '1');
$invoice[0]->setField('invoice_type', '0');
$invoice[0]->setField('customer_id', '9999');
$invoice[0]->setField('invoice_text', 'Text');
$invoice[0]->setField('language', '0');
$invoice[0]->setField('system_name', 'CoffeeBikeCollmexBundle');
$invoice[0]->setField('status', '10');
$invoice[0]->setField('position_type', '0');
$invoice[0]->setField('product_id', '1000');
$invoice[0]->setField('quantity', '1');
$invoice[0]->setField('foreign_tax', '0');

$invoice[1] = new Invoice();
$invoice[1]->setField('invoice_id', '-1');
$invoice[1]->setField('position', '2');
$invoice[1]->setField('client_id', '1');
$invoice[1]->setField('invoice_type', '0');
$invoice[1]->setField('customer_id', '9999');
$invoice[1]->setField('invoice_text', 'Text');
$invoice[1]->setField('language', '0');
$invoice[1]->setField('system_name', 'CoffeeBikeCollmexBundle');
$invoice[1]->setField('status', '10');
$invoice[1]->setField('position_type', '0');
$invoice[1]->setField('product_id', '1000');
$invoice[1]->setField('quantity', '1');
$invoice[1]->setField('foreign_tax', '0');

$response = $this->cm->send(new Request($invoice));

$this->assertEquals('S', $response->getMessages()[0]->getStatus());
$this->assertEquals('Datenübertragung erfolgreich. Es wurden 2 Datensätze verarbeitet.', $response->getMessages()[0]->getText());
}

public function testRequest()
public function testGetInvoice()
{
$testType = new TestType();
$testType->template = array(
'CUSTOMER_GET',
'9999',
'1',
);
$invoice = $this->cm->getInvoice(1);

// CollmexResult object
$result = $this->cm->sendRequest([$testType]);
$this->assertEquals('MESSAGE', $result->get('type_identifier'));
$this->assertEquals('S', $result->get('status'));
$this->assertNotNull($result->get('code'));
$this->assertNotNull($result->get('text'));
// $this->assertTrue($result->get('line'));
//$this->assertEquals('Allgemeiner Geschäftspartner', $result->getResponse()[0]->get('name'));
$this->assertEquals('Test', $invoice->getField('product_description'));
}

/*
public function testCreateInvoice()
{
$invoice['marketing'] = new Invoice();
$invoice['marketing']->setInvoiceData(array(
'invoice_id' => -1,
'position' => null,
'client_id' => 1,
'customer_id' => 9999, //$sm->getCollmexId($monthlyReport->getStore()->getCompany()),
'invoice_type' => 0,
'invoice_text' => 'Test ÄÜÖß', //TODO: Invoice text
'language' => 0, // TODO: Language
'employee_id' => null,
'system_name' => 'eFIS',
'status' => 10,
'position_type' => 0,
'product_id' => 1, //$monthlyReport->getStore()->getCompany()->getCountry()->getProdNoMarketing(),
'quantity' => 1,
'foreign_tax' => 0,
));
$result = $this->cm->sendRequest($invoice);
}
*/

}

0 comments on commit 2720b81

Please sign in to comment.