From 03ad4f776e0c85e6514989a8b65c0b40b2a6ef51 Mon Sep 17 00:00:00 2001 From: Luca Cermenati Date: Mon, 30 Mar 2020 12:07:26 +0200 Subject: [PATCH 01/10] retrieved new files --- .../Salesforce/MapperBundle/WsdlValidator.php | 155 ++++++++++++++++++ .../MapperBundle/Resources/test.wsdl.xml | 103 ++++++++++++ .../Salesforce/MapperBundle/Stubs/Account.php | 35 ++++ .../Salesforce/MapperBundle/Stubs/Contact.php | 18 ++ .../Salesforce/MapperBundle/Stubs/User.php | 23 +++ .../MapperBundle/WsdlValidatorTestCase.php | 32 ++++ .../MapperBundle/WsdlValidatorUnitTest.php | 97 +++++++++++ 7 files changed, 463 insertions(+) create mode 100644 src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php create mode 100644 tests/LogicItLab/Salesforce/MapperBundle/Resources/test.wsdl.xml create mode 100644 tests/LogicItLab/Salesforce/MapperBundle/Stubs/Account.php create mode 100644 tests/LogicItLab/Salesforce/MapperBundle/Stubs/Contact.php create mode 100644 tests/LogicItLab/Salesforce/MapperBundle/Stubs/User.php create mode 100644 tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php create mode 100644 tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorUnitTest.php diff --git a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php new file mode 100644 index 0000000..7ba5f15 --- /dev/null +++ b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php @@ -0,0 +1,155 @@ +annotationReader = $annotationReader; + $this->baseProjectDir = $baseProjectDir; + $this->modelDirPath = $modelDirPath; + $this->wsdlPath = $wsdlPath; + } + + public function validate(): array + { + $missingFields = []; + + foreach ($this->getAllClassAnnotations() as $annotation) { + $objectName = $annotation['object']->name; + $fieldNames = $this->getFieldNames($annotation['fields']); + + if (!$objectName || !$fieldNames) { + continue; + } + + foreach ($fieldNames as $fieldName) { + if ($this->hasField($objectName, $fieldName) === false) { + $missingFields[$objectName][] = $fieldName; + } + } + } + + return $missingFields; + } + + private function getObject(string $objectName): ?string + { + preg_match("/([\w\W]*)<\/complexType>/", $this->getWsdlContents(), $matches); + + return $matches[0] ?? null; + } + + private function hasField(string $objectName, string $fieldName): bool + { + return strpos($this->getObject($objectName), sprintf('wsdlContents) { + $this->wsdlContents = file_get_contents($this->wsdlPath); + } + + return $this->wsdlContents; + } + + private function getAllClassAnnotations(): array + { + return array_map(function ($className) { + return $this->annotationReader->getSalesforceProperties($className); + }, $this->getAllClassNames()); + } + + private function getAllClassNames(): array + { + $classNames = []; + $AllFiles = Finder::create()->files()->in($this->baseProjectDir . $this->modelDirPath)->name('*.php'); + foreach ($AllFiles as $file) { + $classNames[] = $this->getClassNameFromFile($file); + } + + return $this->filterNonExistentClasses($classNames); + } + + private function getClassNameFromFile($file): string + { + $realPath = $file->getRealpath(); + $fileName = str_replace($this->baseProjectDir, '', $realPath); + $className = str_replace('.php', '', $fileName); + + if (strpos($this->baseProjectDir, 'test') !== false) { + $className = "Tests/$className"; + } + + return str_replace('/', '\\', $className); + } + + private function filterNonExistentClasses(array $classNames): array + { + $classNames = array_filter($classNames, function ($className) { + return class_exists($className); + }); + + return $classNames; + } + + private function getFieldNames(?array $mapFieldAnnotation): ?array + { + if (!$mapFieldAnnotation) { + return null; + } + + $fieldNames = []; + foreach ($mapFieldAnnotation as $propertyName => $annotation) { + $fieldNames[] = $annotation->name; + } + + return $fieldNames; + } + + private function sortByObjectName(array $missingFields) + { + ksort($missingFields); + + return $missingFields; + } + + public function buildErrorMessage(array $missingFields): string + { + $list = "These objects or fields are missing in wsdl:"; + foreach ($this->sortByObjectName($missingFields) as $objectName => $fields) { + foreach ($fields as $field) { + $list .= "\n$objectName -> $field"; + } + } + + return $list; + } +} \ No newline at end of file diff --git a/tests/LogicItLab/Salesforce/MapperBundle/Resources/test.wsdl.xml b/tests/LogicItLab/Salesforce/MapperBundle/Resources/test.wsdl.xml new file mode 100644 index 0000000..3b90ae9 --- /dev/null +++ b/tests/LogicItLab/Salesforce/MapperBundle/Resources/test.wsdl.xml @@ -0,0 +1,103 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Sforce SOAP API + + + + + \ No newline at end of file diff --git a/tests/LogicItLab/Salesforce/MapperBundle/Stubs/Account.php b/tests/LogicItLab/Salesforce/MapperBundle/Stubs/Account.php new file mode 100644 index 0000000..46758c1 --- /dev/null +++ b/tests/LogicItLab/Salesforce/MapperBundle/Stubs/Account.php @@ -0,0 +1,35 @@ +assertInstanceOf(WsdlValidator::class, $this->wsdlValidator, "Validator should be booted before run a test."); + + $missingFields = $this->wsdlValidator->validate(); + $this->assertEmpty($missingFields, $this->wsdlValidator->buildErrorMessage($missingFields)); + } + + public function bootValidator(string $baseProjectDir, string $modelDirPath, string $wsdlPath) + { + /** @var AnnotationReader $annotationReader */ + $annotationReader = $this->bootKernel()->getContainer()->get(AnnotationReader::class); + $this->wsdlValidator = new WsdlValidator( + $annotationReader, + $baseProjectDir, + $modelDirPath, + $wsdlPath + ); + } +} \ No newline at end of file diff --git a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorUnitTest.php b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorUnitTest.php new file mode 100644 index 0000000..989367d --- /dev/null +++ b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorUnitTest.php @@ -0,0 +1,97 @@ + [ + 'BillingCity', + 'BillingCountry' + ], + 'Contact' => [ + 'AccountId' + ], + 'User' => [ + 'City' + ] + ]; + + private $expectedErrorMessage = 'These objects or fields are missing in wsdl: +Account -> BillingCity +Account -> BillingCountry +Contact -> AccountId +User -> City'; + + public function setUp(): void + { + $this->annotationReaderMock = $this->createMock(AnnotationReader::class); + $this->wsdlValidator = new WsdlValidator( + $this->annotationReaderMock, + str_replace('LogicItLab/Salesforce/MapperBundle', '', dirname(__FILE__)), + 'LogicItLab/Salesforce/MapperBundle/Stubs', + sprintf('%s/Resources/test.wsdl.xml', dirname(__FILE__)) + ); + } + + public function testValidate() + { + $accountProperties = array( + 'object' => new SObject(['name' => 'Account']), + 'relations' => array(), + 'fields' => [ + new Field(['name' => 'AccountNumber']), + new Field(['name' => 'BillingCity']), + new Field(['name' => 'BillingCountry']), + new Field(['name' => 'Owner']), + ] + ); + + $contactProperties = array( + 'object' => new SObject(['name' => 'Contact']), + 'relations' => array(), + 'fields' => [ + new Field(['name' => 'AccountId']), + ] + ); + + $userProperties = array( + 'object' => new SObject(['name' => 'User']), + 'relations' => array(), + 'fields' => [ + new Field(['name' => 'City']), + new Field(['name' => 'Country']), + ] + ); + + $this->annotationReaderMock->expects($this->exactly(3)) + ->method('getSalesforceProperties') + ->willReturnOnConsecutiveCalls( + $accountProperties, + $contactProperties, + $userProperties + ); + + $missingFields = $this->wsdlValidator->validate(); + + $this->assertEquals($this->expectedMissingFields, $missingFields); + } + + public function testBuildMessage() + { + $this->assertEquals($this->expectedErrorMessage, $this->wsdlValidator->buildErrorMessage($this->expectedMissingFields)); + } +} \ No newline at end of file From cf0a4c8d85e4b5d2ed5becaede1b4aee148474e7 Mon Sep 17 00:00:00 2001 From: Luca Cermenati Date: Mon, 30 Mar 2020 13:56:19 +0200 Subject: [PATCH 02/10] Added methods to test case --- .../MapperBundle/WsdlValidatorTestCase.php | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php index a79a468..902cdd4 100644 --- a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php +++ b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php @@ -4,9 +4,14 @@ use LogicItLab\Salesforce\MapperBundle\Annotation\AnnotationReader; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpKernel\KernelInterface; class WsdlValidatorTestCase extends KernelTestCase { + /** @var KernelInterface */ + protected $bootedKernel; + /** @var WsdlValidator */ private $wsdlValidator = null; @@ -21,7 +26,7 @@ public function testWsdlIsValid() public function bootValidator(string $baseProjectDir, string $modelDirPath, string $wsdlPath) { /** @var AnnotationReader $annotationReader */ - $annotationReader = $this->bootKernel()->getContainer()->get(AnnotationReader::class); + $annotationReader = $this->getService(AnnotationReader::class); $this->wsdlValidator = new WsdlValidator( $annotationReader, $baseProjectDir, @@ -29,4 +34,28 @@ public function bootValidator(string $baseProjectDir, string $modelDirPath, stri $wsdlPath ); } + + public function getService(string $className) + { + return $this->getContainer()->get($className); + } + + public function getParameter(string $parameterName) + { + return $this->getContainer()->getParameter($parameterName); + } + + public function getContainer(): ContainerInterface + { + return $this->getKernel()->getContainer(); + } + + public function getKernel(): KernelInterface + { + if (!$this->bootedKernel) { + $this->bootedKernel = static::bootKernel(); + } + + return $this->bootedKernel; + } } \ No newline at end of file From bd06121e145b10ac64498dbe3b5740405597b08f Mon Sep 17 00:00:00 2001 From: Luca Cermenati Date: Mon, 30 Mar 2020 14:39:18 +0200 Subject: [PATCH 03/10] added namespace prefix --- .../Salesforce/MapperBundle/WsdlValidator.php | 10 +++++++++- .../Salesforce/MapperBundle/WsdlValidatorTestCase.php | 5 +++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php index 7ba5f15..db07ca9 100644 --- a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php +++ b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php @@ -19,6 +19,9 @@ class WsdlValidator /** @var string */ private $wsdlPath; + /** @var string */ + private $namespacePrefix; + /** @var string */ private $wsdlContents; @@ -29,12 +32,13 @@ class WsdlValidator * @param string $wsdlPath * @codeCoverageIgnore */ - public function __construct(AnnotationReader $annotationReader, string $baseProjectDir, string $modelDirPath, string $wsdlPath) + public function __construct(AnnotationReader $annotationReader, string $baseProjectDir, string $modelDirPath, string $wsdlPath, string $namespacePrefix = "") { $this->annotationReader = $annotationReader; $this->baseProjectDir = $baseProjectDir; $this->modelDirPath = $modelDirPath; $this->wsdlPath = $wsdlPath; + $this->namespacePrefix = $namespacePrefix; } public function validate(): array @@ -108,6 +112,10 @@ private function getClassNameFromFile($file): string $className = "Tests/$className"; } + if ($this->namespacePrefix) { + $className = "$this->namespacePrefix/$className"; + } + return str_replace('/', '\\', $className); } diff --git a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php index 902cdd4..1a2f2ac 100644 --- a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php +++ b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php @@ -23,7 +23,7 @@ public function testWsdlIsValid() $this->assertEmpty($missingFields, $this->wsdlValidator->buildErrorMessage($missingFields)); } - public function bootValidator(string $baseProjectDir, string $modelDirPath, string $wsdlPath) + public function bootValidator(string $baseProjectDir, string $modelDirPath, string $wsdlPath, string $namespacePrefix = "") { /** @var AnnotationReader $annotationReader */ $annotationReader = $this->getService(AnnotationReader::class); @@ -31,7 +31,8 @@ public function bootValidator(string $baseProjectDir, string $modelDirPath, stri $annotationReader, $baseProjectDir, $modelDirPath, - $wsdlPath + $wsdlPath, + $namespacePrefix ); } From d0ce53a7ce7ea01dca5d3632c88b690fcb5e925e Mon Sep 17 00:00:00 2001 From: Luca Cermenati Date: Mon, 30 Mar 2020 20:02:52 +0200 Subject: [PATCH 04/10] excluded path from constructor, used namespace for build class name --- .../Salesforce/MapperBundle/WsdlValidator.php | 48 ++++++-------- .../MapperBundle/WsdlValidatorTestCase.php | 42 +++++++++++++ .../MapperBundle/WsdlValidatorTestCase.php | 62 ------------------- .../WsdlValidatorTestCaseTest.php | 30 +++++++++ .../MapperBundle/WsdlValidatorUnitTest.php | 13 ++-- ..._MapperBundle_KernelTestDebugContainer.php | 0 6 files changed, 97 insertions(+), 98 deletions(-) create mode 100644 src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php delete mode 100644 tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php create mode 100644 tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php create mode 100644 var/cache/test/MapperBundleLogicItLab_Salesforce_MapperBundle_KernelTestDebugContainer.php diff --git a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php index db07ca9..0afea7b 100644 --- a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php +++ b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php @@ -10,39 +10,28 @@ class WsdlValidator /** @var AnnotationReader */ private $annotationReader; - /** @var string */ - private $baseProjectDir; - /** @var string */ private $modelDirPath; /** @var string */ private $wsdlPath; - /** @var string */ - private $namespacePrefix; - /** @var string */ private $wsdlContents; /** * @param AnnotationReader $annotationReader - * @param string $baseProjectDir - * @param string $modelDirPath - * @param string $wsdlPath * @codeCoverageIgnore */ - public function __construct(AnnotationReader $annotationReader, string $baseProjectDir, string $modelDirPath, string $wsdlPath, string $namespacePrefix = "") + public function __construct(AnnotationReader $annotationReader) { $this->annotationReader = $annotationReader; - $this->baseProjectDir = $baseProjectDir; - $this->modelDirPath = $modelDirPath; - $this->wsdlPath = $wsdlPath; - $this->namespacePrefix = $namespacePrefix; } - public function validate(): array + public function validate(string $modelDirPath, string $wsdlPath): array { + $this->modelDirPath = $modelDirPath; + $this->wsdlPath = $wsdlPath; $missingFields = []; foreach ($this->getAllClassAnnotations() as $annotation) { @@ -94,29 +83,31 @@ private function getAllClassAnnotations(): array private function getAllClassNames(): array { $classNames = []; - $AllFiles = Finder::create()->files()->in($this->baseProjectDir . $this->modelDirPath)->name('*.php'); - foreach ($AllFiles as $file) { - $classNames[] = $this->getClassNameFromFile($file); + $allFiles = Finder::create()->files()->in($this->modelDirPath)->name('*.php'); + foreach ($allFiles as $file) { + $namespace = $this->getNamespaceFromFile($file); + $className = $this->getClassNameFromFile($file); + + $classNames[] = "$namespace\\$className"; } return $this->filterNonExistentClasses($classNames); } - private function getClassNameFromFile($file): string + private function getNamespaceFromFile(\SplFileInfo $file) { - $realPath = $file->getRealpath(); - $fileName = str_replace($this->baseProjectDir, '', $realPath); - $className = str_replace('.php', '', $fileName); + $fileText = file_get_contents($file->getRealPath()); - if (strpos($this->baseProjectDir, 'test') !== false) { - $className = "Tests/$className"; + if (preg_match('#^namespace\s+(.+?);$#sm', $fileText, $matches)) { + return str_replace('/', '\\', $matches[1]); } - if ($this->namespacePrefix) { - $className = "$this->namespacePrefix/$className"; - } + return null; + } - return str_replace('/', '\\', $className); + private function getClassNameFromFile(\SplFileInfo $file): string + { + return str_replace('.php', '', $file->getFileName()); } private function filterNonExistentClasses(array $classNames): array @@ -145,7 +136,6 @@ private function getFieldNames(?array $mapFieldAnnotation): ?array private function sortByObjectName(array $missingFields) { ksort($missingFields); - return $missingFields; } diff --git a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php new file mode 100644 index 0000000..1714f57 --- /dev/null +++ b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php @@ -0,0 +1,42 @@ +getContainer()->get($className); + } + + public function getParameter(string $parameterName) + { + return $this->getContainer()->getParameter($parameterName); + } + + private function getContainer(): ContainerInterface + { + return $this->getKernel()->getContainer(); + } + + private function getKernel(): KernelInterface + { + if (!$this->bootedKernel) { + $this->bootedKernel = $this->bootKernel(); + } + + return $this->bootedKernel; + } +} \ No newline at end of file diff --git a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php deleted file mode 100644 index 1a2f2ac..0000000 --- a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php +++ /dev/null @@ -1,62 +0,0 @@ -assertInstanceOf(WsdlValidator::class, $this->wsdlValidator, "Validator should be booted before run a test."); - - $missingFields = $this->wsdlValidator->validate(); - $this->assertEmpty($missingFields, $this->wsdlValidator->buildErrorMessage($missingFields)); - } - - public function bootValidator(string $baseProjectDir, string $modelDirPath, string $wsdlPath, string $namespacePrefix = "") - { - /** @var AnnotationReader $annotationReader */ - $annotationReader = $this->getService(AnnotationReader::class); - $this->wsdlValidator = new WsdlValidator( - $annotationReader, - $baseProjectDir, - $modelDirPath, - $wsdlPath, - $namespacePrefix - ); - } - - public function getService(string $className) - { - return $this->getContainer()->get($className); - } - - public function getParameter(string $parameterName) - { - return $this->getContainer()->getParameter($parameterName); - } - - public function getContainer(): ContainerInterface - { - return $this->getKernel()->getContainer(); - } - - public function getKernel(): KernelInterface - { - if (!$this->bootedKernel) { - $this->bootedKernel = static::bootKernel(); - } - - return $this->bootedKernel; - } -} \ No newline at end of file diff --git a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php new file mode 100644 index 0000000..f494882 --- /dev/null +++ b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php @@ -0,0 +1,30 @@ +wsdlValidatorTestCase = new WsdlValidatorTestCase(); + } + + public function testBuildValidator() + { + sprintf('%s/Stubs', dirname(__FILE__)); + sprintf('%s/Resources/test.wsdl.xml', dirname(__FILE__)); + + $this->assertInstanceOf(WsdlValidator::class, $this->buildValidator()); + } +} \ No newline at end of file diff --git a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorUnitTest.php b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorUnitTest.php index 989367d..6b003bc 100644 --- a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorUnitTest.php +++ b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorUnitTest.php @@ -39,16 +39,12 @@ class WsdlValidatorUnitTest extends TestCase public function setUp(): void { $this->annotationReaderMock = $this->createMock(AnnotationReader::class); - $this->wsdlValidator = new WsdlValidator( - $this->annotationReaderMock, - str_replace('LogicItLab/Salesforce/MapperBundle', '', dirname(__FILE__)), - 'LogicItLab/Salesforce/MapperBundle/Stubs', - sprintf('%s/Resources/test.wsdl.xml', dirname(__FILE__)) - ); + $this->wsdlValidator = new WsdlValidator($this->annotationReaderMock); } public function testValidate() { + $accountProperties = array( 'object' => new SObject(['name' => 'Account']), 'relations' => array(), @@ -85,7 +81,10 @@ public function testValidate() $userProperties ); - $missingFields = $this->wsdlValidator->validate(); + $missingFields = $this->wsdlValidator->validate( + sprintf('%s/Stubs', dirname(__FILE__)), + sprintf('%s/Resources/test.wsdl.xml', dirname(__FILE__)) + ); $this->assertEquals($this->expectedMissingFields, $missingFields); } diff --git a/var/cache/test/MapperBundleLogicItLab_Salesforce_MapperBundle_KernelTestDebugContainer.php b/var/cache/test/MapperBundleLogicItLab_Salesforce_MapperBundle_KernelTestDebugContainer.php new file mode 100644 index 0000000..e69de29 From b05913623031e4b0ad32de81c40028e4852a0b4d Mon Sep 17 00:00:00 2001 From: Luca Cermenati Date: Mon, 30 Mar 2020 20:29:48 +0200 Subject: [PATCH 05/10] marked test as incomplete --- .../MapperBundle/WsdlValidatorTestCase.php | 2 +- .../WsdlValidatorTestCaseTest.php | 21 +++---------------- 2 files changed, 4 insertions(+), 19 deletions(-) diff --git a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php index 1714f57..66dc080 100644 --- a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php +++ b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php @@ -13,7 +13,7 @@ class WsdlValidatorTestCase extends KernelTestCase public function buildValidator(): WsdlValidator { - + $this->getService(WsdlValidator::class); } private function getService(string $className) diff --git a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php index f494882..e785259 100644 --- a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php +++ b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php @@ -2,29 +2,14 @@ namespace Tests\LogicItLab\Salesforce\MapperBundle; -use LogicItLab\Salesforce\MapperBundle\Annotation\AnnotationReader; -use LogicItLab\Salesforce\MapperBundle\Annotation\Field; -use LogicItLab\Salesforce\MapperBundle\Annotation\SObject; use LogicItLab\Salesforce\MapperBundle\WsdlValidator; -use LogicItLab\Salesforce\MapperBundle\WsdlValidatorTestCase; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -class WsdlValidatorTestCaseTest extends WsdlValidatorTestCase +class WsdlValidatorTestCaseTest extends TestCase { - /** @var WsdlValidatorTestCase */ - private $wsdlValidatorTestCase; - - public function setUp(): void - { - $this->wsdlValidatorTestCase = new WsdlValidatorTestCase(); - } - public function testBuildValidator() { - sprintf('%s/Stubs', dirname(__FILE__)); - sprintf('%s/Resources/test.wsdl.xml', dirname(__FILE__)); - - $this->assertInstanceOf(WsdlValidator::class, $this->buildValidator()); + $this->markTestIncomplete(); + $this->assertInstanceOf(WsdlValidator::class, $this->getWsdlValidator()); } } \ No newline at end of file From 8c3579c563d029b726667992eaa324de76e72869 Mon Sep 17 00:00:00 2001 From: Luca Cermenati Date: Tue, 31 Mar 2020 10:01:08 +0200 Subject: [PATCH 06/10] Used abstract data provider in test --- .../MapperBundle/WsdlValidatorTestCase.php | 29 +++-- .../MapperBundle/Resources/test.full.wsdl.xml | 117 ++++++++++++++++++ .../Salesforce/MapperBundle/Stubs/Contact.php | 1 - .../WsdlValidatorTestCaseTest.php | 14 ++- .../MapperBundle/WsdlValidatorUnitTest.php | 18 +-- 5 files changed, 156 insertions(+), 23 deletions(-) create mode 100644 tests/LogicItLab/Salesforce/MapperBundle/Resources/test.full.wsdl.xml diff --git a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php index 66dc080..f2c049b 100644 --- a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php +++ b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php @@ -6,19 +6,24 @@ use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpKernel\KernelInterface; -class WsdlValidatorTestCase extends KernelTestCase +abstract class WsdlValidatorTestCase extends KernelTestCase { /** @var KernelInterface */ protected $bootedKernel; - public function buildValidator(): WsdlValidator - { - $this->getService(WsdlValidator::class); - } + public abstract function modelAndWsdlDataProvider(): array; - private function getService(string $className) + /** + * @param $modelsDir + * @param $wsdlPath + * @dataProvider modelAndWsdlDataProvider + */ + public function testWsdlIsValid($modelsDir, $wsdlPath) { - return $this->getContainer()->get($className); + $this->markTestIncomplete(); + + $validator = $this->buildValidator(); + $this->assertEmpty($validator->validate($modelsDir, $wsdlPath)); } public function getParameter(string $parameterName) @@ -26,6 +31,16 @@ public function getParameter(string $parameterName) return $this->getContainer()->getParameter($parameterName); } + private function buildValidator(): WsdlValidator + { + $this->getService(WsdlValidator::class); + } + + private function getService(string $className) + { + return $this->getContainer()->get($className); + } + private function getContainer(): ContainerInterface { return $this->getKernel()->getContainer(); diff --git a/tests/LogicItLab/Salesforce/MapperBundle/Resources/test.full.wsdl.xml b/tests/LogicItLab/Salesforce/MapperBundle/Resources/test.full.wsdl.xml new file mode 100644 index 0000000..ef1e01c --- /dev/null +++ b/tests/LogicItLab/Salesforce/MapperBundle/Resources/test.full.wsdl.xml @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Sforce SOAP API + + + + + \ No newline at end of file diff --git a/tests/LogicItLab/Salesforce/MapperBundle/Stubs/Contact.php b/tests/LogicItLab/Salesforce/MapperBundle/Stubs/Contact.php index 687cd45..8e2a485 100644 --- a/tests/LogicItLab/Salesforce/MapperBundle/Stubs/Contact.php +++ b/tests/LogicItLab/Salesforce/MapperBundle/Stubs/Contact.php @@ -2,7 +2,6 @@ namespace Tests\LogicItLab\Salesforce\MapperBundle\Stubs; -use DateTime; use LogicItLab\Salesforce\MapperBundle\Annotation as Salesforce; /** diff --git a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php index e785259..3dd7d2e 100644 --- a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php +++ b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php @@ -2,14 +2,16 @@ namespace Tests\LogicItLab\Salesforce\MapperBundle; -use LogicItLab\Salesforce\MapperBundle\WsdlValidator; -use PHPUnit\Framework\TestCase; +use LogicItLab\Salesforce\MapperBundle\WsdlValidatorTestCase; -class WsdlValidatorTestCaseTest extends TestCase +class WsdlValidatorTestCaseTest extends WsdlValidatorTestCase { - public function testBuildValidator() + public function modelAndWsdlDataProvider(): array { - $this->markTestIncomplete(); - $this->assertInstanceOf(WsdlValidator::class, $this->getWsdlValidator()); + return [ + [ sprintf('%s/Stubs', dirname(__FILE__)), + sprintf('%s/Resources/test.full.wsdl.xml', dirname(__FILE__)) + ] + ]; } } \ No newline at end of file diff --git a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorUnitTest.php b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorUnitTest.php index 6b003bc..77b9e33 100644 --- a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorUnitTest.php +++ b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorUnitTest.php @@ -45,33 +45,33 @@ public function setUp(): void public function testValidate() { - $accountProperties = array( + $accountProperties = [ 'object' => new SObject(['name' => 'Account']), - 'relations' => array(), + 'relations' => [], 'fields' => [ new Field(['name' => 'AccountNumber']), new Field(['name' => 'BillingCity']), new Field(['name' => 'BillingCountry']), new Field(['name' => 'Owner']), ] - ); + ]; - $contactProperties = array( + $contactProperties = [ 'object' => new SObject(['name' => 'Contact']), - 'relations' => array(), + 'relations' => [], 'fields' => [ new Field(['name' => 'AccountId']), ] - ); + ]; - $userProperties = array( + $userProperties = [ 'object' => new SObject(['name' => 'User']), - 'relations' => array(), + 'relations' => [], 'fields' => [ new Field(['name' => 'City']), new Field(['name' => 'Country']), ] - ); + ]; $this->annotationReaderMock->expects($this->exactly(3)) ->method('getSalesforceProperties') From 55d86fe21274f1338fd335b342eace006f22fd8e Mon Sep 17 00:00:00 2001 From: Luca Cermenati Date: Tue, 31 Mar 2020 11:04:49 +0200 Subject: [PATCH 07/10] Avoided Kernel use for test case --- .../MapperBundle/WsdlValidatorTestCase.php | 42 +++---------------- ..._MapperBundle_KernelTestDebugContainer.php | 0 2 files changed, 6 insertions(+), 36 deletions(-) delete mode 100644 var/cache/test/MapperBundleLogicItLab_Salesforce_MapperBundle_KernelTestDebugContainer.php diff --git a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php index f2c049b..8db155a 100644 --- a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php +++ b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php @@ -2,15 +2,11 @@ namespace LogicItLab\Salesforce\MapperBundle; -use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; -use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\HttpKernel\KernelInterface; +use LogicItLab\Salesforce\MapperBundle\Annotation\AnnotationReader; +use PHPUnit\Framework\TestCase; -abstract class WsdlValidatorTestCase extends KernelTestCase +abstract class WsdlValidatorTestCase extends TestCase { - /** @var KernelInterface */ - protected $bootedKernel; - public abstract function modelAndWsdlDataProvider(): array; /** @@ -20,38 +16,12 @@ public abstract function modelAndWsdlDataProvider(): array; */ public function testWsdlIsValid($modelsDir, $wsdlPath) { - $this->markTestIncomplete(); - - $validator = $this->buildValidator(); - $this->assertEmpty($validator->validate($modelsDir, $wsdlPath)); - } - - public function getParameter(string $parameterName) - { - return $this->getContainer()->getParameter($parameterName); + $this->assertEmpty($this->buildValidator()->validate($modelsDir, $wsdlPath)); } private function buildValidator(): WsdlValidator { - $this->getService(WsdlValidator::class); - } - - private function getService(string $className) - { - return $this->getContainer()->get($className); - } - - private function getContainer(): ContainerInterface - { - return $this->getKernel()->getContainer(); - } - - private function getKernel(): KernelInterface - { - if (!$this->bootedKernel) { - $this->bootedKernel = $this->bootKernel(); - } - - return $this->bootedKernel; + $annotationReader = new AnnotationReader(new \Doctrine\Common\Annotations\AnnotationReader()); + return new WsdlValidator($annotationReader); } } \ No newline at end of file diff --git a/var/cache/test/MapperBundleLogicItLab_Salesforce_MapperBundle_KernelTestDebugContainer.php b/var/cache/test/MapperBundleLogicItLab_Salesforce_MapperBundle_KernelTestDebugContainer.php deleted file mode 100644 index e69de29..0000000 From d330d1edffd0fe40f81a1ccb930f148eb8baea5d Mon Sep 17 00:00:00 2001 From: Luca Cermenati Date: Tue, 31 Mar 2020 11:16:39 +0200 Subject: [PATCH 08/10] restored error message --- src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php | 1 + .../Salesforce/MapperBundle/WsdlValidatorTestCase.php | 5 ++++- .../Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php index 0afea7b..63fd443 100644 --- a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php +++ b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php @@ -136,6 +136,7 @@ private function getFieldNames(?array $mapFieldAnnotation): ?array private function sortByObjectName(array $missingFields) { ksort($missingFields); + return $missingFields; } diff --git a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php index 8db155a..8014776 100644 --- a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php +++ b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCase.php @@ -16,12 +16,15 @@ public abstract function modelAndWsdlDataProvider(): array; */ public function testWsdlIsValid($modelsDir, $wsdlPath) { - $this->assertEmpty($this->buildValidator()->validate($modelsDir, $wsdlPath)); + $missingFields = $this->buildValidator()->validate($modelsDir, $wsdlPath); + + $this->assertEmpty($missingFields, $this->buildValidator()->buildErrorMessage($missingFields)); } private function buildValidator(): WsdlValidator { $annotationReader = new AnnotationReader(new \Doctrine\Common\Annotations\AnnotationReader()); + return new WsdlValidator($annotationReader); } } \ No newline at end of file diff --git a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php index 3dd7d2e..de438d5 100644 --- a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php +++ b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php @@ -10,7 +10,7 @@ public function modelAndWsdlDataProvider(): array { return [ [ sprintf('%s/Stubs', dirname(__FILE__)), - sprintf('%s/Resources/test.full.wsdl.xml', dirname(__FILE__)) + sprintf('%s/Resources/test.wsdl.xml', dirname(__FILE__)) ] ]; } From 944ec665c6cf28c84fba9008958954125e565d5d Mon Sep 17 00:00:00 2001 From: Luca Cermenati Date: Tue, 31 Mar 2020 15:38:14 +0200 Subject: [PATCH 09/10] validate to accept array of dir --- src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php | 9 +++++++-- .../MapperBundle/WsdlValidatorTestCaseTest.php | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php index 63fd443..ab41ed8 100644 --- a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php +++ b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php @@ -10,7 +10,7 @@ class WsdlValidator /** @var AnnotationReader */ private $annotationReader; - /** @var string */ + /** @var string|string[] */ private $modelDirPath; /** @var string */ @@ -28,7 +28,12 @@ public function __construct(AnnotationReader $annotationReader) $this->annotationReader = $annotationReader; } - public function validate(string $modelDirPath, string $wsdlPath): array + /** + * @param string|string[] $modelDirPath + * @param string $wsdlPath + * @return array + */ + public function validate($modelDirPath, string $wsdlPath): array { $this->modelDirPath = $modelDirPath; $this->wsdlPath = $wsdlPath; diff --git a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php index de438d5..3dd7d2e 100644 --- a/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php +++ b/tests/LogicItLab/Salesforce/MapperBundle/WsdlValidatorTestCaseTest.php @@ -10,7 +10,7 @@ public function modelAndWsdlDataProvider(): array { return [ [ sprintf('%s/Stubs', dirname(__FILE__)), - sprintf('%s/Resources/test.wsdl.xml', dirname(__FILE__)) + sprintf('%s/Resources/test.full.wsdl.xml', dirname(__FILE__)) ] ]; } From bd8ca8ed504257e343b40b05330897a9b227afa0 Mon Sep 17 00:00:00 2001 From: Luca Cermenati Date: Tue, 31 Mar 2020 15:39:43 +0200 Subject: [PATCH 10/10] fixed some return type --- src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php index ab41ed8..719aa53 100644 --- a/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php +++ b/src/LogicItLab/Salesforce/MapperBundle/WsdlValidator.php @@ -99,7 +99,7 @@ private function getAllClassNames(): array return $this->filterNonExistentClasses($classNames); } - private function getNamespaceFromFile(\SplFileInfo $file) + private function getNamespaceFromFile(\SplFileInfo $file): ?string { $fileText = file_get_contents($file->getRealPath()); @@ -138,7 +138,7 @@ private function getFieldNames(?array $mapFieldAnnotation): ?array return $fieldNames; } - private function sortByObjectName(array $missingFields) + private function sortByObjectName(array $missingFields): array { ksort($missingFields);