Skip to content

Commit 58cfa70

Browse files
committed
LNX-332 - Fail gracefully if setup configuration fails
1 parent 6225d23 commit 58cfa70

2 files changed

Lines changed: 239 additions & 34 deletions

File tree

src/Stackify/Utils/Rum.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,20 @@ public static function getInstance()
6363
*/
6464
public function setupConfiguration($appName, $environment, $rumKey = null, $rumScriptUrl = null, $config = null)
6565
{
66-
$this->appName = $this->validateAppName($appName);
67-
$this->environment = $this->validateEnvironment($environment);
68-
$this->rumScriptUrl = $this->checkRumScriptUrl($rumScriptUrl);
69-
$this->rumKey = $this->checkRumKey($rumKey);
70-
$this->hasSetup = true;
66+
try {
67+
$this->appName = $this->validateAppName($appName);
68+
$this->environment = $this->validateEnvironment($environment);
69+
$this->rumScriptUrl = $this->checkRumScriptUrl($rumScriptUrl);
70+
$this->rumKey = $this->checkRumKey($rumKey);
71+
$this->hasSetup = true;
72+
} catch (\Exception $e) {
73+
$this->logError('Unable to setup RUM Configuration. Something went wrong. Message: %s', $e->getMessage());
74+
// Reset state
75+
$this->appName = null;
76+
$this->environment = null;
77+
$this->rumScriptUrl = self::DEFAULT_RUM_SCRIPT_URL;
78+
$this->rumKey = null;
79+
}
7180

7281
return $this;
7382
}

tests/Utils/UtilsRumTest.php

Lines changed: 225 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -447,24 +447,75 @@ public function testRumInsertScriptWithExceptionAndCallWriteToFileAndErrorLog()
447447
}
448448

449449
public function testRumSetupConfigurationWithEmptyAppName()
450+
{
451+
$appName = '';
452+
$environment = 'test environment';
453+
$rumKey = '`invalid-rum-key';
454+
$mockRumObject = $this->getMockBuilder(Rum::class)
455+
->setMethods(['logError'])
456+
->getMock();
457+
458+
$mockRumObject->expects($this->once())
459+
->method('logError')
460+
->with(
461+
'Unable to setup RUM Configuration. Something went wrong. Message: %s',
462+
'Application Name is empty.'
463+
);
464+
465+
$mockRumObject->setupConfiguration(
466+
$appName,
467+
$environment,
468+
$rumKey
469+
);
470+
471+
$this->assertNull($mockRumObject->getApplicationName());
472+
$this->assertNull($mockRumObject->getEnvironment());
473+
$this->assertSame($mockRumObject->getRumScriptUrl(), self::DEFAULT_RUM_SCRIPT_URL);
474+
$this->assertNull($mockRumObject->getRumKey());
475+
}
476+
477+
public function testRumValidateAppNameWithEmptyAppName()
450478
{
451479
$this->expectException(RumValidationException::class);
452480
$this->expectExceptionMessage('Application Name is empty.');
453481

454482
$ds = DIRECTORY_SEPARATOR;
455483
$appName = '';
456-
$environment = 'test environment';
457-
$rumKey = '`invalid-rum-key';
458484
$mockRumObject = $this->givenARumObjectWithDefaultSetting();
485+
$mockRumObject->validateAppName(
486+
$appName
487+
);
488+
}
489+
490+
public function testRumSetupConfigurationWithEmptyEnvironment()
491+
{
492+
$appName = 'test app name';
493+
$environment = '';
494+
$rumKey = '`invalid-rum-key';
495+
$mockRumObject = $this->getMockBuilder(Rum::class)
496+
->setMethods(['logError'])
497+
->getMock();
498+
499+
$mockRumObject->expects($this->once())
500+
->method('logError')
501+
->with(
502+
'Unable to setup RUM Configuration. Something went wrong. Message: %s',
503+
'Environment is empty.'
504+
);
459505

460506
$mockRumObject->setupConfiguration(
461507
$appName,
462508
$environment,
463509
$rumKey
464510
);
511+
512+
$this->assertNull($mockRumObject->getApplicationName());
513+
$this->assertNull($mockRumObject->getEnvironment());
514+
$this->assertSame($mockRumObject->getRumScriptUrl(), self::DEFAULT_RUM_SCRIPT_URL);
515+
$this->assertNull($mockRumObject->getRumKey());
465516
}
466517

