Permalink
Browse files

unit tests for item and some more for the session

  • Loading branch information...
1 parent 75deb85 commit 5c373cddd8b802313d6ef762a5be1fa18e288b0d @dbu dbu committed Oct 23, 2011
Showing with 172 additions and 5 deletions.
  1. +159 −2 tests/Jackalope/ItemTest.php
  2. +13 −3 tests/Jackalope/SessionTest.php
@@ -4,9 +4,166 @@
class ItemTest extends TestCase
{
- public function testDummy()
+ /**
+ * create the item and mock any of the constructor parameters not specified explicitly
+ */
+ protected function getItem($factory = null, $path = null, $session = null, $objectManager = null, $new = false)
{
- $this->markTestSkipped('No tests for this class yet');
+ if (! $factory) {
+ $factory = new \Jackalope\Factory;
+ }
+ if (! $path) {
+ $path = '/';
+ }
+ if (! $session) {
+ $session = $this->getSessionMock();
+ }
+ if (! $objectManager) {
+ $objectManager = $this->getObjectManagerMock();
+ }
+ return new TestItem($factory, $path, $session, $objectManager, $new);
}
+ /**
+ * a mock that will additionally expect getNodeByPath once with $path and return the string 'placeholder'
+ */
+ protected function getObjectManagerMockWithPath($path)
+ {
+ $factory = new \Jackalope\Factory;
+ $om = $this->getMock('\Jackalope\ObjectManager', array('getNodeTypes', 'getNodeByPath'), array($factory, $this->getTransportStub('/jcr:root'), $this->getSessionMock()));
+ $om->expects($this->once())
+ ->method('getNodeByPath')
+ ->with($this->equalTo($path))
+ ->will($this->returnValue('placeholder'));
+
+ return $om;
+ }
+
+ public function testPath()
+ {
+ $item = $this->getItem();
+ $item->setPath('/b');
+ $this->assertEquals('/b', $item->getPath());
+ }
+
+ public function testName()
+ {
+ $item = $this->getItem(null, '/path/itemname');
+ $this->assertEquals('itemname', $item->getName());
+ $item->setPath('/other/name');
+ $this->assertEquals('name', $item->getName());
+ }
+
+ public function testGetAncestor()
+ {
+ $om = $this->getObjectManagerMockWithPath('/path');
+ $item = $this->getItem(null, '/path/name', null, $om);
+
+ $self = $item->getAncestor(2);
+ $this->assertSame($item, $self);
+
+ $ancestor = $item->getAncestor(1);
+ $this->assertSame('placeholder', $ancestor);
+ }
+
+ public function testGetAncestorRoot()
+ {
+ $om = $this->getObjectManagerMockWithPath('/');
+
+ $item = $this->getItem(null, '/path/name', null, $om);
+
+ $ancestor = $item->getAncestor(0);
+ $this->assertSame('placeholder', $ancestor);
+ }
+
+ /**
+ * @expectedException \PHPCR\ItemNotFoundException
+ */
+ public function testGetAncestorTooDeep()
+ {
+ $item = $this->getItem(null, '/path/name');
+ $ancestor = $item->getAncestor(3);
+ }
+
+ /**
+ * @expectedException \PHPCR\ItemNotFoundException
+ */
+ public function testGetAncestorTooLow()
+ {
+ $item = $this->getItem(null, '/path/name');
+ $ancestor = $item->getAncestor(-1);
+ }
+
+ public function testGetParent()
+ {
+ $om = $this->getObjectManagerMockWithPath('/path');
+ $item = $this->getItem(null, '/path/name', null, $om);
+ $parent = $item->getParent();
+ $this->assertSame('placeholder', $parent);
+ }
+
+ public function testGetDepth()
+ {
+ $item = $this->getItem(null, '/path/name');
+ $this->assertEquals(2, $item->getDepth());
+ $item = $this->getItem(null, '/');
+ $this->assertEquals(0, $item->getDepth());
+ }
+
+ public function testGetSession()
+ {
+ $session = $this->getSessionMock();
+ $item = $this->getItem($session);
+ $this->assertEquals($session, $item->getSession());
+ }
+
+ public function testIsSame()
+ {
+ $this->markTestSkipped('TODO: do some mean stuff');
+ }
+
+ public function testAccept()
+ {
+ $item = $this->getItem();
+ $visitor = $this->getMock('\PHPCR\ItemVisitorInterface');
+ $visitor->expects($this->once())
+ ->method('visit');
+ $item->accept($visitor);
+ }
+
+ public function testRemove()
+ {
+ $factory = new \Jackalope\Factory;
+ $om = $this->getMock('\Jackalope\ObjectManager', array('getNodeTypes', 'removeItem'), array($factory, $this->getTransportStub('/jcr:root'), $this->getSessionMock()));
+ $om->expects($this->once())
+ ->method('removeItem')
+ ->with($this->equalTo('/path'));
+
+ $item = $this->getItem(null, '/path', null, $om);
+ $item->remove();
+ $this->assertTrue($item->isDeleted());
+ }
+
+ /**
+ * @expectedException \PHPCR\RepositoryException
+ */
+ public function testRemoveRoot()
+ {
+ $item = $this->getItem('/');
+ $item->remove();
+ }
+
+ // TODO: test the whole state model processes (probably in separate test case)
+}
+
+class TestItem extends Item
+{
+ public function __construct($factory,$path,$session,$objectManager,$new)
+ {
+ parent::__construct($factory,$path,$session,$objectManager,$new);
+ }
+ public function refresh($keep)
+ {
+ // tested in extending classes
+ }
}
@@ -25,10 +25,20 @@ public function testConstructor()
$this->assertSame('value', $s->getAttribute('other'));
}
- public function testLogout()
+ public function testLogoutAndRegistry()
{
- $this->markTestSkipped();
- //TODO: test flush object manager with the help of mock objects
+ $factory = new \Jackalope\Factory;
+ $repository = $this->getMock('Jackalope\Repository', array(), array($factory), '', false);
+ $transport = $this->getMock('Jackalope\TransportInterface');
+ $transport->expects($this->once())
+ ->method('logout');
+ $session = new Session($factory, $repository, 'x', new \PHPCR\SimpleCredentials('foo', 'bar'), $transport);
+ $this->assertTrue($session->isLive());
+ $key = $session->getRegistryKey();
+ $this->assertSame($session, Session::getSessionFromRegistry($key));
+ $session->logout();
+ $this->assertFalse($session->isLive());
+ $this->assertNull(Session::getSessionFromRegistry($key));
}
public function testSessionRegistry()

0 comments on commit 5c373cd

Please sign in to comment.