EntityPreloaderTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Elgg;
  3. class EntityPreloaderTest extends \PHPUnit_Framework_TestCase {
  4. /**
  5. * @var \PHPUnit_Framework_MockObject_MockObject
  6. */
  7. public $mock;
  8. /**
  9. * @var EntityPreloader
  10. */
  11. public $obj;
  12. public function setup() {
  13. $this->obj = new EntityPreloader();
  14. $dependency = new PreloaderMock_20140623();
  15. $this->obj->_callable_cache_checker = array($dependency, 'isCached');
  16. $this->obj->_callable_entity_loader = array($dependency, 'load');
  17. $this->mock = $this->getMock('Elgg\\PreloaderMock_20140623');
  18. }
  19. public function testAcceptsOnlyArraysOfObjects() {
  20. $inputs = array(
  21. 'foo',
  22. array('0', array()),
  23. array(
  24. (object)array('foo' => 123),
  25. array('bar' => 234),
  26. ),
  27. );
  28. $this->obj->_callable_cache_checker = array($this->mock, 'isCached');
  29. $this->mock->expects($this->once())->method('isCached')->with(123);
  30. foreach ($inputs as $input) {
  31. $this->obj->preload($input, array('foo', 'bar'));
  32. }
  33. }
  34. public function testOnlyLoadsIfNotCached() {
  35. $this->obj->_callable_entity_loader = array($this->mock, 'load');
  36. $this->mock->expects($this->once())->method('load')->with(array('guids' => array(234, 345)));
  37. $this->obj->preload(array(
  38. (object)array('foo' => 23,),
  39. (object)array('bar' => 234,),
  40. (object)array('bar' => 345,),
  41. ), array('foo', 'bar'));
  42. }
  43. public function testOnlyLoadsIfMoreThanOne() {
  44. $this->obj->_callable_entity_loader = array($this->mock, 'load');
  45. $this->mock->expects($this->never())->method('load');
  46. $this->obj->preload(array(
  47. (object)array('foo' => 23,),
  48. (object)array('bar' => 234,),
  49. ), array('foo', 'bar'));
  50. }
  51. public function testQuietlyIgnoresMissingProperty() {
  52. $this->obj->_callable_entity_loader = array($this->mock, 'load');
  53. $this->mock->expects($this->once())->method('load')->with(array('guids' => array(234, 345)));
  54. $this->obj->preload(array(
  55. (object)array('foo' => 234),
  56. (object)array(),
  57. (object)array('bar' => 345)
  58. ), array('foo', 'bar'));
  59. }
  60. }
  61. class PreloaderMock_20140623 {
  62. function isCached($guid) {
  63. return $guid < 100;
  64. }
  65. function load($opts) {}
  66. }