Completed
Pull Request — master (#53)
by Helpful
02:20
created

StaticPagesQueueTest::testAddManyURLs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
4
class StaticPagesQueueTest extends SapphireTest {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
5
	
6
	public function setUp() {
7
		parent::setUp();
8
		Config::inst()->nest();
9
		Config::inst()->update('StaticPagesQueue', 'realtime', true);
10
	}
11
12
	public function tearDown() {
13
		// Remove all StaticPagesQueue that might be left after running a testcase
14
		self::empty_temp_db();
15
16
		Config::inst()->unnest();
17
		parent::tearDown();
18
	}
19
	
20
	public function testAddOneURL() {
21
		$obj = StaticPagesQueue::get()->First();
22
		$this->assertFalse( $obj instanceof StaticPagesQueue );
23
24
		Injector::inst()->get('URLArrayObject')->addUrls(array('test1' => 1));
25
26
		$obj = StaticPagesQueue::get()->First();
27
		$this->assertTrue($obj instanceof StaticPagesQueue);
28
		$this->assertEquals('test1', $obj->URLSegment );
29
	}
30
	
31
	public function testAddManyURLs() {
32
		$objSet = DataObject::get('StaticPagesQueue');
0 ignored issues
show
Unused Code introduced by
$objSet is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
33
		Injector::inst()->get('URLArrayObject')->addUrls(array('test2-2' => 2 ,'test2-10' => 10),true);
34
		$objSet = DataObject::get('StaticPagesQueue');
35
		$this->assertEquals( 2, $objSet->Count() );
36
		$this->assertDOSContains(array( 
37
			  array('URLSegment' => 'test2-2'), 
38
			  array('URLSegment' => 'test2-10')
39
		), $objSet);
40
	}
41
	
42
	public function testGetNextUrlInEmptyQueue() {
43
		$this->assertEquals( '', StaticPagesQueue::get_next_url() );
44
	}
45
	
46
	public function testGetNextUrlByPriority() {
47
		Injector::inst()->get('URLArrayObject')->addUrls(array('monkey' => 1 ,'stool' => 10),true);
48
		$this->assertEquals( 'stool', StaticPagesQueue::get_next_url() );
49
		$this->assertEquals( 'monkey', StaticPagesQueue::get_next_url() );
50
	}
51
	
52
	public function testBumpThePriority() {
53
		Injector::inst()->get('URLArrayObject')->addUrls(array('foobar' => 1),true);
54
		Injector::inst()->get('URLArrayObject')->addUrls(array('monkey' => 10),true);
55
		// Adding a duplicate
56
		Injector::inst()->get('URLArrayObject')->addUrls(array('monkey' => 1),true);
57
		$this->assertEquals(3, DataObject::get('StaticPagesQueue')->Count());
58
		StaticPagesQueue::get_next_url();
59
		$this->assertEquals(10, DataObject::get('StaticPagesQueue')->Last()->Priority);
60
	}
61
	
62
	public function testNothingToDelete() {
63
		$this->assertFalse( StaticPagesQueue::delete_by_link("Im not there"));
64
	}
65
	
66
	public function testGetNextUrlByPrioAndDate() {
67
		$oldObject = new StaticPagesQueue(array('URLSegment'=>'old/page','Priority'=>10),true);
68
		$oldObject->write();
69
		$oldObject->Created = "2010-01-01 10:00:01";
70
		$oldObject->write();
71
		Injector::inst()->get('URLArrayObject')->addUrls(array('monkey/bites' => 1 ,'stool/falls' => 10),true);
72
		$this->assertEquals( 'old/page', StaticPagesQueue::get_next_url());
73
		$this->assertEquals( 'stool/falls', StaticPagesQueue::get_next_url());
74
		$this->assertEquals( 'monkey/bites', StaticPagesQueue::get_next_url());
75
	}
76
	
77
	public function testMarkAsDone() {
78
		Injector::inst()->get('URLArrayObject')->addUrls(array('monkey' => 1),true);
79
		$url = StaticPagesQueue::get_next_url();
80
		$this->assertTrue( StaticPagesQueue::delete_by_link($url) );
81
		#$this->assertNull( DataObject::get('StaticPagesQueue'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
82
	}
83
84
	/**
85
	 * This tests that queueitems that are marked as regenerating, but are older 
86
	 * than 10 minutes actually gets another try
87
	 */
88
	public function testDeadEntries() {
89
		$oldObject = new StaticPagesQueue(array('URLSegment'=>'old/page','Freshness'=>'regenerating'));
90
		$oldObject->write();
91
		// Update the entry directly, this is the only way to change LastEdited to a set value
92
		DB::query('UPDATE "StaticPagesQueue" SET "LastEdited" =\''.date("Y-m-d H:i:s", time()-60*11).'\'');
93
		Injector::inst()->get('URLArrayObject')->addUrls(array('monkey/bites' => 1),true);
94
		$this->assertEquals( 'monkey/bites', StaticPagesQueue::get_next_url());
95
		$this->assertEquals( 'old/page', StaticPagesQueue::get_next_url());
96
		Injector::inst()->get('URLArrayObject')->addUrls(array('should/be/next' => 1),true);
97
		$this->assertEquals( 'should/be/next', StaticPagesQueue::get_next_url());
98
	}
99
100
	public function testRemoveDuplicates() {
101
		Injector::inst()->get('URLArrayObject')->addUrls(array(
102
			'test1' => 1 ,
103
			'test2' => 1,
104
			'test3' => 1
105
		));
106
		Injector::inst()->get('URLArrayObject')->addUrls(array(
107
			'test2' => 2, // duplicate
108
			'test3' => 2, // duplicate
109
		));
110
		Injector::inst()->get('URLArrayObject')->addUrls(array(
111
			'test2' => 3, // duplicate
112
		));
113
		$test1Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test1\'');
114
		$test2Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test2\'');
115
		$test3Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test3\'');
