* @package taoItems */ class PackerTest extends TestCase { public function setUp(): void { \common_ext_ExtensionsManager::singleton()->getExtensionById('taoItems'); } /** * Test creating an ItemPack */ public function testConstructor() { $item = new core_kernel_classes_Resource('toto'); $packer = new Packer($item); $this->assertInstanceOf('oat\taoItems\model\pack\Packer', $packer); } /** * Test assigning assets to a pack */ public function testPack() { $item = new core_kernel_classes_Resource('foo'); $model = new core_kernel_classes_Resource('fooModel'); $serviceMock = $this ->getMockBuilder('\taoItems_models_classes_ItemsService') ->disableOriginalConstructor() ->getMock(); $modelMock = $this ->getMockBuilder('\taoItems_models_classes_itemModel') ->getMock(); $directoryMock = $this ->getMockBuilder(\tao_models_classes_service_StorageDirectory::class) ->disableOriginalConstructor() ->getMock(); $packerMock = new PackerMock(); $modelMock ->method('getPackerClass') ->will($this->returnValue(get_class($packerMock))); $serviceMock ->method('getItemModel') ->will($this->returnValue(new core_kernel_classes_Resource('fooModel'))); $serviceMock ->method('getItemModelImplementation') ->with($this->equalTo($model)) ->will($this->returnValue($modelMock)); $serviceMock ->method('singleton') ->will($this->returnValue($serviceMock)); $packer = $this->getMockBuilder(Packer::class) ->setConstructorArgs([$item]) ->setMethods(['getStorageDirectory']) ->getMock(); $prop = new \ReflectionProperty(Packer::class, 'itemService'); $prop->setAccessible(true); $prop->setValue($packer, $serviceMock); $packer ->method('getStorageDirectory') ->will($this->returnValue($directoryMock)); $result = $packer->pack(); $this->assertInstanceOf('oat\taoItems\model\pack\ItemPack', $result); $this->assertEquals('qti', $result->getType()); $this->assertEquals(['uri' => $item->getUri()], $result->getData()); } /** * Test the exception chain when the item has no model * */ public function testNoItemModel() { $this->expectException(common_Exception::class); $item = new core_kernel_classes_Resource('foo'); $serviceMock = $this ->getMockBuilder('\taoItems_models_classes_ItemsService') ->disableOriginalConstructor() ->getMock(); $serviceMock ->method('getItemModel') ->will($this->returnValue(null)); $serviceMock ->method('singleton') ->will($this->returnValue($serviceMock)); $packer = new Packer($item); $prop = new \ReflectionProperty('oat\taoItems\model\pack\Packer', 'itemService'); $prop->setAccessible(true); $prop->setValue($packer, $serviceMock); $packer->pack(); } /** * Test the exception chain when there is no implementations for a model */ public function testNoModelImplementation() { $this->expectException(common_Exception::class); $item = new core_kernel_classes_Resource('foo'); $model = new core_kernel_classes_Resource('fooModel'); $serviceMock = $this ->getMockBuilder('\taoItems_models_classes_ItemsService') ->disableOriginalConstructor() ->getMock(); $serviceMock ->method('getItemModel') ->will($this->returnValue($model)); $serviceMock ->method('getItemModelImplementation') ->with($this->equalTo($model)) ->will($this->returnValue(null)); $serviceMock ->method('singleton') ->will($this->returnValue($serviceMock)); $packer = new Packer($item); $prop = new \ReflectionProperty('oat\taoItems\model\pack\Packer', 'itemService'); $prop->setAccessible(true); $prop->setValue($packer, $serviceMock); $packer->pack(); } /** * Test the exception chain when the model does not return a correct packer class * */ public function testNoPackerClass() { $this->expectException(common_Exception::class); $item = new core_kernel_classes_Resource('foo'); $serviceMock = $this ->getMockBuilder('\taoItems_models_classes_ItemsService') ->disableOriginalConstructor() ->getMock(); $modelMock = $this ->getMockBuilder('\taoItems_models_classes_itemModel') ->getMock(); $modelMock ->method('getPackerClass') ->will($this->returnValue(null)); $serviceMock ->method('getItemModel') ->will($this->returnValue(new core_kernel_classes_Resource('fooModel'))); $serviceMock ->method('getItemModelImplementation') ->will($this->returnValue($modelMock)); $serviceMock ->method('singleton') ->will($this->returnSelf()); $packer = new Packer($item); $prop = new \ReflectionProperty('oat\taoItems\model\pack\Packer', 'itemService'); $prop->setAccessible(true); $prop->setValue($packer, $serviceMock); $packer->pack(); } /** * Test the exception chain when the model returns a wrong packer class * */ public function testWrongPackerClass() { $this->expectException(common_Exception::class); $item = new core_kernel_classes_Resource('foo'); $serviceMock = $this ->getMockBuilder('\taoItems_models_classes_ItemsService') ->disableOriginalConstructor() ->getMock(); $modelMock = $this ->getMockBuilder('\taoItems_models_classes_itemModel') ->getMock(); $modelMock ->method('getPackerClass') ->will($this->returnValue("stdClass")); $serviceMock ->method('getItemModel') ->will($this->returnValue(new core_kernel_classes_Resource('fooModel'))); $serviceMock ->method('getItemModelImplementation') ->will($this->returnValue($modelMock)); $serviceMock ->method('singleton') ->will($this->returnSelf()); $packer = new Packer($item); $prop = new \ReflectionProperty('oat\taoItems\model\pack\Packer', 'itemService'); $prop->setAccessible(true); $prop->setValue($packer, $serviceMock); $packer->pack(); } } //use an old school mock as the Packer create it's own instance from the class class PackerMock extends ItemPacker { public function packItem(core_kernel_classes_Resource $item, $lang, Directory $directory) { return new ItemPack('qti', ['uri' => $item->getUri()]); } }