Skip to content

Commit a7cb7b0

Browse files
committed
add more tests
1 parent e4dae5e commit a7cb7b0

16 files changed

+613
-35
lines changed

phpunit.xml.dist

+4
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@
1919
<directory>src</directory>
2020
</whitelist>
2121
</filter>
22+
<php>
23+
<env name="USERNAME" value=""/>
24+
<env name="APIKEY" value=""/>
25+
</php>
2226
</phpunit>

src/Contact/ContactSearchStruct.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function setCustomerNumberFilter(string $customerNumber)
1616
$this->filters['CUSTOMER_NUMBER'] = $customerNumber;
1717
}
1818

19-
public function setContactId(string $contactId)
19+
public function setContactId(int $contactId)
2020
{
2121
$this->filters['CONTACT_ID'] = $contactId;
2222
}

src/Contact/ContactValidator.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ public function validateRequiredUpdateProperties(ContactEntity $entity): array
2323
{
2424
$errorMessages = $this->validateRequiredCreationProperties($entity);
2525

26-
try {
27-
$this->checkCustomerId($entity);
28-
} catch (MissingPropertyException $exception) {
29-
$errorMessages[] = $exception->getMessage();
30-
}
31-
3226
try {
3327
$this->checkContactId($entity);
3428
} catch (MissingPropertyException $exception) {
@@ -67,7 +61,7 @@ private function checkCustomerId(ContactEntity $entity)
6761
private function checkContactId(ContactEntity $entity)
6862
{
6963
if (!$entity->contactId) {
70-
throw new MissingPropertyException('The property customerId is not valid!');
64+
throw new MissingPropertyException('The property contactId is not valid!');
7165
}
7266
}
7367
}

src/Estimate/EstimateItemValidator.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ public function validateEstimateItem(EstimateItemEntity $entity): array
3434
public function checkDescription(EstimateItemEntity $entity)
3535
{
3636
if (!$entity->description) {
37-
throw new MissingPropertyException('The property description is not valid!');
37+
throw new MissingPropertyException($entity->articleNumber . ': The property description is not valid!');
3838
}
3939
}
4040

4141
public function checkUnitPrice(EstimateItemEntity $entity)
4242
{
4343
if (!$entity->unitPrice) {
44-
throw new MissingPropertyException('The property unitPrice is not valid!');
44+
throw new MissingPropertyException($entity->articleNumber . ': The property unitPrice is not valid!');
4545
}
4646
}
4747

4848
public function checkVatPercent(EstimateItemEntity $entity)
4949
{
5050
if (!$entity->vatPercent) {
51-
throw new MissingPropertyException('The property vatPercent is not valid!');
51+
throw new MissingPropertyException($entity->articleNumber . ': The property vatPercent is not valid!');
5252
}
5353
}
5454
}

src/Estimate/EstimateValidator.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,17 @@ private function checkEstimateId(EstimateEntity $entity)
8383
*/
8484
private function checkItems(EstimateEntity $entity)
8585
{
86+
$errorMessages = [];
8687
foreach ($entity->items as $item) {
8788
if (!$item instanceof EstimateItemEntity) {
8889
throw new \InvalidArgumentException('The given item is not a EstimateItemEntity');
8990
}
9091

91-
$this->estimateItemValidator->checkDescription($item);
92-
$this->estimateItemValidator->checkUnitPrice($item);
93-
$this->estimateItemValidator->checkVatPercent($item);
92+
$errorMessages[$item->articleNumber] = $this->estimateItemValidator->validateEstimateItem($item);
93+
}
94+
95+
if (count($errorMessages)) {
96+
throw new MissingPropertyException(implode("\r\n", $errorMessages));
9497
}
9598
}
9699
}

src/Product/ProductSearchStruct.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
class ProductSearchStruct extends AbstractSearchStruct
88
{
9-
public function setArticleNumberIdFilter(int $invoiceId): void
9+
public function setArticleNumberFilter(string $articleNumber): void
1010
{
11-
$this->filters['ARTICLE_NUMBER'] = $invoiceId;
11+
$this->filters['ARTICLE_NUMBER'] = $articleNumber;
1212
}
1313
}

tests/Common/BaseTestTrait.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace FastBillSdkTest\Common;
4+
5+
use FastBillSdk\Api\ApiClient;
6+
use FastBillSdk\Api\ApiClientInterface;
7+
use FastBillSdk\Common\XmlService;
8+
use FastBillSdkTest\Helper\ApiDummyClient;
9+
10+
trait BaseTestTrait
11+
{
12+
public function getApiClient(): ApiClientInterface
13+
{
14+
return new ApiClient(getenv('USERNAME'), getenv('APIKEY'));
15+
}
16+
17+
public function getApiDummyClient(): ApiClientInterface
18+
{
19+
return new ApiDummyClient();
20+
}
21+
22+
public function getXmlService(): XmlService
23+
{
24+
return new XmlService();
25+
}
26+
}

tests/Contact/ContactServiceTest.php

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace FastBillSdkTest\Contact;
4+
5+
use FastBillSdk\Common\MissingPropertyException;
6+
use FastBillSdk\Contact\ContactEntity;
7+
use FastBillSdk\Contact\ContactSearchStruct;
8+
use FastBillSdk\Contact\ContactService;
9+
use FastBillSdk\Contact\ContactValidator;
10+
use FastBillSdkTest\Common\BaseTestTrait;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class ContactServiceTest extends TestCase
14+
{
15+
use BaseTestTrait;
16+
17+
/**
18+
* @var ContactService
19+
*/
20+
private $contactService;
21+
22+
public function getContactService(): ContactService
23+
{
24+
if (!$this->contactService) {
25+
$this->contactService = new ContactService(
26+
$this->getApiClient(),
27+
$this->getXmlService(),
28+
new ContactValidator()
29+
);
30+
}
31+
32+
return $this->contactService;
33+
}
34+
35+
public function getContactServiceWithApiDummy(): ContactService
36+
{
37+
if (!$this->contactService) {
38+
$this->contactService = new ContactService(
39+
$this->getApiDummyClient(),
40+
$this->getXmlService(),
41+
new ContactValidator()
42+
);
43+
}
44+
45+
return $this->contactService;
46+
}
47+
48+
public function testGetContacts()
49+
{
50+
foreach ($this->getContactService()->getContact(new ContactSearchStruct()) as $contact) {
51+
self::assertInstanceOf(ContactEntity::class, $contact);
52+
}
53+
}
54+
55+
public function testGetContactsWithFilters()
56+
{
57+
$searchStruct = new ContactSearchStruct();
58+
$searchStruct->setContactId(11);
59+
$searchStruct->setCustomerIdFilter(22);
60+
$searchStruct->setCustomerNumberFilter('KDNR33');
61+
$searchStruct->setSearchTermFilter('Musterfirma');
62+
63+
$contacts = $this->getContactService()->getContact($searchStruct);
64+
65+
self::assertCount(0, $contacts);
66+
}
67+
68+
public function testCreateContactWithEmptyEntity()
69+
{
70+
$contact = new ContactEntity();
71+
72+
$this->expectException(MissingPropertyException::class);
73+
$this->getContactServiceWithApiDummy()->createContact($contact);
74+
}
75+
76+
public function testUpdateContactWithEmptyEntity()
77+
{
78+
$contact = new ContactEntity();
79+
80+
$this->expectException(MissingPropertyException::class);
81+
$this->getContactServiceWithApiDummy()->updateContact($contact);
82+
}
83+
84+
public function testUpdateContact()
85+
{
86+
$contact = new ContactEntity();
87+
$contact->customerId = 111;
88+
$contact->contactId = 222;
89+
90+
self::assertEquals($this->getContactServiceWithApiDummy()->updateContact($contact), $contact);
91+
}
92+
93+
public function testDeleteContactWithEmptyEntity()
94+
{
95+
$contact = new ContactEntity();
96+
97+
$this->expectException(MissingPropertyException::class);
98+
$this->getContactServiceWithApiDummy()->deleteContact($contact);
99+
}
100+
101+
public function testDeleteContact()
102+
{
103+
$contact = new ContactEntity();
104+
$contact->contactId = 1337;
105+
$contact->customerId = 13337;
106+
107+
$this->getContactServiceWithApiDummy()->deleteContact($contact);
108+
109+
self::assertNull($contact->contactId);
110+
}
111+
}
+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace FastBillSdkTest\Customer;
4+
5+
use FastBillSdk\Common\MissingPropertyException;
6+
use FastBillSdk\Customer\CustomerEntity;
7+
use FastBillSdk\Customer\CustomerSearchStruct;
8+
use FastBillSdk\Customer\CustomerService;
9+
use FastBillSdk\Customer\CustomerValidator;
10+
use FastBillSdkTest\Common\BaseTestTrait;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class CustomerServiceTest extends TestCase
14+
{
15+
use BaseTestTrait;
16+
17+
/**
18+
* @var CustomerService
19+
*/
20+
private $customerService;
21+
22+
public function getCustomerService(): CustomerService
23+
{
24+
if (!$this->customerService) {
25+
$this->customerService = new CustomerService(
26+
$this->getApiClient(),
27+
$this->getXmlService(),
28+
new CustomerValidator()
29+
);
30+
}
31+
32+
return $this->customerService;
33+
}
34+
35+
public function getCustomerServiceWithApiDummy(): CustomerService
36+
{
37+
if (!$this->customerService) {
38+
$this->customerService = new CustomerService(
39+
$this->getApiDummyClient(),
40+
$this->getXmlService(),
41+
new CustomerValidator()
42+
);
43+
}
44+
45+
return $this->customerService;
46+
}
47+
48+
public function testGetCustomers()
49+
{
50+
$customers = $this->getCustomerService()->getCustomer(new CustomerSearchStruct());
51+
foreach ($customers as $customer) {
52+
self::assertInstanceOf(CustomerEntity::class, $customer);
53+
}
54+
}
55+
56+
public function testGetCustomersWithFilters()
57+
{
58+
$searchStruct = new CustomerSearchStruct();
59+
$searchStruct->setCustomerIdFilter(22);
60+
$searchStruct->setCustomerNumberFilter('KDNR33');
61+
$searchStruct->setCustomerIdFilter(123);
62+
$searchStruct->setCityFilter('Musterstadt');
63+
$searchStruct->setCountryCodeFilter('DE');
64+
$searchStruct->setSearchTermFilter('Musterfirma');
65+
66+
$customers = $this->getCustomerservice()->getCustomer($searchStruct);
67+
68+
self::assertCount(0, $customers);
69+
}
70+
71+
public function testCreateCustomerWithEmptyEntity()
72+
{
73+
$customer = new CustomerEntity();
74+
$customer->customerType = 'business';
75+
76+
$this->expectException(MissingPropertyException::class);
77+
$this->getCustomerServiceWithApiDummy()->createCustomer($customer);
78+
}
79+
80+
public function testCreateCustomerWithMissingProperties()
81+
{
82+
$customer = new CustomerEntity();
83+
$customer->customerType = 'consumer';
84+
85+
$this->expectException(MissingPropertyException::class);
86+
$this->getCustomerServiceWithApiDummy()->createCustomer($customer);
87+
}
88+
89+
public function testUpdateCustomerWithEmptyEntity()
90+
{
91+
$customer = new CustomerEntity();
92+
93+
$this->expectException(MissingPropertyException::class);
94+
$this->getCustomerServiceWithApiDummy()->updateCustomer($customer);
95+
}
96+
97+
public function testUpdateCustomer()
98+
{
99+
$customer = new CustomerEntity();
100+
$customer->customerId = 111;
101+
$customer->customerType = 'business';
102+
$customer->organization = 'Bullshit Productions';
103+
104+
self::assertEquals($this->getCustomerServiceWithApiDummy()->updateCustomer($customer), $customer);
105+
}
106+
107+
public function testDeleteCustomerWithEmptyEntity()
108+
{
109+
$customer = new CustomerEntity();
110+
111+
$this->expectException(MissingPropertyException::class);
112+
$this->getCustomerServiceWithApiDummy()->deleteCustomer($customer);
113+
}
114+
115+
public function testDeleteCustomer()
116+
{
117+
$customer = new CustomerEntity();
118+
$customer->customerId = 1337;
119+
120+
$this->getCustomerServiceWithApiDummy()->deleteCustomer($customer);
121+
122+
self::assertNull($customer->customerId);
123+
}
124+
}

0 commit comments

Comments
 (0)