PhasePseudoRecordTest.php 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/**
 * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
 *
 * BOKEH is free software; you can redistribute it and/or modify
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
 * BOKEH is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
 * along with BOKEH; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */


23 24 25 26 27 28 29
abstract class PhasePseudoRecordTestCase extends Class_Cosmogramme_Integration_PhaseTestCase {
  protected
    $_phase_name,
    $_phase_label,
    $_model_name,
    $_model_id;

30 31
  public function setUp() {
    parent::setUp();
32 33 34
    $this->_phase = $this->_buildPhase($this->_phase_name)
                         ->setMemoryCleaner(function() {})
                         ->run();
35 36 37 38
  }


  protected function _prepareFixtures() {
39 40
    $loader = $this
      ->onLoaderOfModel($this->_model_name)
41

42 43 44 45 46
      ->whenCalled('findAllBy')
      ->with(['where' => $this->_model_id . ' > 0',
              'order' => $this->_model_id,
              'limit' => 100])
      ->answers([$this->_buildModel(15)])
47

48 49 50 51 52
      ->whenCalled('findAllBy')
      ->with(['where' => $this->_model_id . ' > 15',
              'order' => $this->_model_id,
              'limit' => 100])
      ->answers([$this->_buildModel(16)])
53

54
      ->whenCalled('findAllBy')->answers([]);
55
  }
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81


  protected function _buildModel($id) {
    return $this->fixture($this->_model_name,
                          $this->_getModelAttribs($id));
  }


  protected function _getModelAttribs($id) {
    return ['id' => $id];
  }


  protected function _getModelRecord($id) {
    return call_user_func([$this->_model_name, 'find'], $id)->getNotice();
  }


  public function assertNullRecordOf($id) {
    $this->assertNull($this->_getModelRecord($id));
  }


  public function assertNotNullRecordOf($id) {
    $this->assertNotNull($this->_getModelRecord($id));
  }
82 83 84 85
}



86
trait PhasePseudoRecordInvalidPreviousPhase {
87 88 89 90
  protected function _getPreviousPhase() {
    return (new Class_Cosmogramme_Integration_Phase(2))
      ->beCron();
  }
91 92


93 94
  /** @test */
  public function recordShouldNotBeCreated() {
95
    $this->assertNullRecordOf(15);
96
  }
97 98


99 100
  /** @test */
  public function logShouldNotContainsPhaseLabel() {
101
    $this->assertNotLogContains($this->_phase_label);
102 103
  }
}
104 105


106
trait PhasePseudoRecordValidCron {
107
  /** @test */
108 109
  public function shouldHaveCreatedFirstPseudoRecord() {
    $this->assertNotNullRecordOf(15);
110 111 112
  }


113
  /** @test */
114 115
  public function shouldHaveCreatedSecondPseudoRecord() {
    $this->assertNotNullRecordOf(16);
116 117 118 119 120
  }


  /** @test */
  public function traitemenPhaseShouldBePseudoRecord() {
121
    $this->assertEquals($this->_phase_label,
122 123
                        Class_CosmoVar::get('traitement_phase'));
  }
124 125


126 127
  /** @test */
  public function logShouldContainsPhaseLabel() {
128
    $this->assertLogContains($this->_phase_label);
129 130 131 132 133 134 135
  }


  /** @test */
  public function dataNombreShouldBe2() {
    $this->assertEquals(2, $this->_phase->getData('nombre'));
  }
136 137


138 139 140 141 142 143
  /** @test */
  public function insertCountShouldBe7() {
    $this->assertEquals(7,
                        $this->_phase
                        ->getCount(Class_Cosmogramme_Integration_Phase::RECORD_INSERT));
  }
144

145

146 147 148 149
  /** @test */
  public function shouldDisplayRunSummary() {
    $this->assertLogContains('2 notice(s) traitée(s)');
    $this->assertLogContains('Temps de traitement');
150
  }
151
}
152 153


154 155

