StubFactory.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. declare(strict_types=1);
  3. namespace TestApp\Datasource;
  4. use Cake\Datasource\Exception\MissingModelException;
  5. use Cake\Datasource\Locator\LocatorInterface;
  6. use Cake\Datasource\RepositoryInterface;
  7. class StubFactory implements LocatorInterface
  8. {
  9. private $instances = [];
  10. /**
  11. * @inheritDoc
  12. */
  13. public function get(string $alias, array $options = []): RepositoryInterface
  14. {
  15. if (!isset($this->instances[$alias])) {
  16. throw new MissingModelException(sprintf(
  17. 'Model class "%s" of type "Test" could not be found.',
  18. $alias
  19. ));
  20. }
  21. return $this->instances[$alias];
  22. }
  23. /**
  24. * @inheritDoc
  25. */
  26. public function set(string $alias, RepositoryInterface $repository): RepositoryInterface
  27. {
  28. $this->instances[$alias] = $repository;
  29. return $repository;
  30. }
  31. /**
  32. * @inheritDoc
  33. */
  34. public function exists(string $alias): bool
  35. {
  36. return isset($this->instances[$alias]);
  37. }
  38. /**
  39. * @inheritDoc
  40. */
  41. public function remove(string $alias): void
  42. {
  43. unset($this->instances[$alias]);
  44. }
  45. /**
  46. * @inheritDoc
  47. */
  48. public function clear(): void
  49. {
  50. $this->instances = [];
  51. }
  52. }