OAuth1ProviderTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace SocialiteProviders\Manager\Test;
  3. use Laravel\Socialite\Contracts\Factory as SocialiteFactoryContract;
  4. use Mockery as m;
  5. use PHPUnit\Framework\TestCase;
  6. use SocialiteProviders\Manager\Exception\InvalidArgumentException;
  7. use SocialiteProviders\Manager\SocialiteWasCalled;
  8. class OAuth1ProviderTest extends TestCase
  9. {
  10. use ManagerTestTrait;
  11. /**
  12. * @test
  13. */
  14. public function it_should_build_a_provider_and_extend_socialite(): void
  15. {
  16. $providerName = 'bar';
  17. $providerClass = $this->oauth1ProviderStubClass();
  18. $socialite = $this->socialiteMock();
  19. $socialite
  20. ->shouldReceive('formatConfig')
  21. ->with($this->config())
  22. ->andReturn($this->oauth1FormattedConfig($this->config()));
  23. $socialite
  24. ->shouldReceive('extend')
  25. ->withArgs([
  26. $providerName,
  27. m::on(function ($closure) use ($providerClass) {
  28. $this->assertInstanceOf($providerClass, $closure());
  29. return is_callable($closure);
  30. }),
  31. ]);
  32. $app = $this->appMock();
  33. $app
  34. ->shouldReceive('make')
  35. ->with(SocialiteFactoryContract::class)
  36. ->andReturn($socialite);
  37. $app
  38. ->shouldReceive('offsetGet')
  39. ->with('request')
  40. ->andReturn($this->buildRequest());
  41. $configRetriever = $this->configRetrieverMockWithDefaultExpectations(
  42. $providerName,
  43. $providerClass
  44. );
  45. $event = new SocialiteWasCalled($app, $configRetriever);
  46. $event->extendSocialite(
  47. $providerName,
  48. $providerClass,
  49. $this->oauth1ServerStubClass()
  50. );
  51. }
  52. /**
  53. * @test
  54. */
  55. public function it_throws_if_given_an_invalid_oauth1_provider(): void
  56. {
  57. $this->expectExceptionObject(new InvalidArgumentException("FooBar doesn't exist"));
  58. $providerName = 'foo';
  59. $providerClass = $this->oauth1ProviderStubClass();
  60. $socialite = $this->socialiteMock();
  61. $socialite
  62. ->shouldReceive('formatConfig')
  63. ->with($this->config())
  64. ->andReturn($this->oauth1FormattedConfig($this->config()));
  65. $app = $this->appMock();
  66. $app
  67. ->shouldReceive('make')
  68. ->andReturn($socialite);
  69. $configRetriever = $this->configRetrieverMockWithDefaultExpectations(
  70. $providerName,
  71. $providerClass
  72. );
  73. $event = new SocialiteWasCalled($app, $configRetriever);
  74. $event->extendSocialite(
  75. $providerName,
  76. $this->invalidClass(),
  77. $this->oauth1ServerStubClass()
  78. );
  79. }
  80. /**
  81. * @test
  82. */
  83. public function it_throws_if_given_an_invalid_oauth1_server(): void
  84. {
  85. $this->expectExceptionObject(new InvalidArgumentException("FooBar doesn't exist"));
  86. $providerName = 'bar';
  87. $providerClass = $this->oauth1ProviderStubClass();
  88. $socialite = $this->socialiteMock();
  89. $app = $this->appMock();
  90. $app
  91. ->shouldReceive('make')
  92. ->andReturn($socialite);
  93. $configRetriever = $this->configRetrieverMockWithDefaultExpectations(
  94. $providerName,
  95. $providerClass
  96. );
  97. $event = new SocialiteWasCalled($app, $configRetriever);
  98. $event->extendSocialite(
  99. $providerName,
  100. $providerClass,
  101. $this->invalidClass()
  102. );
  103. }
  104. }