You are here

TagadelicCloudTest.php in Tagadelic 7.2

File

tests/TagadelicCloudTest.php
View source
<?php

require_once 'TagadelicCloud.php';
require_once 'TagadelicTag.php';

/**
 * Generated by PHPUnit_SkeletonGenerator on 2012-05-16 at 15:23:33.
 */
class TagadelicCloudTest extends PHPUnit_Framework_TestCase {

  /**
   * @var TagadelicCloud
   */
  protected $object;

  /**
   * Sets up the fixture, for example, opens a network connection.
   * This method is called before a test is executed.
   */
  protected function setUp() {
    $this->object = new TagadelicCloud(1337);
    $this
      ->addTagStub("jane", "Jane", 200);
    $this
      ->addTagStub("blackbeard", "Blackbeard", 100);
  }

  /**
   * Tears down the fixture, for example, closes a network connection.
   * This method is called after a test is executed.
   */
  protected function tearDown() {
  }

  /**
   * constructor should set the ID to the variable passed.
   */
  public function test__construct() {

    // First param should be assigned to the id.
    $this
      ->assertAttributeSame(1337, "id", $this->object);

    // Optional second argument pre-sets the tags.
    $this->object = new TagadelicCloud(1337, $this->mock_tags);
    $this
      ->assertAttributeSame($this->mock_tags, "tags", $this->object);
  }

  /**
   * @covers TagadelicCloud::get_id
   */
  public function testGet_id() {
    $this
      ->assertEquals(1337, $this->object
      ->get_id());
  }

  /**
   * @covers TagadelicCloud::get_tags
   * Tests if get_Tags returns an array only.
   */
  public function testGet_tags() {
    $this->object = new TagadelicCloud(1337, $this->mock_tags);
    $this
      ->assertSame($this->mock_tags, $this->object
      ->get_tags());
  }

  /**
   * @covers TagadelicCloud::add_tag
   */
  public function testAdd_tag() {
    $this->object
      ->add_tag($this->mock_tags["blackbeard"]);
    $this
      ->assertAttributeContains($this->mock_tags["blackbeard"], "tags", $this->object);
  }

  /**
   * @covers TagadelicCloud::add_tag()
   */
  public function testAdd_tagIsChainable() {
    $this
      ->assertEquals($this->object
      ->add_tag($this->mock_tags["blackbeard"]), $this->object);
  }

  /**
   * @covers TagadelicCloud::set_drupal()
   */
  public function testSet_drupal() {
    $drupal = new StdClass();
    $this->object
      ->set_drupal($drupal);
    $this
      ->assertAttributeSame($drupal, "drupal", $this->object);
  }

  /**
   * @covers TagadelicCloud::drupal()
   */
  public function testDrupalReturnsSetValue() {
    $drupal = "ThisIsDrupal";
    $this->object
      ->set_drupal($drupal);
    $this
      ->assertSame($this->object
      ->drupal(), $drupal);
  }

  /**
   * @covers TagadelicCloud::drupal()
   */
  public function testDrupalInstantiatesNewWrapper() {
    $this->object
      ->set_drupal(NULL);
    $drupal = $this
      ->getMock("TagadelicDrupalWrapper");
    $this
      ->assertInstanceOf("TagadelicDrupalWrapper", $this->object
      ->drupal());
  }

  /**
   * @covers tagadeliccloud::from_cache
   */
  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);
  }

  /**
   * @covers tagadeliccloud::to_cache
   */
  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();
  }

  /**
   * Get Tags should calculate the weights
   */
  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();
  }

  /**
   * Get Tags should calculate the weights
   */
  public function testGetCalculatedWeights() {
    $mocks = array();
    $assert_table = array(
      //       name              , count , weight
      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();
  }

  /**
   * Default is not sorted
   **/
  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);
  }

  /**
   * Sort By name
   **/
  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);
  }

  /**
   * Sort By name should sort International characters like ÅÄÖABO
   *
   * Uses Locale de_DE, which must be available on your system.
   *  Debian/Ubuntu users:
   *   $ locale -a
   *   $ #lists all locales available. Find if de_DE is there, if not, install it.
   *   $ sudo apt-get install language-pack-de-base
   **/
  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);
  }

  /**
   * Sort by count. Highest count first.
   */
  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);
  }

  /**
   * Sort random.
   */
  public function testSortRandom() {
    $drupal = $this
      ->getMock("TagadelicDrupalWrapper", array(
      "shuffle",
    ));
    $drupal
      ->expects($this
      ->once())
      ->method("shuffle");
    $this->object
      ->set_drupal($drupal);
    $this->object
      ->sort("random");
  }

  /**
   * Creates a stub for a tag
   */
  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;
  }

}

Classes

Namesort descending Description
TagadelicCloudTest Generated by PHPUnit_SkeletonGenerator on 2012-05-16 at 15:23:33.