ManagerTestTrait.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace SocialiteProviders\Manager\Test;
  3. use Illuminate\Contracts\Container\Container as ContainerContract;
  4. use Illuminate\Contracts\Foundation\Application;
  5. use Illuminate\Http\Request as HttpRequest;
  6. use Laravel\Socialite\SocialiteManager;
  7. use Mockery as m;
  8. use Mockery\MockInterface;
  9. use SocialiteProviders\Manager\Config;
  10. use SocialiteProviders\Manager\Contracts\Helpers\ConfigRetrieverInterface;
  11. trait ManagerTestTrait
  12. {
  13. public static $functions;
  14. protected function setUp(): void
  15. {
  16. self::$functions = m::mock();
  17. }
  18. protected function tearDown(): void
  19. {
  20. m::close();
  21. }
  22. /**
  23. * @return \Mockery\MockInterface|\SocialiteProviders\Manager\Contracts\Helpers\ConfigRetrieverInterface
  24. */
  25. protected function configRetrieverMock()
  26. {
  27. return m::mock(ConfigRetrieverInterface::class);
  28. }
  29. /**
  30. * @return \Mockery\MockInterface|\Illuminate\Contracts\Container\Container
  31. */
  32. protected function appMock()
  33. {
  34. return m::mock(ContainerContract::class);
  35. }
  36. /**
  37. * @return \Mockery\MockInterface|\Illuminate\Contracts\Foundation\Application
  38. */
  39. protected function appMockWithBooted()
  40. {
  41. $app = m::mock(Application::class);
  42. $app->shouldReceive('booted')->with(m::on(function ($callback) {
  43. $callback();
  44. return true;
  45. }));
  46. return $app;
  47. }
  48. /**
  49. * @return \Mockery\MockInterface|\Laravel\Socialite\SocialiteManager
  50. */
  51. protected function socialiteMock()
  52. {
  53. return m::mock(SocialiteManager::class);
  54. }
  55. /**
  56. * @return \Mockery\MockInterface|\Illuminate\Http\Request
  57. */
  58. protected function buildRequest()
  59. {
  60. return m::mock(HttpRequest::class);
  61. }
  62. protected function configObject(): Config
  63. {
  64. return new Config('test', 'test', 'test');
  65. }
  66. protected function configRetrieverMockWithDefaultExpectations($providerName, $providerClass)
  67. {
  68. $configRetriever = $this->configRetrieverMock();
  69. $configRetriever
  70. ->shouldReceive('fromServices')
  71. ->with($providerName, $providerClass::additionalConfigKeys())
  72. ->andReturn($this->configObject());
  73. return $configRetriever;
  74. }
  75. /**
  76. * @return array
  77. */
  78. protected function config(): array
  79. {
  80. return [
  81. 'client_id' => 'test',
  82. 'client_secret' => 'test',
  83. 'redirect' => 'test',
  84. ];
  85. }
  86. /**
  87. * @param array $config
  88. * @return array
  89. */
  90. protected function oauth1FormattedConfig(array $config): array
  91. {
  92. return [
  93. 'identifier' => $config['client_id'],
  94. 'secret' => $config['client_secret'],
  95. 'callback_uri' => $config['redirect'],
  96. ];
  97. }
  98. protected function oauth2ProviderStub(): MockInterface
  99. {
  100. static $provider = null;
  101. if (is_null($provider)) {
  102. $provider = $this->mockStub('OAuth2ProviderStub')->shouldDeferMissing();
  103. }
  104. return $provider;
  105. }
  106. protected function oauth1ProviderStub(): MockInterface
  107. {
  108. static $provider = null;
  109. if (is_null($provider)) {
  110. $provider = $this->mockStub('OAuth1ProviderStub');
  111. }
  112. return $provider;
  113. }
  114. protected function oauth1ProviderStubClass(): string
  115. {
  116. return $this->fullStubClassName('OAuth1ProviderStub');
  117. }
  118. protected function oauth1ServerStubClass(): string
  119. {
  120. return $this->fullStubClassName('OAuth1ServerStub');
  121. }
  122. protected function oauth2ProviderStubClass(): string
  123. {
  124. return $this->fullStubClassName('OAuth2ProviderStub');
  125. }
  126. /**
  127. * @param string $stub
  128. * @return \Mockery\MockInterface
  129. */
  130. protected function mockStub($stub): MockInterface
  131. {
  132. return m::mock($this->fullStubClassName($stub));
  133. }
  134. /**
  135. * @param string $stub
  136. * @return string
  137. */
  138. protected function fullStubClassName($stub): string
  139. {
  140. return __NAMESPACE__.'\Stubs\\'.$stub;
  141. }
  142. /**
  143. * @return string
  144. */
  145. protected function invalidClass(): string
  146. {
  147. return 'FooBar';
  148. }
  149. }