467-
public function testRumSetupConfigurationWithEmptyEnvironment()
518+
public function testRumValidateEnvironmentWithEmptyEnvironment()
468519
{
469520
$this->expectException(RumValidationException::class);
470521
$this->expectExceptionMessage('Environment is empty.');
@@ -475,14 +526,68 @@ public function testRumSetupConfigurationWithEmptyEnvironment()
475526
$rumKey = '`invalid-rum-key';
476527
$mockRumObject = $this->givenARumObjectWithDefaultSetting();
477528

529+
$mockRumObject->validateEnvironment(
530+
$environment
531+
);
532+
}
533+
534+
public function testRumSetupConfigurationWithEmptyRumKey()
535+
{
536+
$appName = 'test app name';
537+
$environment = 'test';
538+
$rumKey = '';
539+
$mockRumObject = $this->getMockBuilder(Rum::class)
540+
->setMethods(['logError'])
541+
->getMock();
542+
543+
$mockRumObject->expects($this->once())
544+
->method('logError')
545+
->with(
546+
'Unable to setup RUM Configuration. Something went wrong. Message: %s',
547+
'RUM Key is empty.'
548+
);
549+
478550
$mockRumObject->setupConfiguration(
479551
$appName,
480552
$environment,
481553
$rumKey
482554
);
555+
556+
$this->assertNull($mockRumObject->getApplicationName());
557+
$this->assertNull($mockRumObject->getEnvironment());
558+
$this->assertSame($mockRumObject->getRumScriptUrl(), self::DEFAULT_RUM_SCRIPT_URL);
559+
$this->assertNull($mockRumObject->getRumKey());
483560
}
484561

