ClassAliasAutoloaderTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Laravel\Tinker\Tests;
  3. use Laravel\Tinker\ClassAliasAutoloader;
  4. use Mockery;
  5. use PHPUnit\Framework\TestCase;
  6. use Psy\Shell;
  7. class ClassAliasAutoloaderTest extends TestCase
  8. {
  9. protected $classmapPath;
  10. protected $loader;
  11. protected function setUp(): void
  12. {
  13. $this->classmapPath = __DIR__.'/fixtures/vendor/composer/autoload_classmap.php';
  14. }
  15. protected function tearDown(): void
  16. {
  17. $this->loader->unregister();
  18. }
  19. public function testCanAliasClasses()
  20. {
  21. $this->loader = ClassAliasAutoloader::register(
  22. $shell = Mockery::mock(Shell::class),
  23. $this->classmapPath
  24. );
  25. $shell->shouldReceive('writeStdout')
  26. ->with("[!] Aliasing 'Bar' to 'App\Foo\Bar' for this Tinker session.\n")
  27. ->once();
  28. $this->assertTrue(class_exists('Bar'));
  29. $this->assertInstanceOf(\App\Foo\Bar::class, new \Bar);
  30. }
  31. public function testCanExcludeNamespacesFromAliasing()
  32. {
  33. $this->loader = ClassAliasAutoloader::register(
  34. $shell = Mockery::mock(Shell::class),
  35. $this->classmapPath,
  36. [],
  37. ['App\Baz']
  38. );
  39. $shell->shouldNotReceive('writeStdout');
  40. $this->assertFalse(class_exists('Qux'));
  41. }
  42. public function testVendorClassesAreExcluded()
  43. {
  44. $this->loader = ClassAliasAutoloader::register(
  45. $shell = Mockery::mock(Shell::class),
  46. $this->classmapPath
  47. );
  48. $shell->shouldNotReceive('writeStdout');
  49. $this->assertFalse(class_exists('Three'));
  50. }
  51. public function testVendorClassesCanBeWhitelisted()
  52. {
  53. $this->loader = ClassAliasAutoloader::register(
  54. $shell = Mockery::mock(Shell::class),
  55. $this->classmapPath,
  56. ['One\Two']
  57. );
  58. $shell->shouldReceive('writeStdout')
  59. ->with("[!] Aliasing 'Three' to 'One\Two\Three' for this Tinker session.\n")
  60. ->once();
  61. $this->assertTrue(class_exists('Three'));
  62. $this->assertInstanceOf(\One\Two\Three::class, new \Three);
  63. }
  64. }