@@ -593,6 +593,78 @@ conditions::
593593 ],
594594 ]);
595595
596+ Class Existence Based Tests
597+ ---------------------------
598+
599+ Tests that behave differently depending on existing classes, for example Composer's
600+ development dependencies, are often hard to test for the alternate case. For that
601+ reason, this component also provides mocks for these PHP functions:
602+
603+ * :phpfunction: `class_exists `
604+ * :phpfunction: `interface_exists `
605+ * :phpfunction: `trait_exists `
606+
607+ Use Case
608+ ~~~~~~~~
609+
610+ Consider the following example that relies on the ``Vendor\DependencyClass `` to
611+ toggle a behavior::
612+
613+ use Vendor\DependencyClass;
614+
615+ class MyClass
616+ {
617+ public function hello(): string
618+ {
619+ if (class_exists(DependencyClass::class)) {
620+ return 'The dependency bahavior.';
621+ }
622+
623+ return 'The default behavior.';
624+ }
625+ }
626+
627+ A regular test case for ``MyClass `` (assuming the development dependencies
628+ are installed during tests) would look like::
629+
630+ use MyClass;
631+ use PHPUnit\Framework\TestCase;
632+
633+ class MyClassTest extends TestCase
634+ {
635+ public function testHello()
636+ {
637+ $class = new MyClass();
638+ $result = $class->hello(); // "The dependency bahavior."
639+
640+ // ...
641+ }
642+ }
643+
644+ In order to test the default behavior instead use the
645+ ``ClassExistsMock::withMockedClasses() `` to configure the expected
646+ classes, interfaces and/or traits for the code to run::
647+
648+ use MyClass;
649+ use PHPUnit\Framework\TestCase;
650+ use Vendor\DependencyClass;
651+
652+ class MyClassTest extends TestCase
653+ {
654+ // ...
655+
656+ public function testHelloDefault()
657+ {
658+ ClassExistsMock::register(MyClass::class);
659+ ClassExistsMock::withMockedClasses([DependencyClass::class => false]);
660+
661+ $class = new MyClass();
662+ $result = $class->hello(); // "The default bahavior."
663+
664+ // ...
665+ }
666+ }
667+
596668Troubleshooting
597669---------------
598670
0 commit comments