PluginTests.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. use League\Flysystem\Adapter\Local;
  3. use League\Flysystem\File;
  4. use League\Flysystem\Filesystem;
  5. use League\Flysystem\FilesystemInterface;
  6. use League\Flysystem\PluginInterface;
  7. use PHPUnit\Framework\TestCase;
  8. class MyPlugin implements PluginInterface
  9. {
  10. public function getMethod()
  11. {
  12. return 'beAwesome';
  13. }
  14. public function setFilesystem(FilesystemInterface $filesystem)
  15. {
  16. // yay
  17. }
  18. public function handle($argument = false)
  19. {
  20. return $argument;
  21. }
  22. }
  23. class InvalidPlugin implements PluginInterface
  24. {
  25. public function getMethod()
  26. {
  27. return 'beInvalid';
  28. }
  29. public function setFilesystem(FilesystemInterface $filesystem)
  30. {
  31. // yay
  32. }
  33. }
  34. class AuthorizePlugin implements PluginInterface
  35. {
  36. public function getMethod()
  37. {
  38. return 'authorize';
  39. }
  40. public function setFilesystem(FilesystemInterface $filesystem)
  41. {
  42. // yay
  43. }
  44. public function handle($path)
  45. {
  46. return $path !== 'bad';
  47. }
  48. }
  49. class PluginTests extends TestCase
  50. {
  51. protected $filesystem;
  52. public function setup(): void
  53. {
  54. $this->filesystem = new Filesystem(new Local(__DIR__));
  55. }
  56. public function testPlugin()
  57. {
  58. $this->expectException(LogicException::class);
  59. $this->filesystem->addPlugin(new MyPlugin());
  60. $this->assertEquals('result', $this->filesystem->beAwesome('result'));
  61. $this->filesystem->unknownPlugin();
  62. }
  63. public function testInvalidPlugin()
  64. {
  65. $this->expectException(LogicException::class);
  66. $this->filesystem->addPlugin(new InvalidPlugin());
  67. $this->filesystem->beInvalid();
  68. }
  69. public function testMagicCall()
  70. {
  71. $this->filesystem->addPlugin(new AuthorizePlugin());
  72. $badFile = $this->filesystem->get('bad', new File());
  73. $this->assertFalse($badFile->authorize());
  74. $goodFile = $this->filesystem->get('good', new File());
  75. $this->assertTrue($goodFile->authorize());
  76. }
  77. public function testBadMagicCall()
  78. {
  79. $this->expectException(BadMethodCallException::class);
  80. $file = $this->filesystem->get('foo', new File());
  81. $file->nonExistentMethod();
  82. }
  83. }