From 0693e7cf2db3e49c54e27cab3ddbad060395205e Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 9 Sep 2014 13:06:30 +0200 Subject: [PATCH] Fix ResourceForm tests refs #5525 --- .../Config/Resource/DbResourceFormTest.php | 66 +++++ .../Config/Resource/LdapResourceFormTest.php | 66 +++++ .../Resource/LivestatusResourceFormTest.php | 67 +++++ .../Resource/ResourceFormTestBroken.php | 268 ------------------ 4 files changed, 199 insertions(+), 268 deletions(-) create mode 100644 test/php/application/forms/Config/Resource/DbResourceFormTest.php create mode 100644 test/php/application/forms/Config/Resource/LdapResourceFormTest.php create mode 100644 test/php/application/forms/Config/Resource/LivestatusResourceFormTest.php delete mode 100644 test/php/application/forms/Config/Resource/ResourceFormTestBroken.php diff --git a/test/php/application/forms/Config/Resource/DbResourceFormTest.php b/test/php/application/forms/Config/Resource/DbResourceFormTest.php new file mode 100644 index 000000000..1b624c49f --- /dev/null +++ b/test/php/application/forms/Config/Resource/DbResourceFormTest.php @@ -0,0 +1,66 @@ +setUpResourceFactoryMock( + Mockery::mock()->shouldReceive('getConnection')->atMost()->twice()->andReturn(Mockery::self())->getMock() + ); + + $form = new DbResourceForm(); + + $this->assertTrue( + $form->isValidResource($form), + 'ResourceForm claims that a valid db resource is not valid' + ); + } + + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testInvalidDbResourceIsNotValid() + { + $this->setUpResourceFactoryMock( + Mockery::mock()->shouldReceive('getConnection')->once()->andThrow('\Exception')->getMock() + ); + + $form = new DbResourceForm(); + + $this->assertFalse( + $form->isValidResource($form), + 'ResourceForm claims that an invalid db resource is valid' + ); + } + + protected function setUpResourceFactoryMock($resourceMock) + { + Mockery::mock('alias:Icinga\Data\ResourceFactory') + ->shouldReceive('createResource') + ->with(Mockery::type('\Zend_Config')) + ->andReturn($resourceMock); + } +} diff --git a/test/php/application/forms/Config/Resource/LdapResourceFormTest.php b/test/php/application/forms/Config/Resource/LdapResourceFormTest.php new file mode 100644 index 000000000..078a11146 --- /dev/null +++ b/test/php/application/forms/Config/Resource/LdapResourceFormTest.php @@ -0,0 +1,66 @@ +setUpResourceFactoryMock( + Mockery::mock()->shouldReceive('connect')->getMock() + ); + + $form = new LdapResourceForm(); + + $this->assertTrue( + $form->isValidResource($form), + 'ResourceForm claims that a valid ldap resource is not valid' + ); + } + + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testInvalidLdapResourceIsNotValid() + { + $this->setUpResourceFactoryMock( + Mockery::mock()->shouldReceive('connect')->once()->andThrow('\Exception')->getMock() + ); + + $form = new LdapResourceForm(); + + $this->assertFalse( + $form->isValidResource($form), + 'ResourceForm claims that an invalid ldap resource is valid' + ); + } + + protected function setUpResourceFactoryMock($resourceMock) + { + Mockery::mock('alias:Icinga\Data\ResourceFactory') + ->shouldReceive('createResource') + ->with(Mockery::type('\Zend_Config')) + ->andReturn($resourceMock); + } +} diff --git a/test/php/application/forms/Config/Resource/LivestatusResourceFormTest.php b/test/php/application/forms/Config/Resource/LivestatusResourceFormTest.php new file mode 100644 index 000000000..300adebed --- /dev/null +++ b/test/php/application/forms/Config/Resource/LivestatusResourceFormTest.php @@ -0,0 +1,67 @@ +setUpResourceFactoryMock( + Mockery::mock()->shouldReceive('connect')->andReturn(Mockery::self()) + ->shouldReceive('disconnect')->getMock() + ); + + $form = new LivestatusResourceForm(); + + $this->assertTrue( + $form->isValidResource($form), + 'ResourceForm claims that a valid livestatus resource is not valid' + ); + } + + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testInvalidLivestatusResourceIsNotValid() + { + $this->setUpResourceFactoryMock( + Mockery::mock()->shouldReceive('connect')->once()->andThrow('\Exception')->getMock() + ); + + $form = new LivestatusResourceForm(); + + $this->assertFalse( + $form->isValidResource($form), + 'ResourceForm claims that an invalid livestatus resource is valid' + ); + } + + protected function setUpResourceFactoryMock($resourceMock) + { + Mockery::mock('alias:Icinga\Data\ResourceFactory') + ->shouldReceive('createResource') + ->with(Mockery::type('\Zend_Config')) + ->andReturn($resourceMock); + } +} diff --git a/test/php/application/forms/Config/Resource/ResourceFormTestBroken.php b/test/php/application/forms/Config/Resource/ResourceFormTestBroken.php deleted file mode 100644 index 6dba6ce54..000000000 --- a/test/php/application/forms/Config/Resource/ResourceFormTestBroken.php +++ /dev/null @@ -1,268 +0,0 @@ -is_valid; - } -} - -class ResourceFormTest extends BaseTestCase -{ - public function tearDown() - { - parent::tearDown(); - Mockery::close(); // Necessary because some tests run in a separate process - } - - public function testIsForceCreationCheckboxBeingAdded() - { - $form = new TestResourceForm(); - $form->is_valid = false; - - $this->assertFalse($form->isValid(array())); - $this->assertNotNull( - $form->getElement('resource_force_creation'), - 'Checkbox to force the creation of a resource is not being added though the resource is invalid' - ); - } - - public function testIsForceCreationCheckboxNotBeingAdded() - { - $form = new TestResourceForm(); - $form->is_valid = true; - - $this->assertTrue($form->isValid(array())); - $this->assertNull( - $form->getElement('resource_force_creation'), - 'Checkbox to force the creation of a resource is being added though the resource is valid' - ); - } - - public function testIsTheFormValidIfForceCreationTrue() - { - $form = new TestResourceForm(); - $form->is_valid = false; - - $this->assertTrue( - $form->isValid(array('resource_force_creation' => 1)), - 'ResourceForm with invalid resource is not valid though force creation is set' - ); - } - - /** - * @runInSeparateProcess - * @preserveGlobalState disabled - */ - public function testValidDbResourceIsValid() - { - $this->setUpResourceFactoryMock( - Mockery::mock()->shouldReceive('getConnection')->atMost()->twice()->andReturn(Mockery::self())->getMock() - ); - $form = $this->buildResourceForm(new Zend_Config(array('type' => 'db'))); - - $this->assertTrue( - $form->isValidResource(), - 'ResourceForm claims that a valid db resource is not valid' - ); - } - - /** - * @runInSeparateProcess - * @preserveGlobalState disabled - */ - public function testInvalidDbResourceIsNotValid() - { - $this->setUpResourceFactoryMock( - Mockery::mock()->shouldReceive('getConnection')->once()->andThrow('\Exception')->getMock() - ); - $form = $this->buildResourceForm(new Zend_Config(array('type' => 'db'))); - - $this->assertFalse( - $form->isValidResource(), - 'ResourceForm claims that an invalid db resource is valid' - ); - } - - /** - * @runInSeparateProcess - * @preserveGlobalState disabled - */ - public function testValidLdapResourceIsValid() - { - $this->setUpResourceFactoryMock( - Mockery::mock()->shouldReceive('connect')->getMock() - ); - $form = $this->buildResourceForm(new Zend_Config(array('type' => 'ldap'))); - - $this->assertTrue( - $form->isValidResource(), - 'ResourceForm claims that a valid ldap resource is not valid' - ); - } - - /** - * @runInSeparateProcess - * @preserveGlobalState disabled - */ - public function testInvalidLdapResourceIsNotValid() - { - $this->setUpResourceFactoryMock( - Mockery::mock()->shouldReceive('connect')->once()->andThrow('\Exception')->getMock() - ); - $form = $this->buildResourceForm(new Zend_Config(array('type' => 'ldap'))); - - $this->assertFalse( - $form->isValidResource(), - 'ResourceForm claims that an invalid ldap resource is valid' - ); - } - - /** - * @runInSeparateProcess - * @preserveGlobalState disabled - */ - public function testValidLivestatusResourceIsValid() - { - $this->setUpResourceFactoryMock( - Mockery::mock()->shouldReceive('connect')->andReturn(Mockery::self()) - ->shouldReceive('disconnect')->getMock() - ); - $form = $this->buildResourceForm(new Zend_Config(array('type' => 'livestatus'))); - - $this->assertTrue( - $form->isValidResource(), - 'ResourceForm claims that a valid livestatus resource is not valid' - ); - } - - /** - * @runInSeparateProcess - * @preserveGlobalState disabled - */ - public function testInvalidLivestatusResourceIsNotValid() - { - $this->setUpResourceFactoryMock( - Mockery::mock()->shouldReceive('connect')->once()->andThrow('\Exception')->getMock() - ); - $form = $this->buildResourceForm(new Zend_Config(array('type' => 'livestatus'))); - - $this->assertFalse( - $form->isValidResource(), - 'ResourceForm claims that an invalid livestatus resource is valid' - ); - } - - public function testValidFileResourceIsValid() - { - $form = $this->buildResourceForm( - new Zend_Config( - array( - 'type' => 'file', - 'filename' => BaseTestCase::$testDir . '/res/status/icinga.status.dat' - ) - ) - ); - - $this->assertTrue( - $form->isValidResource(), - 'ResourceForm claims that a valid file resource is not valid' - ); - } - - public function testInvalidFileResourceIsNotValid() - { - $form = $this->buildResourceForm( - new Zend_Config( - array( - 'type' => 'file', - 'filename' => 'not_existing' - ) - ) - ); - - $this->assertFalse( - $form->isValidResource(), - 'ResourceForm claims that an invalid file resource is valid' - ); - } - - public function testValidStatusdatResourceIsValid() - { - $form = $this->buildResourceForm( - new Zend_Config( - array( - 'type' => 'statusdat', - 'status_file' => BaseTestCase::$testDir . '/res/status/icinga.status.dat', - 'object_file' => BaseTestCase::$testDir . '/res/status/icinga.objects.cache', - ) - ) - ); - - $this->assertTrue( - $form->isValidResource(), - 'ResourceForm claims that a valid statusdat resource is not valid' - ); - } - - public function testInvalidStatusdatResourceIsNotValid() - { - $form = $this->buildResourceForm( - new Zend_Config( - array( - 'type' => 'statusdat', - 'status_file' => 'not_existing', - 'object_file' => 'not_existing' - ) - ) - ); - - $this->assertFalse( - $form->isValidResource(), - 'ResourceForm claims that an invalid statusdat resource is valid' - ); - } - - protected function buildResourceForm($resourceConfig) - { - $form = new ResourceForm(); - $form->setRequest($this->getRequestMock()); - $form->setResource($resourceConfig); - $form->create(); - - return $form; - } - - protected function getRequestMock() - { - return Mockery::mock('\Zend_Controller_Request_Abstract') - ->shouldReceive('getParam') - ->with(Mockery::type('string'), Mockery::type('string')) - ->andReturnUsing(function ($name, $default) { return $default; }) - ->getMock(); - } - - protected function setUpResourceFactoryMock($resourceMock) - { - Mockery::mock('alias:Icinga\Data\ResourceFactory') - ->shouldReceive('createResource') - ->with(Mockery::type('\Zend_Config')) - ->andReturn($resourceMock); - } -}