Skip to content

Commit a0ad012

Browse files
committed
MC-6296: Update DCTRequest
Updated unit tests to use data providers
1 parent 2927757 commit a0ad012

File tree

1 file changed

+69
-63
lines changed

1 file changed

+69
-63
lines changed

app/code/Magento/Dhl/Test/Unit/Model/CarrierTest.php

Lines changed: 69 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@ public function testCollectRates()
283283
$this->httpResponse->method('getBody')
284284
->willReturn($responseXml);
285285

286+
$this->coreDateMock->method('date')
287+
->willReturnCallback(function () {
288+
return date(\DATE_RFC3339);
289+
});
290+
286291
$request = $this->objectManager->getObject(RateRequest::class, $requestData);
287292

288293
$reflectionClass = new \ReflectionObject($this->httpClient);
@@ -516,68 +521,104 @@ public function dhlProductsDataProvider() : array
516521
}
517522

518523
/**
519-
* Tests that the built message reference string is of the appropriate format.
524+
* Tests that the built MessageReference string is of the appropriate format.
520525
*
521-
* @expectedException \Magento\Framework\Exception\LocalizedException
522-
* @expectedExceptionMessage Invalid service prefix
526+
* @dataProvider buildMessageReferenceDataProvider
527+
* @param $servicePrefix
523528
* @throws \ReflectionException
524529
*/
525-
public function testBuildMessageReference()
530+
public function testBuildMessageReference($servicePrefix)
526531
{
527532
$method = new \ReflectionMethod($this->model, 'buildMessageReference');
528533
$method->setAccessible(true);
529534

530-
$constPrefixQuote = new \ReflectionClassConstant($this->model, 'SERVICE_PREFIX_QUOTE');
531-
$constPrefixShipval = new \ReflectionClassConstant($this->model, 'SERVICE_PREFIX_SHIPVAL');
532-
$constPrefixTracking = new \ReflectionClassConstant($this->model, 'SERVICE_PREFIX_TRACKING');
533-
534-
$msgRefQuote = $method->invoke($this->model, $constPrefixQuote->getValue());
535-
self::assertGreaterThanOrEqual(28, strlen($msgRefQuote));
536-
self::assertLessThanOrEqual(32, strlen($msgRefQuote));
535+
$messageReference = $method->invoke($this->model, $servicePrefix);
536+
$this->assertGreaterThanOrEqual(28, strlen($messageReference));
537+
$this->assertLessThanOrEqual(32, strlen($messageReference));
538+
}
537539

538-
$msgRefShip = $method->invoke($this->model, $constPrefixShipval->getValue());
539-
self::assertGreaterThanOrEqual(28, strlen($msgRefShip));
540-
self::assertLessThanOrEqual(32, strlen($msgRefShip));
540+
/**
541+
* @return array
542+
*/
543+
public function buildMessageReferenceDataProvider()
544+
{
545+
return [
546+
'quote_prefix' => ['QUOT'],
547+
'shipval_prefix' => ['SHIP'],
548+
'tracking_prefix' => ['TRCK']
549+
];
550+
}
541551

542-
$msgRefTrack = $method->invoke($this->model, $constPrefixTracking->getValue());
543-
self::assertGreaterThanOrEqual(28, strlen($msgRefTrack));
544-
self::assertLessThanOrEqual(32, strlen($msgRefTrack));
552+
/**
553+
* Tests that an exception is thrown when an invalid service prefix is provided.
554+
*
555+
* @expectedException \Magento\Framework\Exception\LocalizedException
556+
* @expectedExceptionMessage Invalid service prefix
557+
*/
558+
public function testBuildMessageReferenceInvalidPrefix()
559+
{
560+
$method = new \ReflectionMethod($this->model, 'buildMessageReference');
561+
$method->setAccessible(true);
545562

546-
$method->invoke($this->model, 'TEST');
563+
$method->invoke($this->model, 'INVALID');
547564
}
548565