trait PhasePseudoRecordValidInteractiveTimeout {
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  protected function _prepareFixtures() {
    parent::_prepareFixtures();
    $this->_time_source = $this
      ->mock(new Class_TimeSource())
      ->whenCalled('time')->answers(mktime(1, 0, 0, 9, 1, 2015))
      ->whenCalled('dateYmd')->answers('2015-09-01');

    Class_Cosmogramme_Chronometre::setTimeSource($this->_time_source);
    $this->_chrono->start(mktime(0, 0, 0, 9, 1, 2015));
  }


  /** @test */
  public function shouldNotHaveCreatedRecord() {
    $this->assertEquals(0, Class_Notice::count());
  }
}



176
trait PhasePseudoRecordValidInteractiveCallback {
177 178 179 180 181 182 183 184 185 186 187 188 189 190
  protected function _prepareFixtures() {
    parent::_prepareFixtures();
    $this->_time_source = $this
      ->mock(new Class_TimeSource())
      ->whenCalled('time')->answers(mktime(0, 0, 0, 9, 1, 2015))
      ->whenCalled('dateYmd')->answers('2015-09-01');

    Class_Cosmogramme_Chronometre::setTimeSource($this->_time_source);
    $this->_chrono->start(mktime(0, 0, 0, 9, 1, 2015));
  }


  /** @test */
  public function shouldNotHaveCreatedArticle15PseudoRecord() {
191
    $this->assertNullRecordOf(15);
192 193 194 195 196
  }


  /** @test */
  public function shouldHaveCreatedArticle16PseudoRecord() {
197
    $this->assertNotNullRecordOf(16);
198 199 200 201 202
  }


  /** @test */
  public function printerShouldContainsPhaseLabel() {
203
    $this->assertPrinterContains($this->_phase_label);
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
  }


  /** @test */
  public function dataNombreShouldBe2() {
    $this->assertEquals(2, $this->_phase->getData('nombre'));
  }


  /** @test */
  public function insertCountShouldBe7() {
    $this->assertEquals(7,
                        $this->_phase
                        ->getCount(Class_Cosmogramme_Integration_Phase::RECORD_INSERT));
  }
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
}


/**
 * CMS
 */

abstract class PhasePseudoRecordCmsTestCase extends PhasePseudoRecordTestCase {
  protected
    $_phase_name = 'PseudoRecordCms',
    $_phase_label = 'Pseudo-notices : CMS',
    $_model_name = 'Class_Article',
    $_model_id = 'id_article';


  protected function _getModelAttribs($id) {
    return array_merge(parent::_getModelAttribs($id),
                       ['titre' => 'Article ' . $id,
                        'contenu' => 'Content ' . $id]);
  }
}



class PhasePseudoRecordCmsInvalidPreviousPhaseTest extends PhasePseudoRecordCmsTestCase {
  use PhasePseudoRecordInvalidPreviousPhase;
}



class PhasePseudoRecordCmsValidCronTest extends PhasePseudoRecordCmsTestCase {
  use PhasePseudoRecordValidCron;

  protected function _getPreviousPhase() {
    return (new Class_Cosmogramme_Integration_Phase(0.1))
      ->setData('pointeur_reprise', 45)
      ->setCount(Class_Cosmogramme_Integration_Phase::RECORD_INSERT, 5)
      ->beCron();
  }
}



class PhasePseudoRecordCmsValidInteractiveTimeoutTest
  extends PhasePseudoRecordCmsTestCase {
  use PhasePseudoRecordValidInteractiveTimeout;

  protected function _getPreviousPhase() {
    return (new Class_Cosmogramme_Integration_Phase(0.1));
  }
}



class PhasePseudoRecordCmsValidInteractiveCallbackTest
  extends PhasePseudoRecordCmsTestCase {
  use PhasePseudoRecordValidInteractiveCallback;

  protected function _getPreviousPhase() {
    return (new Class_Cosmogramme_Integration_Phase(0.2))
      ->beCallBack()
      ->setData('nombre', 1)
      ->setData('pointeur_reprise', 15)
      ->setCount(Class_Cosmogramme_Integration_Phase::RECORD_INSERT, 6);
  }
}


