AttributeBagNamespacedTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. use Fhaculty\Graph\Graph;
  3. use Fhaculty\Graph\Attribute\AttributeBagNamespaced;
  4. use Fhaculty\Graph\Attribute\AttributeAware;
  5. use Fhaculty\Graph\Attribute\AttributeBagContainer;
  6. class AtributeBagNamespacedTest extends TestCase
  7. {
  8. public function testBagContainer()
  9. {
  10. $container = new AttributeBagContainer();
  11. $bag = new AttributeBagNamespaced($container, 'test.');
  12. $this->assertSame($bag, $bag->getAttributeBag());
  13. $container->setAttribute('a.b', 'c');
  14. $container->setAttribute('test.d', 'e');
  15. $this->assertEquals('e', $bag->getAttribute('d'));
  16. $this->assertNull($bag->getAttribute('unknown'));
  17. $this->assertEquals('default', $bag->getAttribute('unknown', 'default'));
  18. $bag->setAttribute('d', 'test');
  19. $this->assertEquals('test', $bag->getAttribute('d'));
  20. $this->assertEquals('test', $container->getAttribute('test.d'));
  21. $bag->setAttributes(array('d' => 'd', 'e' => 'e'));
  22. $this->assertEquals(array('a.b' => 'c', 'test.d' => 'd', 'test.e' => 'e'), $container->getAttributes());
  23. }
  24. /**
  25. *
  26. * @param AttributeAware $entity
  27. * @dataProvider provideNamespacable
  28. */
  29. public function testReadableEntities(AttributeAware $entity)
  30. {
  31. $bag = new AttributeBagNamespaced($entity, 'test.');
  32. $this->assertSame($bag, $bag->getAttributeBag());
  33. $entity->setAttribute('a.b', 'c');
  34. $entity->setAttribute('test.d', 'e');
  35. $this->assertEquals('e', $bag->getAttribute('d'));
  36. $this->assertNull($bag->getAttribute('a.b'));
  37. $this->assertNull($bag->getAttribute('test.d'));
  38. $this->assertEquals(array('d' => 'e'), $bag->getAttributes());
  39. }
  40. public function provideNamespacable()
  41. {
  42. $graph = new Graph();
  43. $vertex = $graph->createVertex();
  44. $bag = $vertex->getAttributeBag();
  45. $subNamespace = new AttributeBagNamespaced($bag, 'prefix');
  46. return array(
  47. array($graph),
  48. array($vertex),
  49. array($bag),
  50. array($subNamespace),
  51. );
  52. }
  53. }