Skip to content
Snippets Groups Projects
Commit e999f53a authored by Ghislain Loas's avatar Ghislain Loas
Browse files

Merge branch 'fix_reading_first_element_of_tables' into 'master'

fix reading first element in table in collection and loader

See merge request !23
parents 4b288ded ecd5356f
1 merge request!23fix reading first element in table in collection and loader
Pipeline #8808 passed with stage
in 31 seconds
......@@ -84,11 +84,25 @@ class Storm_Collection extends ArrayObject {
public function first() {
return array_values($this->getArrayCopy())[0];
if (!$values = $this->_getValues())
return null;
return reset($values);
}
public function last() {
if (!$values = array_reverse($this->_getValues()))
return null;
return reset($values);
}
public function includes($object) {
return in_array($object, $this->getArrayCopy());
}
protected function _getValues() {
return array_values($this->getArrayCopy());
}
}
\ No newline at end of file
......@@ -452,8 +452,8 @@ class Storm_Model_Loader {
if (count($instances) == 0) {
return null;
}
$this->cacheInstance($instances[0]);
return $instances[0];
$first_instance = reset($instances);
$this->cacheInstance($first_instance);
return $first_instance;
}
}
<?php
/*
STORM is under the MIT License (MIT)
Copyright (c) 2010-2018 Agence Française Informatique http://www.afi-sa.fr
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
class Storm_Test_CollectionEmptyTest extends Storm_Test_ModelTestCase {
/** @test */
public function firstElementShouldReturnNull() {
$this->assertNull((new Storm_Model_Collection([]))->first());
}
/** @test */
public function lastElementShouldReturnNull() {
$this->assertNull((new Storm_Model_Collection([]))->last());
}
}
class Storm_Test_CollectionOfStringsTest extends Storm_Test_ModelTestCase {
/** @test */
public function firstElementShouldBeOne() {
$this->assertEquals('first', (new Storm_Model_Collection(['first', 'second']))->first());
}
/** @test */
public function lastElementShouldBeTroll() {
$this->assertEquals('troll', (new Storm_Model_Collection(['elf', 'troll']))->last());
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment