View source
<?php
require_once 'TagadelicCloud.php';
require_once 'TagadelicTag.php';
class TagadelicCloudTest extends PHPUnit_Framework_TestCase {
protected $object;
protected function setUp() {
$this->object = new TagadelicCloud(1337);
$this
->addTagStub("jane", "Jane", 200);
$this
->addTagStub("blackbeard", "Blackbeard", 100);
}
protected function tearDown() {
}
public function test__construct() {
$this
->assertAttributeSame(1337, "id", $this->object);
$this->object = new TagadelicCloud(1337, $this->mock_tags);
$this
->assertAttributeSame($this->mock_tags, "tags", $this->object);
}
public function testGet_id() {
$this
->assertEquals(1337, $this->object
->get_id());
}
public function testGet_tags() {
$this->object = new TagadelicCloud(1337, $this->mock_tags);
$this
->assertSame($this->mock_tags, $this->object
->get_tags());
}
public function testAdd_tag() {
$this->object
->add_tag($this->mock_tags["blackbeard"]);
$this
->assertAttributeContains($this->mock_tags["blackbeard"], "tags", $this->object);
}
public function testAdd_tagIsChainable() {
$this
->assertEquals($this->object
->add_tag($this->mock_tags["blackbeard"]), $this->object);
}
public function testSet_drupal() {
$drupal = new StdClass();
$this->object
->set_drupal($drupal);
$this
->assertAttributeSame($drupal, "drupal", $this->object);
}
public function testDrupalReturnsSetValue() {
$drupal = "ThisIsDrupal";
$this->object
->set_drupal($drupal);
$this
->assertSame($this->object
->drupal(), $drupal);
}
public function testDrupalInstantiatesNewWrapper() {
$this->object
->set_drupal(NULL);
$drupal = $this
->getMock("TagadelicDrupalWrapper");
$this
->assertInstanceOf("TagadelicDrupalWrapper", $this->object
->drupal());
}
public function testfrom_cache() {
$drupal = $this
->getMock("TagadelicDrupalWrapper", array(
"cache_get",
));
$drupal
->expects($this
->once())
->method("cache_get")
->with("tagadelic_cloud_1337")
->will($this
->returnvalue($this->object));
$cloud = TagadelicCloud::from_cache(1337, $drupal);
$this
->assertinstanceof("TagadelicCloud", $cloud);
}
public function testTo_cache() {
$drupal = $this
->getMock("TagadelicDrupalWrapper", array(
"cache_set",
));
$drupal
->expects($this
->once())
->method("cache_set")
->with("tagadelic_cloud_1337", $this->object);
$this->object
->set_drupal($drupal);
$this->object
->to_cache();
}
public function testGetCalculatedTags() {
foreach ($this->mock_tags as $mock_tag) {
$mock_tag
->expects($this
->once())
->method('set_weight')
->with($this
->greaterThan(0))
->will($this
->returnSelf());
$mocks[] = $mock_tag;
}
$this->object = new TagadelicCloud(1337, $mocks);
$this->object
->get_tags();
}
public function testGetCalculatedWeights() {
$mocks = array();
$assert_table = array(
array(
"Mary Read",
1,
1,
),
array(
"Jean Fleury",
1,
1,
),
array(
"François Le Clerc",
1,
1,
),
array(
"Blackbeard",
2,
1,
),
array(
"Henry Morgan",
3,
2,
),
array(
"Bartolomew Roberts",
10,
3,
),
array(
"Stede Bonnet",
20,
4,
),
array(
"Edward Low",
40,
5,
),
array(
"Anne Bonny",
100,
6,
),
);
$i = 1;
foreach ($assert_table as $assertion) {
$mock = $this
->getMock("TagadelicTag", array(
"set_weight",
), array(
$i++,
$assertion[0],
$assertion[1],
));
$mock
->expects($this
->once())
->method("set_weight")
->with($assertion[2])
->will($this
->returnSelf());
$mocks[] = $mock;
}
$this->object = new TagadelicCloud(1337, $mocks);
$this->object
->get_tags();
}
public function testNotSorted() {
$this->object
->add_tag($this
->addTagStub("bill", "William Kidd", 100));
$this->object
->add_tag($this
->addTagStub("cheung", "Cheung Po Tsai", 20));
$expected_order = array(
"William Kidd",
"Cheung Po Tsai",
);
$given_order = array();
foreach ($this->object
->get_tags() as $tag) {
$given_order[] = $tag
->get_name();
}
$this
->assertSame($given_order, $expected_order);
}
public function testSortByName() {
$drupal = $this
->getMock("TagadelicDrupalWrapper", array(
"check_plain",
));
$drupal
->expects($this
->any())
->method("check_plain")
->will($this
->returnArgument(0));
$this->object
->set_drupal($drupal);
$this->object
->add_tag($this
->addTagStub("bill", "William Kidd", 100));
$this->object
->add_tag($this
->addTagStub("cheung", "Cheung Po Tsai", 20));
$expected_order = array(
"Cheung Po Tsai",
"William Kidd",
);
$given_order = array();
$this->object
->sort("name");
foreach ($this->object
->get_tags() as $tag) {
$given_order[] = $tag
->get_name();
}
$this
->assertSame($given_order, $expected_order);
}
public function testSortByNameWithInternationalCharacters() {
$drupal = $this
->getMock("TagadelicDrupalWrapper", array(
"check_plain",
));
$drupal
->expects($this
->any())
->method("check_plain")
->will($this
->returnArgument(0));
$this->object
->set_drupal($drupal);
$this->object
->add_tag($this
->addTagStub("ae", "Ä", 10));
$this->object
->add_tag($this
->addTagStub("oe", "Ö", 20));
$this->object
->add_tag($this
->addTagStub("ue", "Ü", 30));
$this->object
->add_tag($this
->addTagStub("u", "U", 40));
$this->object
->add_tag($this
->addTagStub("o", "O", 50));
$this->object
->add_tag($this
->addTagStub("a", "A", 60));
$expected_order = array(
"A",
"Ä",
"O",
"Ö",
"U",
"Ü",
);
$given_order = array();
setlocale(LC_COLLATE, 'de_DE.utf8');
$this->object
->sort("name");
foreach ($this->object
->get_tags() as $tag) {
$given_order[] = $tag
->get_name();
}
$this
->assertSame($given_order, $expected_order);
}
public function testSortByCount() {
$drupal = $this
->getMock("TagadelicDrupalWrapper", array(
"check_plain",
));
$drupal
->expects($this
->any())
->method("check_plain")
->will($this
->returnArgument(0));
$this->object
->set_drupal($drupal);
$this->object
->add_tag($this
->addTagStub("bill", "William Kidd", 100));
$this->object
->add_tag($this
->addTagStub("cheung", "Cheung Po Tsai", 200));
$expected_order = array(
"Cheung Po Tsai",
"William Kidd",
);
$given_order = array();
$this->object
->sort("count");
foreach ($this->object
->get_tags() as $tag) {
$given_order[] = $tag
->get_name();
}
$this
->assertSame($given_order, $expected_order);
}
public function testSortRandom() {
$drupal = $this
->getMock("TagadelicDrupalWrapper", array(
"shuffle",
));
$drupal
->expects($this
->once())
->method("shuffle");
$this->object
->set_drupal($drupal);
$this->object
->sort("random");
}
private function addTagStub($id, $name, $count) {
$stub = $this
->getMock("TagadelicTag", array(
"get_name",
"get_count",
"set_weight",
), array(
$id,
$name,
$count,
));
$stub
->expects($this
->any())
->method("get_name")
->will($this
->returnValue($name));
$stub
->expects($this
->any())
->method("get_count")
->will($this
->returnValue($count));
$this->mock_tags[$id] = $stub;
return $stub;
}
}