485562
public function testRumSetupConfigurationWithInvalidRumKey()
563+
{
564+
$appName = 'test app name';
565+
$environment = 'test';
566+
$rumKey = '`123';
567+
$mockRumObject = $this->getMockBuilder(Rum::class)
568+
->setMethods(['logError'])
569+
->getMock();
570+
571+
$mockRumObject->expects($this->once())
572+
->method('logError')
573+
->with(
574+
'Unable to setup RUM Configuration. Something went wrong. Message: %s',
575+
'RUM Key is in invalid format.'
576+
);
577+
578+
$mockRumObject->setupConfiguration(
579+
$appName,
580+
$environment,
581+
$rumKey
582+
);
583+
584+
$this->assertNull($mockRumObject->getApplicationName());
585+
$this->assertNull($mockRumObject->getEnvironment());
586+
$this->assertSame($mockRumObject->getRumScriptUrl(), self::DEFAULT_RUM_SCRIPT_URL);
587+
$this->assertNull($mockRumObject->getRumKey());
588+
}
589+
590+
public function testRumCheckRumKeyWithInvalidRumKey()
486591
{
487592
$this->expectException(RumValidationException::class);
488593
$this->expectExceptionMessage('RUM Key is in invalid format.');
@@ -493,14 +598,12 @@ public function testRumSetupConfigurationWithInvalidRumKey()
493598
$rumKey = '`invalid-rum-key';
494599
$mockRumObject = $this->givenARumObjectWithDefaultSetting();
495600

496-
$mockRumObject->setupConfiguration(
497-
$appName,
498-
$environment,
601+
$mockRumObject->checkRumKey(
499602
$rumKey
500603
);
501604
}
502605

503-
public function testRumSetupConfigurationWithEmptyRumKey()
606+
public function testRumCheckRumKeyWithEmptyRumKey()
504607
{
505608
$this->expectException(RumValidationException::class);
506609
$this->expectExceptionMessage('RUM Key is empty.');
@@ -511,29 +614,51 @@ public function testRumSetupConfigurationWithEmptyRumKey()
511614
$rumKey = '';
512615
$mockRumObject = $this->givenARumObjectWithDefaultSetting();
513616

617+
$mockRumObject->checkRumKey(
618+
$rumKey
619+
);
620+
}
621+
622+
public function testRumSetupConfigurationWithInvalidRumScriptUrl()
623+
{
624+
$appName = 'test app name';
625+
$environment = 'test';
626+
$rumKey = '123';
627+
$rumScriptUrl = 'invalid-rum-script-url';
628+
$mockRumObject = $this->getMockBuilder(Rum::class)
629+
->setMethods(['logError'])
630+
->getMock();
631+
632+
$mockRumObject->expects($this->once())
633+
->method('logError')
634+
->with(
635+
'Unable to setup RUM Configuration. Something went wrong. Message: %s',
636+
'RUM Script URL is in invalid format.'
637+
);
638+
514639
$mockRumObject->setupConfiguration(
515640
$appName,
516641
$environment,
517-
$rumKey
642+
$rumKey,
643+
$rumScriptUrl
518644
);
645+
646+
$this->assertNull($mockRumObject->getApplicationName());
647+
$this->assertNull($mockRumObject->getEnvironment());
648+
$this->assertSame($mockRumObject->getRumScriptUrl(), self::DEFAULT_RUM_SCRIPT_URL);
649+
$this->assertNull($mockRumObject->getRumKey());
519650
}
520651

521-
public function testRumSetupConfigurationWithInvalidRumScriptUrl()
652+
public function testRumCheckRumScriptUrlWithInvalidRumScriptUrl()
522653
{
523654
$this->expectException(RumValidationException::class);
524655
$this->expectExceptionMessage('RUM Script URL is in invalid format.');
525656

526657
$ds = DIRECTORY_SEPARATOR;
527-
$appName = 'test app name';
528-
$environment = 'test environment';
529-
$rumKey = 'valid-rum-key';
530658
$rumScriptUrl = 'invalid-rum-script-url';
531659
$mockRumObject = $this->givenARumObjectWithDefaultSetting();
532660

533-
$mockRumObject->setupConfiguration(
534-
$appName,
535-
$environment,
536-
$rumKey,
661+
$mockRumObject->checkRumScriptUrl(
537662
$rumScriptUrl
538663
);
539664
}
@@ -576,39 +701,85 @@ public function testRumSetupConfigurationWithValidRumKeyFromEnv()
576701

577702
public function testRumSetupConfigurationWithInvalidRumKeyFromEnv()
578703
{
579-
$this->expectException(RumValidationException::class);
580-
$this->expectExceptionMessage('RUM Key is in invalid format.');
581-
582-
$ds = DIRECTORY_SEPARATOR;
583704
$appName = 'test app name';
584-
$environment = 'test environment';
705+
$environment = 'test';
585706
$_SERVER['RETRACE_RUM_KEY'] = '`invalid-rum-key';
586-
$mockRumObject = $this->givenARumObjectWithDefaultSetting();
707+
$mockRumObject = $this->getMockBuilder(Rum::class)
708+
->setMethods(['logError'])
709+
->getMock();
710+
711+
$mockRumObject->expects($this->once())
712+
->method('logError')
713+
->with(
714+
'Unable to setup RUM Configuration. Something went wrong. Message: %s',
715+
'RUM Key is in invalid format.'
716+
);
587717

588718
$mockRumObject->setupConfiguration(
589719
$appName,
590720
$environment
591721
);
592722

723+
$this->assertNull($mockRumObject->getApplicationName());
724+
$this->assertNull($mockRumObject->getEnvironment());
725+
$this->assertSame($mockRumObject->getRumScriptUrl(), self::DEFAULT_RUM_SCRIPT_URL);
726+
$this->assertNull($mockRumObject->getRumKey());
727+
593728
unset($_SERVER['RETRACE_RUM_KEY']);
594729
}
595730

596-
public function testRumSetupConfigurationWithEmptyRumKeyFromEnv()
731+
public function testRumCheckRumKeyWithInvalidRumKeyFromEnv()
597732
{
598733
$this->expectException(RumValidationException::class);
599-
$this->expectExceptionMessage('RUM Key is empty.');
734+
$this->expectExceptionMessage('RUM Key is in invalid format.');
600735

601736
$ds = DIRECTORY_SEPARATOR;
737+
$_SERVER['RETRACE_RUM_KEY'] = '`invalid-rum-key';
738+
$mockRumObject = $this->givenARumObjectWithDefaultSetting();
739+
740+
$mockRumObject->checkRumKey(null);
741+
742+
unset($_SERVER['RETRACE_RUM_KEY']);
743+
}
744+
745+
public function testRumSetupConfigurationWithEmptyRumKeyFromEnv()
746+
{
602747
$appName = 'test app name';
603-
$environment = 'test environment';
748+
$environment = 'test';
604749
$_SERVER['RETRACE_RUM_KEY'] = '';
605-
$mockRumObject = $this->givenARumObjectWithDefaultSetting();
750+
$mockRumObject = $this->getMockBuilder(Rum::class)
751+
->setMethods(['logError'])
752+
->getMock();
753+
754+
$mockRumObject->expects($this->once())
755+
->method('logError')
756+
->with(
757+
'Unable to setup RUM Configuration. Something went wrong. Message: %s',
758+
'RUM Key is empty.'
759+
);
606760

607761
$mockRumObject->setupConfiguration(
608762
$appName,
609763
$environment
610764
);
611765

766+
$this->assertNull($mockRumObject->getApplicationName());
767+
$this->assertNull($mockRumObject->getEnvironment());
768+
$this->assertSame($mockRumObject->getRumScriptUrl(), self::DEFAULT_RUM_SCRIPT_URL);
769+
$this->assertNull($mockRumObject->getRumKey());
770+
771+
unset($_SERVER['RETRACE_RUM_KEY']);
772+
}
773+
774+
public function testRumCheckRumKeyWithEmptyRumKeyFromEnv()
775+
{
776+
$this->expectException(RumValidationException::class);
777+
$this->expectExceptionMessage('RUM Key is empty.');
778+
779+
$_SERVER['RETRACE_RUM_KEY'] = '';
780+
$mockRumObject = $this->givenARumObjectWithDefaultSetting();
781+
782+
$mockRumObject->checkRumKey(null);
612783
unset($_SERVER['RETRACE_RUM_KEY']);
613784
}
614785

@@ -632,7 +803,7 @@ public function testRumSetupConfigurationWithValidRumScriptUrlFromEnv()
632803
unset($_SERVER['RETRACE_RUM_SCRIPT_URL']);
633804
}
634805

635-
public function testRumSetupConfigurationWithInvalidRumScriptUrlFromEnv()
806+
public function testRumCheckRumScriptUrlWithInvalidRumScriptUrlFromEnv()
636807
{
637808
$this->expectException(RumValidationException::class);
638809
$this->expectExceptionMessage('RUM Script URL is in invalid format.');
@@ -644,12 +815,37 @@ public function testRumSetupConfigurationWithInvalidRumScriptUrlFromEnv()
644815
$_SERVER['RETRACE_RUM_SCRIPT_URL'] = 'invalid-rum-script-url';
645816
$mockRumObject = $this->givenARumObjectWithDefaultSetting();
646817

818+
$mockRumObject->checkRumScriptUrl(null);
819+
820+
unset($_SERVER['RETRACE_RUM_SCRIPT_URL']);
821+
}
822+
823+
public function testRumSetupConfigurationWithInvalidRumScriptUrlFromEnv()
824+
{
825+
$appName = 'test app name';
826+
$environment = 'test';
827+
$_SERVER['RETRACE_RUM_SCRIPT_URL'] = 'invalid-rum-script-url';
828+
$mockRumObject = $this->getMockBuilder(Rum::class)
829+
->setMethods(['logError'])
830+
->getMock();
831+
832+
$mockRumObject->expects($this->once())
833+
->method('logError')
834+
->with(
835+
'Unable to setup RUM Configuration. Something went wrong. Message: %s',
836+
'RUM Script URL is in invalid format.'
837+
);
838+
647839
$mockRumObject->setupConfiguration(
648840
$appName,
649-
$environment,
650-
$rumKey
841+
$environment
651842
);
652843

844+
$this->assertNull($mockRumObject->getApplicationName());
845+
$this->assertNull($mockRumObject->getEnvironment());
846+
$this->assertSame($mockRumObject->getRumScriptUrl(), self::DEFAULT_RUM_SCRIPT_URL);
847+
$this->assertNull($mockRumObject->getRumKey());
848+
653849
unset($_SERVER['RETRACE_RUM_SCRIPT_URL']);
654850
}
655851

0 commit comments

Comments
 (0)