549566
/**
550567
* Tests that the built software name string is of the appropriate format.
551568
*
569+
* @dataProvider buildSoftwareNameDataProvider
570+
* @param $productName
552571
* @throws \ReflectionException
553572
*/
554-
public function testBuildSoftwareName()
573+
public function testBuildSoftwareName($productName)
555574
{
556575
$method = new \ReflectionMethod($this->model, 'buildSoftwareName');
557576
$method->setAccessible(true);
558577

559-
$name = $method->invoke($this->model);
560-
self::assertLessThanOrEqual(30, $name);
578+
$this->productMetadataMock->method('getName')->willReturn($productName);
561579

562-
$nameExceedsLength = $method->invoke($this->model);
563-
self::assertLessThanOrEqual(30, $nameExceedsLength);
580+
$softwareName = $method->invoke($this->model);
581+
$this->assertLessThanOrEqual(30, strlen($softwareName));
582+
}
583+
584+
/**
585+
* @return array
586+
*/
587+
public function buildSoftwareNameDataProvider()
588+
{
589+
return [
590+
'valid_length' => ['Magento'],
591+
'exceeds_length' => ['Product_Name_Longer_Than_30_Char']
592+
];
564593
}
565594

566595
/**
567596
* Tests that the built software version string is of the appropriate format.
568597
*
598+
* @dataProvider buildSoftwareVersionProvider
599+
* @param $productVersion
569600
* @throws \ReflectionException
570601
*/
571-
public function testBuildSoftwareVersion()
602+
public function testBuildSoftwareVersion($productVersion)
572603
{
573604
$method = new \ReflectionMethod($this->model, 'buildSoftwareVersion');
574605
$method->setAccessible(true);
575606

576-
$version = $method->invoke($this->model);
577-
self::assertLessThanOrEqual(10, strlen($version));
607+
$this->productMetadataMock->method('getVersion')->willReturn($productVersion);
578608

579-
$versionExceedsLength = $method->invoke($this->model);
580-
self::assertLessThanOrEqual(10, strlen($versionExceedsLength));
609+
$softwareVersion = $method->invoke($this->model);
610+
$this->assertLessThanOrEqual(10, strlen($softwareVersion));
611+
}
612+
613+
/**
614+
* @return array
615+
*/
616+
public function buildSoftwareVersionProvider()
617+
{
618+
return [
619+
'valid_length' => ['2.3.1'],
620+
'exceeds_length' => ['dev-MC-1000']
621+
];
581622
}
582623

583624
/**
@@ -725,21 +766,6 @@ private function getCarrierHelper(): CarrierHelper
725766
return $carrierHelper;
726767
}
727768

728-
/**
729-
* @return MockObject
730-
*/
731-
private function getCoreDate(): MockObject
732-
{
733-
$coreDate = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\DateTime::class)
734-
->disableOriginalConstructor()
735-
->getMock();
736-
$coreDate->method('date')->willReturnCallback(function () {
737-
return date(\DATE_RFC3339);
738-
});
739-
740-
return $coreDate;
741-
}
742-
743769
/**
744770
* @return MockObject
745771
*/
@@ -762,24 +788,4 @@ private function getHttpClientFactory(): MockObject
762788

763789
return $httpClientFactory;
764790
}
765-
766-
/**
767-
* @return MockObject
768-
*/
769-
private function getProductMetadata(): MockObject
770-
{
771-
$productMetadata = $this->createMock(\Magento\Framework\App\ProductMetadata::class);
772-
773-
$productMetadata->method('getName')->willReturnOnConsecutiveCalls(
774-
'Magento',
775-
str_pad('Magento', 24, '_')
776-
);
777-
778-
$productMetadata->method('getVersion')->willReturnOnConsecutiveCalls(
779-
'2.3.1',
780-
'dev-MC-1000'
781-
);
782-
783-
return $productMetadata;
784-
}
785791
}

0 commit comments

Comments
 (0)