forms = \FuzeWorks\Factory::getInstance('libraries')->get('forms'); } public function testFoundation() { $this->assertInstanceOf(Forms::class, $this->forms); } // Test prefixes and source directory public function testPrefixes() { $this->assertNull($this->forms->getClassesPrefix()); $this->assertNull($this->forms->getSourceDirectory()); } public function testGetForm() { $form = $this->forms->getForm('testName', 'testLabel'); $this->assertEquals('testName', $form->getName()); $this->assertEquals('testLabel', $form->getLabel()); $this->assertInstanceOf(Form::class, $form); } public function testGetCachedForm() { $form = $this->forms->getCachedForm(function (Form $form) { return $form; }, 'testName', 'testLabel'); $this->assertEquals('testName', $form->getName()); $this->assertEquals('testLabel', $form->getLabel()); $this->assertInstanceOf(Form::class, $form); // Retrieve the same form again $form2 = $this->forms->getCachedForm(function (){}, 'testName', 'testLabel'); $this->assertSame($form, $form2); } /** * @depends testGetCachedForm */ public function testGetCachedFormCsrfChange() { /** @var \FuzeWorks\Security $security */ $security = \FuzeWorks\Factory::getInstance("security"); $hash = $security->get_csrf_hash(); $form = $this->forms->getCachedForm(function (Form $form) { return $form; }, 'testGetCachedFormCsrfChange', 'testLabel'); $csrfField = $form->getCsrfField(); $this->assertInstanceOf(HiddenField::class, $csrfField); $this->assertEquals($hash, $csrfField->getValue()); // Now change the hash $security->csrf_regenerate(); $newHash = $security->get_csrf_hash(); $this->assertNotEquals($hash, $newHash); // Regenerate the form $newForm = $this->forms->getCachedForm(function (Form $form) { return $form; }, 'testGetCachedFormCsrfChange', 'testLabel'); $csrfField = $newForm->getCsrfField(); $this->assertEquals($newHash, $csrfField->getValue()); } }