/**
 * RSS
 */

abstract class PhasePseudoRecordRssTestCase extends PhasePseudoRecordTestCase {
  protected
    $_phase_name = 'PseudoRecordRss',
    $_phase_label = 'Pseudo-notices : FILS RSS',
    $_model_name = 'Class_Rss',
    $_model_id = 'id_rss';

  protected function _getModelAttribs($id) {
    return array_merge(parent::_getModelAttribs($id),
                       ['titre' => 'RSS ' . $id]);
  }
}



class PhasePseudoRecordRssInvalidPreviousPhaseTest extends PhasePseudoRecordRssTestCase {
  use PhasePseudoRecordInvalidPreviousPhase;
}



class PhasePseudoRecordRssValidCronTest extends PhasePseudoRecordRssTestCase {
  use PhasePseudoRecordValidCron;

  protected function _getPreviousPhase() {
    return (new Class_Cosmogramme_Integration_Phase(0.2))
      ->setData('pointeur_reprise', 45)
      ->setCount(Class_Cosmogramme_Integration_Phase::RECORD_INSERT, 5)
      ->beCron();
  }
}



class PhasePseudoRecordRssValidInteractiveTimeoutTest
  extends PhasePseudoRecordRssTestCase {
  use PhasePseudoRecordValidInteractiveTimeout;

  protected function _getPreviousPhase() {
    return (new Class_Cosmogramme_Integration_Phase(0.2));
  }
}



class PhasePseudoRecordRssValidInteractiveCallbackTest
  extends PhasePseudoRecordRssTestCase {
  use PhasePseudoRecordValidInteractiveCallback;

  protected function _getPreviousPhase() {
    return (new Class_Cosmogramme_Integration_Phase(0.3))
      ->beCallBack()
      ->setData('nombre', 1)
      ->setData('pointeur_reprise', 15)
      ->setCount(Class_Cosmogramme_Integration_Phase::RECORD_INSERT, 6);
  }
}



/**
 * SITO
 */

abstract class PhasePseudoRecordSitoTestCase extends PhasePseudoRecordTestCase {
  protected
    $_phase_name = 'PseudoRecordSitotheque',
    $_phase_label = 'Pseudo-notices : SITOTHEQUE',
    $_model_name = 'Class_Sitotheque',
    $_model_id = 'id_sito';

  protected function _getModelAttribs($id) {
    return array_merge(parent::_getModelAttribs($id),
                       ['titre' => 'Sito ' . $id,
                        'url' => 'http://server.com/' . $id]);
  }
}



class PhasePseudoRecordSitoInvalidPreviousPhaseTest extends PhasePseudoRecordSitoTestCase {
  use PhasePseudoRecordInvalidPreviousPhase;
}



class PhasePseudoRecordSitoValidCronTest extends PhasePseudoRecordSitoTestCase {
  use PhasePseudoRecordValidCron;

  protected function _getPreviousPhase() {
    return (new Class_Cosmogramme_Integration_Phase(0.3))
      ->setData('pointeur_reprise', 45)
      ->setCount(Class_Cosmogramme_Integration_Phase::RECORD_INSERT, 5)
      ->beCron();
  }
}



class PhasePseudoRecordSitoValidInteractiveTimeoutTest
  extends PhasePseudoRecordSitoTestCase {
  use PhasePseudoRecordValidInteractiveTimeout;

  protected function _getPreviousPhase() {
    return (new Class_Cosmogramme_Integration_Phase(0.3));
  }
}