116
		$this->assertEquals(1, $test1Objs->Count());
117
		$this->assertEquals(3, $test2Objs->Count());
118
		$this->assertEquals(2, $test3Objs->Count());
119
120
		StaticPagesQueue::remove_duplicates($test1Objs->First()->ID);
121
		$test1Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test1\'');
122
		$test2Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test2\'');
123
		$test3Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test3\'');
124
		$this->assertEquals(1, $test1Objs->Count(), 'Keeps original instance without any duplicates found');
125
		$this->assertEquals(3, $test2Objs->Count(), 'Doesnt remove unrelated duplicates');
126
		$this->assertEquals(2, $test3Objs->Count(), 'Doesnt remove unrelated duplicates');
127
128
		StaticPagesQueue::remove_duplicates($test2Objs->First()->ID);
129
		$test2Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test2\'');
130
		$this->assertEquals(1, $test2Objs->Count(), 'Removing a single duplicate');
131
		
132
		StaticPagesQueue::remove_duplicates($test3Objs->First()->ID);
133
		$test3Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test3\'');
134
		$this->assertEquals(1, $test3Objs->Count(), 'Removing multiple duplicates');
135
	}
136
137
	/**
138
	 * 
139
	 * Test takes about 22sec on my local environment
140
	 */
141
//    public function testPerformance() {
0 ignored issues
show
Unused Code Comprehensibility introduced by
51% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
142
//        $start_time = microtime(true);
143
//        $array = array();
144
//        for($idx=0;$idx<1000;$idx++){
145
//            $array["page-".$idx] = $idx;
146
//        }
147
//        StaticPagesQueue::add_urls($array);
148
//        while( $url = StaticPagesQueue::get_next_url() ) {
149
//            StaticPagesQueue::delete_by_link($url);
150
//        }
151
//        $time_end = microtime(true);
152
//		$end_time = $time_end - $start_time;
153
//        echo sprintf("%.3fs" ,$end_time);
154
//    }
155
}
156