class PhasePseudoRecordSitoValidInteractiveCallbackTest
  extends PhasePseudoRecordSitoTestCase {
  use PhasePseudoRecordValidInteractiveCallback;

  protected function _getPreviousPhase() {
    return (new Class_Cosmogramme_Integration_Phase(0.4))
      ->beCallBack()
      ->setData('nombre', 1)
      ->setData('pointeur_reprise', 15)
      ->setCount(Class_Cosmogramme_Integration_Phase::RECORD_INSERT, 6);
  }
}



/**
 * Albums
 */

abstract class PhasePseudoRecordAlbumTestCase extends PhasePseudoRecordTestCase {
  protected
    $_phase_name = 'PseudoRecordAlbum',
    $_phase_label = 'Pseudo-notices : RESSOURCES NUMERIQUES',
    $_model_name = 'Class_Album',
    $_model_id = 'id';

427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
  protected function _prepareFixtures() {
    Class_Cosmogramme_Integration_PhasePseudoRecordAlbum::setTimeSource(new TimeSourceForTest('2019-01-09 12:11:38'));

    Class_CosmoVar::setValueOf('date_maj_albums', '2018-12-18 17:21:54');

    $loader = $this
      ->onLoaderOfModel($this->_model_name)
      ->whenCalled('findAllBy')
      ->with(['where' => $this->_model_id . ' > 0 and date_maj >= \'2018-12-18 17:21:54\'',
              'order' => $this->_model_id,
              'limit' => 100])
      ->answers([$this->_buildModel(15)])

      ->whenCalled('findAllBy')
      ->with(['where' => $this->_model_id . ' > 15 and date_maj >= \'2018-12-18 17:21:54\'',
              'order' => $this->_model_id,
              'limit' => 100])
      ->answers([$this->_buildModel(16)])

      ->whenCalled('findAllBy')->answers([])
      ;
  }


  public function tearDown() {
    Class_Cosmogramme_Integration_PhasePseudoRecordAlbum::setTimeSource(null);
    parent::tearDown();
  }


457 458 459 460
  protected function _getModelAttribs($id) {
    return array_merge(parent::_getModelAttribs($id),
                       ['titre' => 'Album ' . $id,
                        'visible' => 1,
461 462
                        'status' => Class_Album::STATUS_VALIDATED,
                        'date_maj' => '2018-12-18 17:21:54']);
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
  }
}



class PhasePseudoRecordAlbumInvalidPreviousPhaseTest extends PhasePseudoRecordAlbumTestCase {
  use PhasePseudoRecordInvalidPreviousPhase;
}



class PhasePseudoRecordAlbumValidCronTest extends PhasePseudoRecordAlbumTestCase {
  use PhasePseudoRecordValidCron;

  protected function _getPreviousPhase() {
    return (new Class_Cosmogramme_Integration_Phase(0.4))
      ->setData('pointeur_reprise', 45)
      ->setCount(Class_Cosmogramme_Integration_Phase::RECORD_INSERT, 5)
      ->beCron();
  }
}



class PhasePseudoRecordAlbumValidInteractiveTimeoutTest
  extends PhasePseudoRecordAlbumTestCase {
  use PhasePseudoRecordValidInteractiveTimeout;

  protected function _getPreviousPhase() {
    return (new Class_Cosmogramme_Integration_Phase(0.4));
  }
}



class PhasePseudoRecordAlbumValidInteractiveCallbackTest
  extends PhasePseudoRecordAlbumTestCase {
  use PhasePseudoRecordValidInteractiveCallback;

  protected function _getPreviousPhase() {
    return (new Class_Cosmogramme_Integration_Phase(0.5))
      ->beCallBack()
      ->setData('nombre', 1)
      ->setData('pointeur_reprise', 15)
507
      ->setData('pointeur', '2018-12-18 17:21:54')
508 509
      ->setCount(Class_Cosmogramme_Integration_Phase::RECORD_INSERT, 6);
  }
510 511 512 513 514 515


  /** @test */
  public function dateMajAlbumsShouldBeUpdated() {
    $this->assertEquals('2019-01-09 12:11:38', Class_CosmoVar::get('date_maj_albums'));
  }
516
}